简体   繁体   English

如何在 Compute Engine 中将 Persistence Disk 从一个项目迁移到另一个项目?

[英]How I can migrate a Persistence Disk from one project to another, in Compute Engine?

I would like to know how I can migrate a Persistence Disk (Google Compute Engine) from one project to another?我想知道如何将 Persistence Disk (Google Compute Engine) 从一个项目迁移到另一个项目? If it's posible.如果可能的话。

If you want to copy (non-root) persistent disk, do the steps below:如果要复制(非根)永久性磁盘,请执行以下步骤:

  1. Go to Google Developer Console and detach <disk-name> disk from the machine.转到Google Developer Console并从机器上分离<disk-name>磁盘。
  2. Use gcloud command-line utility to switch to the old project:使用gcloud命令行实用程序切换到旧项目:
    • gcloud config set project <old-project>
  3. Create an image of the <disk-name> disk:创建<disk-name>磁盘的映像:
    • gcloud compute images create <image-name> --source-disk=<disk-name> --source-disk-zone=<zone>
  4. The output of the above command will give you a fully qualified link to the image (in the form https://www.googleapis.com/compute/v1/projects/<project-name>/global/images/<image-name> ).上述命令的输出将为您提供一个完全限定的图像链接(格式为https://www.googleapis.com/compute/v1/projects/<project-name>/global/images/<image-name> )。 Alternatively, run: gcloud compute images list --no-standard-images --uri | grep <image-name>或者,运行: gcloud compute images list --no-standard-images --uri | grep <image-name> gcloud compute images list --no-standard-images --uri | grep <image-name> to find the link. gcloud compute images list --no-standard-images --uri | grep <image-name>查找链接。
  5. Switch to the new project:切换到新项目:
    • gcloud config set project <new-project>
  6. Create a new disk from the image:从映像创建一个新磁盘:
    • gcloud compute disks create <new-disk-name> --image=<link-to-the-image> --zone=<zone>

As above Mike Lutz's answer, except gcutil is now deprecated , but the 2nd command can also be done with gcloud compute instance are:正如上面 Mike Lutz 的回答,除了gcutil现在已弃用,但第二个命令也可以使用gcloud compute instance完成:

1) create your image from you PD (NB! read first! https://cloud.google.com/compute/docs/images#creating_an_image_from_a_root_persistent_disk ) 1)从你的PD创建你的图像(注意!先阅读! https://cloud.google.com/compute/docs/images#creating_an_image_from_a_root_persistent_disk

$ gcloud compute images create [example-image] --source-disk [example-disk] --source-disk-zone ZONE --project="old-project-name"

2) Instantiate the image in the new project (goes without saying but you must have access to both projects) 2)在新项目中实例化图像(不用说,但您必须有权访问两个项目)

$ gcloud compute instances create [example-instance-1] --project=[new-project-name] --image="https://www.googleapis.com/compute/v1/projects/[old-project-name]/global/images/[image-name]" --boot-disk-size [XXXGB] --machine-type=[machine-type] --network="default" --zone=[datacenter-zone] 
  • you can see the URL of your image in the Images tab under "Equivalent REST"您可以在“等效 REST”下的“图像”选项卡中看到图像的 URL

For additional instance config options see: https://cloud.google.com/sdk/gcloud/reference/compute/instances/create有关其他实例配置选项,请参阅: https : //cloud.google.com/sdk/gcloud/reference/compute/instances/create

In case anyone else lands here from a google search, today you can create a disk in one project directly from a snapshot in another project, without creating any temporary physical disks or images.如果其他人通过谷歌搜索登陆这里,今天您可以直接从另一个项目中的快照创建一个项目中的磁盘,而无需创建任何临时物理磁盘或映像。

  1. Create snapshot: gcloud compute disks snapshot your-disk-name --snapshot-names=your-snapshot-name --project=source-project --zone=source-zone创建快照: gcloud compute disks snapshot your-disk-name --snapshot-names=your-snapshot-name --project=source-project --zone=source-zone

  2. Describe the snapshot and find the selfLink: gcloud compute snapshots describe your-snapshot-name --project=source-project描述快照并找到selfLink: gcloud compute snapshots describe your-snapshot-name --project=source-project

  3. Use the selfLink instead of a snapshot name when creating a disk in the new project: gcloud compute disks create your-new-disk-name --project=destination-project --zone=destination-zone --source-snapshot=https://www.googleapis.com/compute/v1/projects/source-project/global/snapshots/your-snapshot-name在新项目中创建磁盘时使用 selfLink 而不是快照名称: gcloud compute disks create your-new-disk-name --project=destination-project --zone=destination-zone --source-snapshot=https://www.googleapis.com/compute/v1/projects/source-project/global/snapshots/your-snapshot-name

