Fully Migrate GCP Instance to Another Project or Account

Introduction

Compute Engine is definitely the most popular service in Google Cloud. Actually, it is nothing but just a virtual machine on cloud. It exists in all cloud platform with different name. You could easily build an instance within one minute. However, when it comes to the instance migration issue, it is not that trivial. Thus, this article will let you have hands-on experience to migrate an instance on GCP to another project or account.

Solution Diagram

The idea is very simple. We just have to create an image from our existing instance, then zip and put it in Cloud Storage. After that we set the permission in Cloud Storage such that others could get the image. Finally, we just create an instance using the custom image.

Setup

In console, we choose Cloud Shell and then click the editor mode such that we could easily review our code.

The we have to setup our environment e.g. define project, zone and region:

gcloud config set project <you-project-id>
gcloud config set compute/zone us-central1-a
gcloud config set compute/region us-central1

Please download my prepared file from github or

git clone https://github.com/manbobo2002/gcp-migrate-instance.git
cd ~/gcp-migrate-instance

Exporting a custom image to Cloud Storage

In fact, all the commands are reference to the GCP document. However, I slightly modify it and combine them into one script. It makes much more easy to use. The flow is simply as below:

  1. Create a snapshot of the disk
  2. Create a temporary disk named temporary-disk to hold your tar file
  3. Create an instance and enable storage-rw scope on the instance. Attach the image-disk and the temporary-disk to the instance as secondary disks.
  4. SSH to the instance.
  5. Format and mount the temporary disk.
  6. Create a tar file of your image.
  7. Upload the image into Cloud Storage.

It looks complicated but don’t worry because I create a script to do the above things.

Before we run our script, we just have to rename the above parameters if necessary.

sh shartup.sh

After running the script, you will find an instance on Compute Engine is created. Then we SSH into the instance.

Copy the whole script of instance-script.sh, or simply copy the below script and paste it into the instance:

sudo screen -S image-transfer
image_disk="wp-image-disk"
bucket="custom-image-storage-20200610"

cat <<EOF >> script.sh
sudo mkdir /mnt/tmp
sudo mkfs.ext4 -F /dev/disk/by-id/google-temporary-disk
sudo mount -o discard,defaults /dev/disk/by-id/google-temporary-disk /mnt/tmp

sudo mkdir /mnt/${image_disk}
ls /dev/disk/by-id/

sudo mount /dev/disk/by-id/"google-${image_disk}-part1" /mnt/${image_disk}
sudo umount /mnt/${image_disk}/
sudo dd if=/dev/disk/by-id/google-${image_disk} of=/mnt/tmp/disk.raw bs=4096

cd /mnt/tmp
sudo tar czvf myimage.tar.gz disk.raw
/mnt/tmp/myimage.tar.gz

gsutil mb gs://${bucket}
gsutil cp /mnt/tmp/myimage.tar.gz gs://${bucket}

exit
EOF
sh script.sh

Then we have to wait for the script until it is finished. It may take few minutes to few hours depending your disk size. Since I am using “screen” so you could close the windows and leave it running. When we want to see the progress, we just have to type:

sudo screen -r

After the script is done, we could find that a zipped image is created on Cloud Storage.

Now we have to set the permission to somewhere we want to access. Click to the bucket name.

First of all, we click “Overview” and find the link for gsutil, please copy it on somewhere, we will use it later.

Click Permissions, and then click “Add members”.

Type the new account we want to give the permission, and set the Storage Admin or Storage Viewer role for them. Since this article is just for demostration, I will use the same account with different project.

Import a Custom Image Into Compute Engine

Now we have stored our customer image. Then the next step is to import it into Compute Engine. Back to the Cloud Shell and change to the correct project name. If you are using different account, please download my prepared file from github or

git clone https://github.com/manbobo2002/gcp-migrate-instance.git
cd ~/gcp-migrate-instance

In restore.sh, please edit the parameters if you need. Also, please paste the url we marked down before to replace my ISO_URL.

Then we execute the restore.sh file:

sh restore.sh

Again, this process will take few minutes, but should be faster than before.

After the restore, we could find the created instance in Compute Engine.

It is exactly same as the target instance.

Cleanup

Same as previous articles, we will need to clean up all the unnecessary things. In Cloud Shell, we back to the original project.

In cleanup script, please make sure the name is the same as you created before. Then run the script by typing:

sh cleanup.sh

It is up to you to delete the bucket and the new created instance, in this script we don’t do it.

Enjoy this tutorial!

Leave a Reply