Google Compute Engine : Use snapshot from another project?

前端 未结 7 1423
别那么骄傲
别那么骄傲 2020-12-13 10:07

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

7条回答
  •  渐次进展
    2020-12-13 10:43

    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  --image image-1 \
        --image-project p1 --zone=zone
    

提交回复
热议问题