There is no easy way to do this on GCE, the only way is to go through the process of creating a custom image, exporting it to a cloud storage bucket (that both projects have permissions on), and then creating a new instance using the new custom image in the new project.在 GCE 上没有简单的方法可以做到这一点,唯一的方法是完成创建自定义映像的过程,将其导出到云存储桶(两个项目都有权限),然后使用新项目中的新自定义图像。

Follow these instructions: https://developers.google.com/compute/docs/images#installinganimage请按照以下说明操作: https : //developers.google.com/compute/docs/images#installinganimage

If you can create an image out of it.如果您可以从中创建图像。 Then you can do something like this and it will copy the image to a disk in you new project:然后你可以做这样的事情,它会将图像复制到新项目中的磁盘:

{gcloud compute instances create my-instance-1 --project=[new-project-id] --image="https://www.googleapis.com/compute/v1/projects/[old-project-id]/global/images/[image-name]"} {gcloud 计算实例创建 my-instance-1 --project=[new-project-id] --image="https://www.googleapis.com/compute/v1/projects/[old-project-id]/全局/图像/[图像名称]"}

Adding on Amos' answer above, in order to avoid having to detach a pd from its instance in order to make an image out of it, I used the following sequence:添加上面 Amos 的回答,为了避免必须从其实例中分离 pd 以便从中制作图像,我使用了以下序列:

In the source project:在源项目中:

  1. Create a SNAPSHOT from the desired boot disk to be cloned.从要克隆的所需启动磁盘创建快照。
  2. Create a new persistent disk from that snapshot.从该快照创建一个新的永久性磁盘。
  3. Create an image from the new pd.从新的 pd 创建一个图像。

In the target project:在目标项目中:

  1. Create a VM instance using the url of the newly created image.使用新创建的镜像的 url 创建一个 VM 实例。

When you're done you can delete the created disk and image in the source project.完成后,您可以删除源项目中创建的磁盘和映像。

While the following won't directly migrate a Persistence Disk, it will let you make a copy of one on a different project with only two commands.虽然以下不会直接迁移 Persistence Disk,但它可以让您只用两个命令在不同的项目上制作一个副本。

If you combine process in the document IanGSY references for turning the disk into an "image" IE:如果您在 IanGSY 参考文档中结合将磁盘转换为“图像”IE 的过程:

gcloud compute images create example-image --source-disk example-disk --source-disk-zone ZONE

