Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Google Compute Engine - Clone Instance

I have a GCE instance that I have customised and uploaded various applications to (such as PHP apps running under Apache). I now want to duplicate this instance - i.e. everything on it. I originally thought clone might do this but I had a play around with it and it only seems to clone the instance config and not anything customised on it.

I've been googling it and it looks like what I need to do is create an image and use this image on a new instance or clone? Is that correct? If so, are there any could steps by steps out there to do this? I had a look at the Google page on images and it talks about having to terminate the instance to do this. I'm a bit wary of this. Maybe it's just the language used in the docs, but I don't want to lose my existing instance.

Also, will everything be stored on the image? So, for example, will the following all make it onto the image?

  • MySQL - config & databases schemas & data?
  • Apache - All installed apps under /var/www/html
  • PHP - php.ini, etc...
  • All other server configs/modifications?
like image 440
fatlog Avatar asked Sep 28 '14 10:09

fatlog


People also ask

How do I clone a GCP VM instance?

Click the name of the instance you want to clone. The VM instance details page opens. In the toolbar at the top of the VM instance details page, click Create similar. The GCP Console copies the configuration and opens the Create an instance page.

Can we clone a instance on GCP?

You can only use the Google Cloud console to duplicate a VM's configuration. You cannot duplicate a VM's configuration by using the gcloud CLI or the Compute Engine API.

What is a Google Compute Engine instance?

This page provides an overview of Compute Engine instances. An instance is a virtual machine (VM) hosted on Google's infrastructure. You can create an instance or create a group of managed instances by using the Google Cloud console, the Google Cloud CLI, or the Compute Engine API.


2 Answers

You can create a snapshot of the source instance, then create a new instance selecting the source snapshot as disk. It will replicate the server very fast. For other attached disks, you have to create a new disk and copy file by net (scp, rsync etc)

like image 115
Marco Avatar answered Oct 13 '22 12:10

Marco


In the Web Console, create a snapshot, then click on the snapshot and over CREATE INSTANCE button, you can customize the settings and then click where it says:

Equivalent REST or command line

and copy the command line, this will be your template.

From this, you can create a a BASH script (clone_instance.sh), I did something like this:

#!/bin/bash -e

snapshot="my-snapshot-name"
gcloud_account="[email protected]"

#clone 10 machines
for machine in 01 02 03 04 05 06 07 08 09 10
do 

    gcloud compute --project "myProject" disks create "instance-${machine}" \
        --size "220" --zone "us-east1-d" --source-snapshot "${snapshot}" \
        --type "pd-standard"

    gcloud compute --project "bizqualify" instances create "webscrape-${machine}" \
        --zone "us-east1-d" --machine-type "n1-highmem-4" --network "default" \
        --maintenance-policy "MIGRATE" \
        --service-account "[email protected]" \
        --scopes "https://www.googleapis.com/auth/devstorage.read_only","https://www.googleapis.com/auth/logging.write","https://www.googleapis.com/auth/monitoring.write","https://www.googleapis.com/auth/servicecontrol","https://www.googleapis.com/auth/service.management.readonly","https://www.googleapis.com/auth/trace.append" \
        --tags "http-server","https-server" \
        --disk "name=webscrape-${machine},device-name=webscrape-${machine},mode=rw,boot=yes,auto-delete=yes"

done

Now, in your terminal, you can execute your script

sh clone_instance.sh
like image 28
Javier Giovannini Avatar answered Oct 13 '22 11:10

Javier Giovannini