Skip to Content

Moving instances between zones in one command

While keeping the same IP addresses

Posted on 2 mins read

With Google Compute Engine each instance is running in a single zone within a region. Sometimes you want to move a specific instance to another zone inside a region to ensure multi-zone redundancy or reduce fees due to inter-zone traffic.

Doing this manually is a little bit complex, you have to :

  • make a snapshot over instances’ disks,
  • use snapshots to create persistent disks in the destination zone,
  • convert the instance’s ephemeral IP address into a static IP address,
  • run instance based on new disks in the new zone,
  • assign a static IP address and convert it back to ephemeral,
  • delete the snapshots, the old instance and its disks.

It’s not very easy, especially if you want to do this multiples times. Fortunately with the gcloud command-line interface, you can do this with a single command !

Imagine we have an instance in europe-west1-c and we want to move it to europe-west1-d :

NAME        ZONE            MACHINE_TYPE   PREEMPTIBLE  INTERNAL_IP  EXTERNAL_IP   STATUS
instance-1  europe-west1-d  n1-standard-1               10.132.0.2   35.187.10.59  RUNNING

And then, we run the magic command: gcloud compute instance move $INSTANCE_ID --zone $SOURCE_ZONE --destination-zone $DEST_ZONE :

$ gcloud compute instances move instance-1 --zone europe-west1-d --destination-zone europe-west1-c
Moving gce instance instance-1...done.

That’s all! Same instance, same IP addresses, different zone!

NAME        ZONE            MACHINE_TYPE   PREEMPTIBLE  INTERNAL_IP  EXTERNAL_IP   STATUS
instance-1  europe-west1-c  n1-standard-1               10.132.0.2   35.187.10.59  RUNNING
comments powered by Disqus