Migrate Google compute engine instance to a different region - google-compute-engine

I am trying to migrate a couple of compute engine instances from Europe to Asia.
I need to do this because most of the users for those servers are in Asia and they get faster access to servers in their region. I am trying following command.
gcloud compute instances move asiawindows1 --zone europe-west1-d --destination-zone asia-east1-c
I get following error when I try this
Instances belonging to subnetworks cannot be moved interregionally.
What is the best way to move these servers without manually setting them up in the Asia zone?

Follow steps below in Google Cloud Platform
Create snapshot of your VM instance
Create disk with this snapshot
Create image with disk created
Create a new instance using the image created in steps above and change your zone

The command you are using is for moving across zones of same region and not across regions.
You can create an image of your instance and use the image to create a new instance in different region.

If the auto move is not happening, you can try the manual move of vm instance. First list all the disk to choose the right disk to take snapshot.
gcloud compute disks list
Identify the name of disk from the list and create a snapshot of that one
gcloud compute disks snapshot <disk_name> --snapshot-names <snapshot_disk_name> --zone <current_zone>
Now you can delete your instance in you are trying to move.
gcloud compute instances delete <instance_name> --zone <instance_zone>
Next step is to create a disk from the snapshot you have created in the new zone where you want to create the instance.
gcloud compute disks create <disk_name> --source-snapshot <snapshot_disk_name> --zone <new_zone_name>
Now, you have to create an instance with the disk you have just created mounted on it. Please choose the right machine type for your need, here am going to create an instance with f1-micro.
gcloud compute instances create <instance_name> --machine-type f1-micro --zone <new_zone>
--disk name=<disk_name>,boot=yes,mode=rw
That's it.
Now if you want you can delete the snapshot
gcloud compute snapshots delete <snapshot_name>

GCP Guide says Snapshot the root disk, create an image, and use the image for the new VM root disk.

Related

How to restore instance using snapshot in Google Compute Engine?

I created a snapshot of a VM instance via cloud console. I would like to know how I can restore an instance using a snapshot. The documentation for compute engine is not very helpful. The instance runs on Ubuntu. Thanks.
To restore an instance from a snapshot without deleting/re-creating the instance:
Shut down the instance and detach the boot disk: gcloud beta compute instances detach-disk INSTANCE_NAME --disk BOOT_DISK_NAME
Create a new disk from the snapshot: gcloud compute disks create DISK_NAME --source-snapshot SNAPSHOT_NAME
Attach the disk created from step 2 as the boot disk: gcloud beta compute instances attach-disk INSTANCE_NAME --disk DISK_NAME --boot
Restoring the instance without deleting/re-creating the instance means that after restore, the instance will keep its IP address and other properties like tags, labels, etc.
In Console, you can go to VM Instances from Compute Engine tab.
There you click on '+Create Instance'
and there in section of boot-disk, you can navigate to 'snapshots' tab and select the snapshot you took.
Like this from console, you can Restore your instance.
Let me know, if this doesn't work for you!
Try using:
gcloud compute disks \
create <NEW_INSTANCE_NAME> \
--source-snapshot <SNAPSHOT_NAME> \
--type pd-ssd \
--zone <ZONE>
You could find useful instruction here.
If you want to do it in the web interface, it's pretty easy. Edit your VM instance. Scroll down to your boot disk and click "x" next to the boot volume, then click create button. In the next window give your new volume a meaningful name, set up your snapshot schedule, and then under "source type" select "Snapshot". Choose your snapshot from the dropdown. If you have a customer managed/supplied key (not recommended) select it, otherwise leave it as google-managed key. Click create. Depending on disk size it might take a while. Be patient and let it finish the setup and then start your instance once it is al complete.

How do I automatically restart a GCE preemptible instance?

How do I automatically restart a preemptible Google Compute Engine instance? I only have one instance that doesn't need 100% uptime but that I would like to restart once the data center becomes unloaded again. The instance/server that I'm trying to automatically restart has its own boot disk that I'd like to use each time it restarts.
You could try using Instance Group Manager to set up a pool of size 1. It will then try to re-create instances after they are preempted.
You should be aware that there is no guarantee that there is going to be capacity for your instance. As the docs say:
Preemptible instances are available from a finite amount of Compute Engine resources, and might not always be available.
You could create a f1-micro instance which is free for one instance per month in several data centers and create a cron job
*/10 * * * * /snap/bin/gcloud beta compute instances start --zone "yourzone" "yourinstance" --project "yourproject"
after you ran gcloud auth login once.
This will restart your instance every 10 minutes. Of course you can set this also to an hour or more. With a bit more scripting also things like exponential back off can be done.
If you'd like to restart it less frequently, you can use Instance schedules that's built in to the Google Cloud Dashboard.
https://cloud.google.com/compute/docs/instances/schedule-instance-start-stop

Creat an image from Google Compute Engine instance

