GCP - moving VMs across projects with the Google Cloud SDK - google-cloud-platform

I want to move some VMs across projects on GCP using the Cloud SDK.
I'd like the process on a high level, and then possibly also some links out to the relevant docs, although I can RTFM when I know what the general high-level steps are.
I think what I want to do is
Create a snapshot
Save it somewhere
Create two Cloud SDK contexts?
Prepare a destination in the context for the new project
Copy the snapshots over to the new context and its associated storage
Rehydrate from the copied snapshot in the new project once everything is copied.
Please help. I'm new with this stuff and want to know whether this is really how it should be done.
Thanks!

You can use this guide
Quick summary of steps needed
Detach the boot disk from the VM that you intend to move by deselecting “Delete boot disk on instance delete” and terminating the VM
Create an image from the detached boot disk
Upload the image to Google Cloud Storage and share it with the new project
Create a custom image under the new project based on the image you uploaded to Google Cloud Storage
Create a new VM instance under the new project based on the custom image

Related

Making Exact Copy of Server Google Cloud

I have recently started using Google Cloud over Digital Ocean because they are now offering a free trial. Although I am having an issue with figuring out how to make an exact copy of a server.
On Digital ocean, I can create an exact copy of a server as a new server, this will copy over all installd files and configurations to the new server. But all I have been able to find on GCP is a way to create a similar instance, but this only copies the settings of the ubuntu image. Is there a way to make an exact copy of a server on Google Cloud Platform? Thank you in advance!
You have two options:
a. Using persistent disk snapshots
b. Using machine image(Beta)
The same point of those methods is create some "Image" from your original server, and create new instance from the "Image".
The difference between (a.) and (b.) is, the scope of backup.
(a.) is only about a disk.
(b.) contains all the configuration, metadata, permissions, and data from one or more disks for a VM instance running on Compute Engine.
Please refer the official documents below.
Reference
Persistent Disk Snapshots
https://cloud.google.com/compute/docs/disks/create-snapshots
Machine Images
https://cloud.google.com/compute/docs/machine-images

Create Custom Image from GCP VM with OS and data disks

I set up centos and windows VMs in Google Cloud which has many custom installations and configuration, with 1 boot disk and 4 data disks.
I am trying to create an image of both VMs. Only options I see in google cloud to create an image from disk, snapshot, another image, cloud file or virtual disk.
Looking for some way to create an image at VM level, which I can reuse to create new VMs something similar to custom ami in AWS or custom image in Azure.
The intention is if I create VM out of that image, my new VM will come up with all 5 disks. On GCP console, when I try to create an image, it has no option of using whole VM as a source instead, I can choose single disk as source.
Is there any way or workaround to achieve this in Google Cloud?
Thanks in advance.
I found GCP Instance Templates as a solution to my problem. I can reuse Instance Templates to deploy my custom VMs.

How to move an instance between two projects in Google Cloud Platform?

In compute engine, How can I move an instance from project A to project B?
I have two projects, and both have the same owner.
I looked at all the interfaces inside the console, but I could not find a way.
This can be done fairly easily now, with the caveat that when you create the VM in the other project it cannot be done through the UI but rather must be done using the gcloud tool. And google even has a page to document how.
First, you need to either create an image or a snapshot of the disk used in the VM. You can do this through the Console UI or the gcloud utility. Google's documentation does a good job of explaining how to do it, but the TLDR is:
stop VM if possible, or reduce number of writes by shutting down services if not
go to Compute Engine -> Images
select create
choose the disk as source
set any other properties you need
press create
Once that has been completed, use the gcloud tool with the other project to create your new VM. To find out/verify the name of the disk image:
gcloud compute images list --project [IMAGE_PROJECT]
then create the vm (add any additional options you need):
gcloud compute instances create [INSTANCE_NAME] --image [IMAGE_NAME] --image-project [IMAGE_PROJECT]
There isn't any tool in GCP that allows migrating one Compute Engine instance from one project to another.
However, it is still possible to recreate one instance from one project to another, by creating a snapshot of the disk, creating a custom image, and create a new VM from it in the second project.
This article gives a nice step by step guide on how to do it.
There is a newer doc page to copy VM between projects

Which method is best to create Google Cloud VM from existing VM?

Cloning VM from console?
I tried this solution but changes inside VM before cloning are not available in new VM
Create snapshot from bootdisk and then use snapshot to create VM.
Use existing boot disk when creating VM from select boot disk popup.
Create new Image form snapshot and use that Image to create the VM.
Since you asked the best, it will be #4 (Create new Image form snapshot and use that Image to create the VM.). This will give you an option to refer to the image by family name while doing the automation. Also it will help you version the image and deprecate the older image etc.

How to export Google Cloud Computer Instance Image?

How to export google compute engine instance image ?. I want to reuse the instance in another google cloud account. Is that possible?
Here's the documentation for exporting an image, and here for importing an image.
You can also share images across projects by granting the “compute.imageUser” IAM role. As explained in this article:
“For example, assume that a User A owns Project A and wants to create VM instances using images owned by Project B. The owner of Project B must grant User A the compute.imageUser role on Project B. This grants User A the ability to use the images from Project B to create instances in Project A.”
Yes it is possible.
First of all, Instance and Image are two different things. Image is more like an install-able package and Instance is the actual installed version.
What might be confusing here, w.r.t. Google Cloud is that Google refers to creating an instance from image as Exporting an Image which is contrary to VMWare or other hypervisor based virtualization terminology where creating an instance is done by Importing a VM (template) and Exporting a VM is basically reverse of installation i.e. creating a re-loadable package from installed version of VM.
This process of creating an Image from VM Instance is called Creating an Image in Google Cloud and the process for same is documented here