(above command only works if the drive isn't live (attached), if you have to drop the VM make sure you don't let it delete the drive - make sure "Delete boot disk when instance is deleted" isn't checked if you use the web UI!) (以上命令仅在驱动器不可用(附加)时才有效,如果您必须删除 VM,请确保不要让它删除驱动器 - 确保选中“删除实例时删除启动磁盘”如果您使用网络用户界面!)

And use what is hinted at by Jens, (which is spelled out in the answer to SO: How to share google compute engine images across projects? ) IE:并使用 Jens 所暗示的内容(在 SO: How to share google compute engine Images 的回答中有详细说明 )IE:

gcutil --service_version="v1" --project=<ID-of-your-project> addinstance <name-you-for-instance> --machine_type=<type-of-machine> --network="default" --external_ip_address="ephemeral" --zone=<datacenter-zone> --image="https://www.googleapis.com/compute/v1/projects/<ID-of-image-owners-project>/global/images/<image-id>" --persistent_boot_disk="true"

You will end up with a copy of the given disk on the other project.您最终会在另一个项目上获得给定磁盘的副本。

I had an awful time getting data on a disk from one of my projects to another.我在将磁盘上的数据从我的一个项目转移到另一个项目时度过了一段糟糕的时光。 Tried my best to follow all the directions here and from Google but couldn't get a brand new Disk created without creating an Instance first.尽我最大的努力遵循此处和 Google 的所有说明,但无法在不先创建实例的情况下创建全新的磁盘。 Here was what I ended up doing... (highly influenced from many other answers)这是我最终做的……(受许多其他答案的影响很大)

In original project:在原始项目中:

  • Go to IAM and look for your email address.转到 IAM 并查找您的电子邮件地址。 In Role(s) add the role: Compute Engine => Compute Image User (this assumes you're the owner of both projects. I'm also not 100% positive this was a necessary step in the end)在 Role(s) 添加角色:Compute Engine => Compute Image User(这假设您是两个项目的所有者。我也不是 100% 肯定这是最后的必要步骤)
  • Create a new Image.创建一个新图像。 Set the "Source" to be "Disk".将“源”设置为“磁盘”。 Then select your disk.然后选择您的磁盘。

In destination project:在目标项目中:

  • Open Google Cloud Shell (button on top right of GCE console)打开 Google Cloud Shell(GCE 控制台右上角的按钮)
  • Run this command to create a new INSTANCE from the disk: gcloud compute instances create NAMEFORNEWINSTANCE --image IMAGENAMEYOUCHOSE --image-project ORIGINALPROJECTNAME (Yes, we're creating a new instance even though you really only want to create a new disk. I couldn't get it to work just creating a new disk.)运行以下命令从磁盘创建一个新实例: gcloud compute instances create NAMEFORNEWINSTANCE --image IMAGENAMEYOUCHOSE --image-project ORIGINALPROJECTNAME (是的,我们正在创建一个新实例,即使您真的只想创建一个新磁盘。我仅创建一个新磁盘就无法使其工作。)
  • Find the Instance that was just created.找到刚刚创建的实例。 Stop it.停下来。 Edit it.编辑它。 Uncheck the box for "Delete boot disk when instance is deleted".取消选中“删除实例时删除启动磁盘”框。 Save Changes.保存更改。 Delete the Instance.删除实例。 Don't check the box to delete the boot disk.不要选中删除启动盘的复选框。
  • Go to the Disks panel.转到磁盘面板。 You should see a disk named the same as whatever you picked for NAMEFORNEWINSTANCE您应该会看到一个与您为NAMEFORNEWINSTANCE选择的名称相同的磁盘

Attach Disk: (not exactly the same topic but putting the steps here for anyone who needs to know how to do this)附加磁盘:(不完全相同的主题,但将步骤放在这里,供需要知道如何执行此操作的任何人使用)

  • In Google Cloud Shell run: gcloud compute instances attach-disk NAMEOFINSTANCETOATTACHTO --disk=NAMEOFDISK (Again, NAMEOFDISK should be the same as what you picked for NAMEFORNEWINSTANCE) You should see in the Disks panel that it's now attached to your new instance.在 Google Cloud Shell 中运行: gcloud compute instances attach-disk NAMEOFINSTANCETOATTACHTO --disk=NAMEOFDISK (同样,NAMEOFDISK 应该与您为 NAMEFORNEWINSTANCE 选择的相同)您应该在“磁盘”面板中看到它现在已附加到您的新实例。
  • SSH into Instance you attached to通过 SSH 连接到您附加到的实例
  • Create a new directory to mount this disk to: sudo mkdir /DIRECTORYNAME创建一个新目录以将此磁盘安装到: sudo mkdir /DIRECTORYNAME
  • Run lsblk to see a list of disks, their names, sizes, and mountpoints.运行lsblk以查看磁盘列表、它们的名称、大小和挂载点。 Try to find your attached disk in there.尝试在那里找到您的附加磁盘。 The size should match what you see in the Disks panel and should not be mounted to anything大小应与您在“磁盘”面板中看到的相符,不应安装到任何内容
  • Mount the disk to your directory using: sudo mount /dev/NAME_FOUND_IN_STEP_ABOVE /DIRECTORY使用以下sudo mount /dev/NAME_FOUND_IN_STEP_ABOVE /DIRECTORY磁盘挂载到您的目录: sudo mount /dev/NAME_FOUND_IN_STEP_ABOVE /DIRECTORY

Don't use the process of creating an image file in a Cloud Storage bucket and sharing it.不要使用在 Cloud Storage 存储分区中创建图像文件并共享它的过程。 It is not very robust (yet).它不是很健壮(还)。 Amos' method of creating an image in the source project and creating an instance from that image in the destination project is much cleaner and worked great for me. Amos 在源项目中创建图像并在目标项目中从该图像创建实例的方法更清晰,对我来说效果很好。

Do pay some attention to the command line options, as the gcloud utility is in a bit of flux.请注意命令行选项,因为 gcloud 实用程序在不断变化。

If the data is not too big (eg under 1 TB(?)), it might be easier to just copy it directly using two VMs, each with the attached disk:如果数据不是太大(例如低于 1 TB(?)),使用两个虚拟机直接复制它可能会更容易,每个虚拟机都有附加的磁盘:

  • On target VM, generate a temporary ssh key: ssh-keygen -t rsa在目标 VM 上,生成临时 ssh 密钥: ssh-keygen -t rsa
  • On source VM, use console to add content of the generated public key (from target's ~/.ssh/id_rsa.pub ).在源虚拟机上,使用控制台添加生成的公钥的内容(来自目标的~/.ssh/id_rsa.pub )。 You can also just add it as a new line to target's ~/.ssh/authorized_keys )您也可以将其作为新行添加到目标的~/.ssh/authorized_keys
  • Use rsync or scp to copy the data.使用 rsync 或 scp 复制数据。

  • If your data requires sudo, you would need an extra step - enable sudo rsync :如果您的数据需要 sudo,则需要一个额外的步骤 - 启用sudo rsync

    • On source, use sudo visudo and add username ALL=NOPASSWD:/usr/bin/rsync ( change username, and use which rsync just in case it has a different path)在源代码上,使用sudo visudo并添加username ALL=NOPASSWD:/usr/bin/rsync (更改用户名,并使用which rsync以防万一它有不同的路径)
    • Use rsync -av --rsync-path="sudo rsync" -e='ssh -o "UserKnownHostsFile /dev/null" -i ~username/.ssh/id_rsa' username@source.ip.address:/source/path/ /target/path (update username twice and use proper source.ip.address)使用rsync -av --rsync-path="sudo rsync" -e='ssh -o "UserKnownHostsFile /dev/null" -i ~username/.ssh/id_rsa' username@source.ip.address:/source/path/ /target/path (更新用户名两次并使用正确的source.ip.address)

Using the GUI使用图形用户界面

Inside project1:内部项目1:

1) grant access images by going to https://console.cloud.google.com/iam-admin/iam and click Add then enter the email associated with project2, and select role as Compute Engine > Compute Image User (might also need Compute Storage Admin role) 1) 访问https://console.cloud.google.com/iam-admin/iam并单击添加,然后输入与 project2 关联的电子邮件,然后选择角色作为 Compute Engine > Compute Image User(可能还需要计算存储管理员角色)

