Google Compute Engine : Use snapshot from another project?

一曲冷凌霜 提交于 2019-12-29 18:37:28

问题


I have two projects in my developer console. I have taken a "Snapshot" of one of the VMs in project-1. I want to create a new VM in project-2 using the snapshot created in project-1. Right now snapshot is not showing in the option. How can I import snapshot from one project to another?


回答1:


You can create an image from the snap in Project 1, then create an instance from that image using Project 2.

I'm assuming you have edit rights in both projects.

Your question says you have a snapshot and want to make an instance in project 2 from the snap in project 1.

If you still have the disk available that you had snapshotted, make sure it's no longer attached to an instance. If it's still attached to the instance, uncheck "delete boot disk when deleting instance" and delete the instance. Go to Images and click create image from disk, and create an image from this disk.

If you do not have the disk available, but just the snapshot, create an instance and set the boot disk as a snapshot and select your snapshot. Then follow the directions above to create an image by deleting the instance first.

Now you have an image in project 1. You should see it listed under images.

I'm not sure why, but you won't see the image listed in the console in project 2, however you can use gcloud to create an instance in project 2 using the image from project 1. In project 1, click on the image and click "view REST" there will be a full URL to the image, similar to this:

https://www.googleapis.com/compute/v1/projects/cpomeroy-whitelist/global/images/ruby-image

Use gcloud to create an instance in project 2 using the image in project 1:

gcloud config set project <project-id-of-project-2>
gcloud config list

(You should verify you are in project 2)

gcloud compute instances create <name of instance> --image https://www.googleapis.com/compute/v1/projects/cpomeroy-whitelist/global/images/ruby-image

Obviously your URL will be different.

I just tested this and it works. Let me know if you need more help.




回答2:


The answer posted by @chrispomeroy worked for me, but I was able to simplify it a little as I need to do this more and more.

Let's say you have an image in project-1, and need to create an instance using that image in project-2.

gcloud config set project "project-2"
gcloud compute instances create <name-of-new-instance> \
    --image <name-of-your-image-from-project-1> \
    --image-project "project-1"

This eliminates the need to worry about using a URL for anything.

EDIT: My answer pretty much looks like his at this point, but the docs for this stuff is here:

gcloud compute instances create




回答3:


You don't need an image or a scratch VM, and you don't have to interrupt the source VM. Just create a snapshot in the source project:

$ gcloud compute --project p1 disks snapshot the-snapshot src-disk --snapshot-names=the-snapshot
Created [https://www.googleapis.com/compute/v1/projects/p1/global/snapshots/the-snapshot].

Then create a disk in the destination project with --source-snapshot pointing at the 'Created' URL returned above:

$ gcloud compute --project p2 disks create the-disk \
    --source-snapshot https://www.googleapis.com/compute/v1/projects/p1/global/snapshots/the-snapshot

This usage was not shown in the gcloud docs, I found it in @krishna praveen's answer, but his explanation is incorrect; you do not need to delete any instances, or use images. And this works even if both source and destination are boot disks:

$ gcloud compute --project p2 instances create the-vm --disk name=the-disk,boot=yes

If for some reason you require an image, you can still restore a snapshot to a disk and use this to create the image without a scratch VM. This is preferable if a scratch VM would automatically start services on boot which could interfere with other running VMs on the same project network.

$ gcloud compute images create image-1 --source-disk=src-disk-image --source-disk-zone=zone1

This image can now be used from another project (as shown by @jiminikiz above).

$ gcloud compute --project p2 instances create <name-of-new-instance> --image image-1 \
    --image-project p1 --zone=zone



回答4:


The solution provided by "chrispomeroy" works fine but require to init gcloud with your personal google user account (instead of the project2 service account) first (since it is the one who has permission to access to both project):

gcloud init (and chose [2] Login with new credentials)

Then you can indeed create the VM on project 2 (from a base image on project 1) with :

gcloud compute instances create testimg --image --image-project (no need for URL) I tested today (nov 2015) and works fine




回答5:


This is click only solution through the browser. What you need? You need to have image. To create image from disk, the disk must be detached from any instance.

What are the steps if you have just instance in Project1:

  • Create snapshot from the instance in Project1.

  • Create instance from this snapshot in Project1. Untick "Delete boot disk when instance is deleted". This instance it's used only for
    now and gonna be deleted

  • Delete the instance that you just created

  • Go to the "Disks" menu and you must see there the disk from the instance.

  • Go to "Images" menu -> "Create an image". Here you can create image. If you don't have detached disk you won't have any disk available in the dropdown.

  • Go to Project 2 and create instance using the custom image that you created for Project 1. How? Boot disk -> change -> Custom images-> Select Project 1-> Here you can see your custom image




回答6:


AFAIK, it is not possible. To accomplish what you have described, the best course of action is to use this tutorial. You have a few steps for creating a blank disk, attaching it to the machine in question, tarball the boot partition and upload it to cloud storage. Once that is done, download it locally, switch projects and upload to the other project. You will then be able to just select the machine from the list of Images when clicking on New Instance




回答7:


Instances can be created across the project using images. Now, if you have an image in Project 1, you can select that in Project 2 as well.

But, as of today, you can't see the disks across the project. If you have to migrate an VM with associated additional disks from Project 1 to Project 2, follow the below.

  1. Use the snapshot and create the disk in Project 2 by connecting the gcloud command interface. Connect to Project 2, and then execute:

    gcloud compute --project "GCPProject2" disks create "myserver-disk1" --size "50" --zone "us-east1-b" --source-snapshot https://www.googleapis.com/compute/v1/projects/GCPProject1/global/snapshots/snapshot-myserver-disk1 --description "DriveName" --type "pd-standard"
    
  2. Above will create the disk in Project2. Then you need to delete the instance in Project1 by retaining the boot disk. Then, create the image from the disk myserver-bootdisk. Once the image is created, switch over to Project 2 and then create the server from image, use the dropdown and select the image from Project 1.

  3. Add the additional disk which you have crated from the snapshot and create the VM.



来源:https://stackoverflow.com/questions/29585381/google-compute-engine-use-snapshot-from-another-project

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!