I am trying to replicate a new instance from my current instance in Google Compute Engine (following Google instruction: https://cloud.google.com/compute/docs/images#create_an_image_from_a_root_persistent_disk).
In step 3 (Terminate your instance), I am following the instruction ($ gcloud compute instances delete example-instance --keep-disks boot). However, it still asks me whether I want to completely delete the instance:
The following instances will be deleted. Attached disks configured to
be auto-deleted will be deleted unless they are attached to any other
instances. Deleting a disk is irreversible and any data on the disk
will be lost. Do you want to continue (Y/n)? n
Am I still able to recover the instance or continue to create the image of my current instance as Google's instruction? If any body has try to replicate Google Compute Engine, please let me know. Thank you!
I just tested this command on my account and deleted my instance using " --keep-disks boot ". This option keeps the boot disk despite the warning message which says "Attached disks configured to be auto-deleted will be deleted....."
You can also use Developers Console, uncheck "Delete boot disk when instance is deleted" under your instance properties and then delete the instance from there.

Google Compute Engine recover from terminated status

My free trial period has been cancelled but I would like to continue using my existing engines.
The console says my instance is terminated and is no longer running.
Rebooting the instance gives "The resource .... is not ready' error.
How can I continue using my engines with exact same IP setting and other configurations?
Once an instance is in a 'TERMINATED' state it can no longer be booted. You will need to recreate an instance with the same configuration, IP address and boot disk as you indicate. See this FAQ for more information about the terminated state: https://cloud.google.com/compute/docs/troubleshooting#terminate
To retain your existing IP address you will need to promote it to a static IP address resource. You can then reassign this address resource to your new instance.
$ gcloud compute addresses create address-name --addresses IP_ADDRESS --region REGION
See this article for the exact steps:
https://cloud.google.com/compute/docs/instances-and-network#promote_ephemeral_ip
To migrate the existing data on your disk you could create a snapshot and then restore that snapshot when creating a new instance:
$ gcloud compute disks snapshot DISK
See this article for the detailed steps:
https://cloud.google.com/compute/docs/disks#creating_snapshots
Finally to migrate all the associated configuration and metadata you could use the describe subcommand in the Cloud SDK:
$ gcloud compute instances describe INSTANCE
This would print out the entire configuration for your existing instance which you can then use to recreate in your new instance.
The exact steps are quite similar to the process of migrating an instance from one zone to another. You could essentially follow the guide for that process but recreate your new instance in the same zone if you prefer not to move the location of your data. The steps for migrating an instance across zones can be found here:
https://cloud.google.com/compute/docs/instances#moving_an_instance_between_zones

How to change a machine type on Google Compute Engine?

On Google Compute Engine, is there a way to change the machine type (for example, add cpu cores) after the machine was created?
It is now possible in the Google compute engine (You can refer to this document).
You just need to stop the instance. And you can then edit the instance type and restart.
This seems to be possible in gcloud:
https://cloud.google.com/sdk/gcloud/reference/compute/instances/set-machine-type
gcloud compute instances set-machine-type
allows you to change the machine type of a virtual machine in the TERMINATED state (that is, a virtual machine instance that has been stopped).
For example, if example-instance is a g1-small virtual machine currently in the TERMINATED state, running:
$ gcloud compute instances set-machine-type example-instance \
--zone us-central1-b --machine-type n1-standard-4
will change the machine type to n1-standard-4, so that when you next start example-instance, it will be provisioned as an n1-standard-4 instead of a g1-small.
UPDATE: this answer is no longer true, as the ability to change instance type was added after this answer was written. See accepted answer.
Although there is no direct "edit machine type" option on GCE, the way to achieve that is:
Deleting the old instance (while making sure the disk is not deleted).
Creating a new instance with the desired type and using the disk from the old instance (instead of creating a new one)
Use gcloud compute instances set-machine-type to change a stopped instance to a machine of another type, for example:
$ gcloud compute instances list
NAME ZONE MACHINE_TYPE PREEMPTIBLE INTERNAL_IP EXTERNAL_IP STATUS
foobaz us-central1-a f1-micro 10.128.0.2 104.197.19.103 RUNNING
$ gcloud compute instances stop foobaz
$ gcloud compute instances set-machine-type foobaz --machine-type g1-small
$ gcloud compute instances start foobaz
$ gcloud compute instances list
NAME ZONE MACHINE_TYPE PREEMPTIBLE INTERNAL_IP EXTERNAL_IP STATUS
foobaz us-central1-a g1-small 10.128.0.2 104.197.179.223 RUNNING
This assumes you've already set your default zone, e.g.:
$ gcloud config set compute/zone us-central1-a
Also, note the EXTERNAL_IP has changed in the example above. If you want to the newly resized machine to keep the original IP address, then before you stop it you should promote the external IP address from ephemeral to static:
$ ipaddr=$(gcloud --format="value(networkInterfaces[0].accessConfigs[0].natIP)" compute instances describe foobaz)
$ gcloud compute addresses create foobaz-ip --addresses $ipaddr
The Google Cloud documentation states that you can do this from the page that lists the VM Instances however it doesn't appear to be that way now. I found that you have to click on the image name in that list. That then brings up a page where you can Edit the instance including the type.
To change the machine type of your VM instance. You need first to stop your VM instance. After that, click edit then change the machine type and then save it.