2) create an image from disk https://console.cloud.google.com/compute/imagesAdd 2)从磁盘创建图像https://console.cloud.google.com/compute/imagesAdd

Inside project2:项目2内部:

3) create a new instance and under 'boot disk' click change https://console.cloud.google.com/compute/instancesAdd 3)创建一个新实例,然后在“启动磁盘”下单击更改https://console.cloud.google.com/compute/instancesAdd

4) select the 'custom images' tab and under 'select images from' select project1 4)选择“自定义图像”选项卡,然后在“从中选择图像”下选择项目1

5) select the shared disk name listed 5)选择列出的共享磁盘名称

6) continue with the new instance setup 6) 继续新的实例设置

There is the official guide that only requires 3 steps to achieve the goal as snapshots can be accessed cross projects: https://cloud.google.com/compute/docs/disks/create-snapshots#sharing_snapshots有官方指南只需要 3 个步骤即可实现目标,因为可以跨项目访问快照: https : //cloud.google.com/compute/docs/disks/create-snapshots#sharing_snapshots

gcloud compute disks snapshot disk-1 --project project-a --snapshot-names snapshot-1
gcloud compute disks create disk-2  --project project-b --source-snapshot projects/project-a/global/snapshots/snapshot-1
gcloud compute instances attach-disk instance-1 --project project-b --disk disk-2

Adding my answer because everything seems to be dated.添加我的答案,因为一切似乎都过时了。 GCP allows images to be moved between projects, and the entire solution revolves around this. GCP 允许在项目之间移动图像,整个解决方案都围绕此展开。

I have 2 GCP projects A and B. The persistent disk in A needs to be transferred to B.我有 2 个 GCP 项目 A 和 B。A 中的永久磁盘需要转移到 B。

 1. Create an image from your persistent disk in A.
 2. In B, create a new image and select the source as the image from A.
 3. In B, create a disk from the image (from 2)
 4. (Cleanup) Delete the images in both A and B.

All this can be done via the UI (15 Jan 2021).所有这些都可以通过 UI 完成(2021 年 1 月 15 日)。

Its nearly 2023 and the answer is much simpler.快到 2023 年了,答案要简单得多。 Here's what I did to clone a regional disk from one project into another:以下是我将区域磁盘从一个项目克隆到另一个项目的操作:

DISK_NAME=source-disk
SOURCE_PROJECT=A
SOURCE_REGION=europe-west1
TARGET_PROJECT=B
TARGET_REGION=europe-north1
TARGET_REPLICA_ZONES=europe-north1-a,europe-north1-c
gcloud compute disks create $DISK_NAME \                                                                                                                                                                       
  --region=europe-north1 \
  --source-disk=https://www.googleapis.com/compute/v1/projects/$SOURCE_PROJECT/regions/$SOURCE_REGION/disks/$DISK_NAME \
  --replica-zones=$TARGET_REPLICA_ZONES \
  --project=$TARGET_PROJECT

