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
NAME ZONE MACHINE_TYPE PREEMPTIBLE INTERNAL_IP EXTERNAL_IP STATUS instance-1 europe-west1-d n1-standard-1 10.132.0.2 220.127.116.11 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 18.104.22.168 RUNNING