The steps are similar if you want to clone a zonal disk: https://cloud.google.com/compute/docs/disks/create-disk-from-source#clone如果要克隆区域磁盘,步骤类似: https://cloud.google.com/compute/docs/disks/create-disk-from-source#clone

The method, described by Amos above, uses the command "gcloud compute instances create" to solve the task requested by the OP.上面 Amos 描述的方法使用命令“gcloud compute instances create”来解决 OP 请求的任务。 When he makes use of the "--image" flag, he provides the google url (which is long, and I have to look it up and check it for syntax each time).当他使用“--image”标志时,他提供了 google url(它很长,我每次都必须查找它并检查它的语法)。

The same "gcloud compute instances create" command also takes an "--image-project" flag, in which the task requested by the OP gets solved in the same way, except, it accepts your image and project names so you don't have to remember or lookup the longer uri.相同的“gcloud计算实例创建”命令还带有“--image-project”标志,其中OP请求的任务以相同的方式解决,除了它接受您的图像和项目名称,因此您不会必须记住或查找更长的uri。

The "--image" and "--image-project" flags are also accepted by the command: "gcloud compute disks create", so you can migrate the original project's image of the original project's persistent disk, over to a new persistent disk in a new project, without having to create a running (ie billable) instance. “--image”和“--image-project”标志也被命令“gcloud compute disks create”接受,因此您可以将原始项目永久磁盘的原始项目映像迁移到新的永久磁盘在新项目中,无需创建正在运行(即计费)的实例。

Either method requires that an image of the disk to be migrated is created in the original project before creation of either instance or disk in new project.这两种方法都要求在新项目中创建实例或磁盘之前,在原始项目中创建要迁移的磁盘的映像。

I just migrated a disk from my development project to a production project, and my session looked as follows:我刚刚将一个磁盘从我的开发项目迁移到了一个生产项目,我的会话如下所示:

  1. in my original project, I created an image of the disk I wanted to migrate (first it had to be detached from any running instances) Someone else might have a different disk zone than the one I was using, so modify accordingly:在我的原始项目中,我创建了一个我想要迁移的磁盘的映像(首先它必须与任何正在运行的实例分离)其他人的磁盘区域可能与我使用的磁盘区域不同,因此请相应地修改:

    gcloud config set project originalproject gcloud 配置集项目 originalproject

    gcloud compute images create imageofmydisk --description "image of my disk, ready for deployment" --family "grsec-4.6.5" --source-disk mydisk --source-disk-zone us-central1-f gcloud compute images create imageofmydisk --description "我的磁盘镜像,准备部署" --family "grsec-4.6.5" --source-disk mydisk --source-disk-zone us-central1-f

  2. switching to my new project, I create a new disk, based on my newly created image of my disk in my old project:切换到我的新项目,我根据我在旧项目中新创建的磁盘映像创建一个新磁盘:

    gcloud config set project newproject gcloud 配置集项目 newproject

    gcloud compute disks create mynewdisk --description "description of mynewdisk" --type pd-ssd --image imageofmydisk --image-project originalproject gcloud 计算磁盘创建 mynewdisk --description "description of mynewdisk" --type pd-ssd --image imageofmydisk --image-project originalproject

Thus I accomplish the task requested by OP, without having to memorize, or look up each time, the full url to the image.因此,我完成了 OP 要求的任务,而无需记住或每次查找图像的完整 url。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何增加 Google Compute Engine 中根磁盘的大小? - How can size of the root disk in Google Compute Engine be increased? 如何将 GCS 存储桶从一个项目迁移到不同帐户中的另一个项目 - How to migrate GCS bucket from one project to another in different account 如何增加谷歌计算引擎内存和磁盘 - How to increase Google compute engine RAM and Disk 需要将 Spanner 数据库从一个 GCP 项目迁移到另一个 - Need to migrate Spanner db from one GCP Project to another 有没有办法从 Google Compute Engine 检索磁盘 I/O 延迟时间序列数据? - Is there a way to retrieve disk I/O latency time series data from Google Compute Engine? 如何将文件从谷歌云计算引擎(linux 终端)复制到谷歌云 shell? - How can I copy files from a Google Cloud Compute engine(linux terminal) to google cloud shell? 如何从计算引擎访问谷歌驱动器 - How to access google drive from compute engine 如何将 GCP 项目从一个组织迁移到另一个组织 - How to migrate a GCP Project from one organization to other migrate for compute engine cannot attach.network card to vm migrated from VMware - migrate for compute engine cannot attach network card to vm migrated from VMware 我可以将 AWS elastic IP 从一个区域迁移到另一个区域吗? - Can I migrate AWS elastic IP from a region to another region?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM