如需将单租户虚拟机迁移到其他节点或节点组,您可以手动启动实时迁移。您也可以手动启动实时迁移,以将多租户虚拟机迁移到单独租用节点。如需了解如何将虚拟机迁移出单独租用节点,请参阅将虚拟机迁移出单独租用节点。
准备工作
-
如果您尚未设置身份验证,请进行设置。
身份验证用于验证您的身份,以便访问 Google Cloud 服务和 API。如需从本地开发环境运行代码或示例,您可以通过选择以下选项之一向 Compute Engine 进行身份验证:
Select the tab for how you plan to use the samples on this page:
gcloud
-
安装 Google Cloud CLI。 安装完成后,运行以下命令来初始化 Google Cloud CLI:
gcloud init如果您使用的是外部身份提供方 (IdP),则必须先使用联合身份登录 gcloud CLI。
- Set a default region and zone.
REST
如需在本地开发环境中使用本页面上的 REST API 示例,请使用您提供给 gcloud CLI 的凭证。
安装 Google Cloud CLI。 安装完成后,运行以下命令来初始化 Google Cloud CLI:
gcloud init如果您使用的是外部身份提供方 (IdP),则必须先使用联合身份登录 gcloud CLI。
如需了解详情,请参阅 Google Cloud 身份验证文档中的使用 REST 时进行身份验证。
API 速率限制
手动实时迁移请求归类为查询 API 速率限制。
将虚拟机手动实时迁移到单独租用节点或在单独租用节点内手动实时迁移虚拟机
如需将虚拟机手动实时迁移到单独租用节点或在单独租用节点内手动实时迁移虚拟机,请使用 gcloud CLI 或 REST。
如果虚拟机的目标位置是节点组,则 Compute Engine 会选择节点组中同时具有足够的空间来存储虚拟机和最小备用容量的节点。
使用共享单租户节点组时,您无法在项目之间执行实时迁移。
执行此任务所需的权限
您必须拥有以下权限才能执行此任务:
-
针对虚拟机的
compute.instances.update权限。
gcloud
如需将虚拟机手动实时迁移到单独租用节点或在单独租用节点内手动实时迁移虚拟机,请使用以下
gcloud compute instances update命令:gcloud compute instances update VM_NAME \ ( --node=NODE \ --node-group=NODE_GROUP \ --node-affinity-file=NODE_AFFINITY_FILE )替换以下内容:
VM_NAME:要更新其节点亲和性标签的虚拟机的名称。
请准确替换以下各项之一:
NODE:要将虚拟机实时迁移到的节点名称。NODE_GROUP:要将虚拟机实时迁移到的节点组名称。NODE_AFFINITY_FILE:包含可调度此虚拟机的节点配置的 JSON 文件的名称。如需了解详情,请参阅配置节点亲和性标签。
REST
如需将虚拟机手动实时迁移到单独租用节点或在单独租用节点内手动实时迁移虚拟机,请使用以下
instances.update方法:PUT https://compute.googleapis.com/compute/v1/projects/PROJECT_ID/zones/ZONE/instances/VM_NAME { ... "scheduling": { "nodeAffinities": [ { "key": "NODE_AFFINITY_LABEL_KEY", "operator": "IN", "values": [ "[NODE_AFFINITY_LABEL_VALUE]" ] } ] } ... }替换以下内容:
PROJECT_ID:要更新其节点亲和性标签的虚拟机所属项目的 ID。ZONE:要更新其节点亲和性标签的虚拟机的可用区。VM_NAME:要更新其节点亲和性标签的虚拟机的名称。NODE_AFFINITY_LABEL_KEY:以下字符串之一,用于指定将虚拟机实时迁移到节点组还是节点:虚拟机目标位置 要指定的键 节点组 compute.googleapis.com/node-group-name节点 compute.googleapis.com/node-nameNODE_AFFINITY_LABEL_VALUE:要将虚拟机实时迁移到的节点组或节点的名称。
后续步骤
如未另行说明,那么本页面中的内容已根据知识共享署名 4.0 许可获得了许可,并且代码示例已根据 Apache 2.0 许可获得了许可。有关详情,请参阅 Google 开发者网站政策。Java 是 Oracle 和/或其关联公司的注册商标。
最后更新时间 (UTC):2025-09-05。
[[["易于理解","easyToUnderstand","thumb-up"],["解决了我的问题","solvedMyProblem","thumb-up"],["其他","otherUp","thumb-up"]],[["很难理解","hardToUnderstand","thumb-down"],["信息或示例代码不正确","incorrectInformationOrSampleCode","thumb-down"],["没有我需要的信息/示例","missingTheInformationSamplesINeed","thumb-down"],["翻译问题","translationIssue","thumb-down"],["其他","otherDown","thumb-down"]],["最后更新时间 (UTC):2025-09-05。"],[[["\u003cp\u003eManual live migration can move sole-tenant VMs to a different node or node group, and multi-tenant VMs into sole-tenancy.\u003c/p\u003e\n"],["\u003cp\u003ePerforming manual live migration requires \u003ccode\u003ecompute.instances.update\u003c/code\u003e permissions on the VM.\u003c/p\u003e\n"],["\u003cp\u003eThe gcloud CLI or REST can be used to manually live migrate VMs into or within sole-tenancy, using the \u003ccode\u003egcloud compute instances update\u003c/code\u003e command or the \u003ccode\u003einstances.update\u003c/code\u003e method.\u003c/p\u003e\n"],["\u003cp\u003eWhen a node group is the destination, Compute Engine selects the node with sufficient space and the least spare capacity.\u003c/p\u003e\n"],["\u003cp\u003eLive migration between projects using shared sole-tenant node groups is not supported.\u003c/p\u003e\n"]]],[],null,["# Manually live migrate VMs\n\nLinux Windows\n\n*** ** * ** ***\n\nTo move sole-tenant VMs to a different node or node group, you can manually\ninitiate a live migration. You can also manually initiate a live migration to\nmove a multi-tenant VM into sole-tenancy. For information about how to move a VM\nout of sole-tenancy, see [Move a VM out of sole-tenancy](/compute/docs/nodes/updating-vm-tenancy#moving_a_vm_out_of_sole-tenancy).\n\nBefore you begin\n----------------\n\n- If you haven't already, set up [authentication](/compute/docs/authentication). Authentication verifies your identity for access to Google Cloud services and APIs. To run code or samples from a local development environment, you can authenticate to Compute Engine by selecting one of the following options:\n\n Select the tab for how you plan to use the samples on this page: \n\n ### gcloud\n\n 1.\n [Install](/sdk/docs/install) the Google Cloud CLI.\n\n After installation,\n [initialize](/sdk/docs/initializing) the Google Cloud CLI by running the following command:\n\n ```bash\n gcloud init\n ```\n\n\n If you're using an external identity provider (IdP), you must first\n [sign in to the gcloud CLI with your federated identity](/iam/docs/workforce-log-in-gcloud).\n | **Note:** If you installed the gcloud CLI previously, make sure you have the latest version by running `gcloud components update`.\n 2. [Set a default region and zone](/compute/docs/gcloud-compute#set_default_zone_and_region_in_your_local_client).\n\n ### REST\n\n\n To use the REST API samples on this page in a local development environment, you use the\n credentials you provide to the gcloud CLI.\n 1. [Install](/sdk/docs/install) the Google Cloud CLI. After installation, [initialize](/sdk/docs/initializing) the Google Cloud CLI by running the following command: \n\n ```bash\n gcloud init\n ```\n 2. If you're using an external identity provider (IdP), you must first [sign in to the gcloud CLI with your federated identity](/iam/docs/workforce-log-in-gcloud).\n\n\n For more information, see\n [Authenticate for using REST](/docs/authentication/rest)\n in the Google Cloud authentication documentation.\n\nAPI rate limits\n---------------\n\nManual live migration requests are categorized in the **Queries** [API rate\nlimit](/compute/docs/api-rate-limits).\n\nManually live migrate VMs into or within sole-tenancy\n-----------------------------------------------------\n\nTo manually live migrate VMs into or within sole-tenancy, use the\ngcloud CLI or REST.\n\nIf the destination of the VM is a node group, Compute Engine selects\nthe node within the group that has both enough space for the VM and the least\namount of spare capacity.\n\nYou cannot perform live migration between projects while using shared\nsole-tenant node groups.\n\n#### Permissions required for this task\n\nTo perform this task, you must have the following [permissions](/iam/docs/overview#permissions):\n\n- `compute.instances.update` permissions on the VM.\n\n### gcloud\n\nTo manually live migrate VMs into or within sole-tenancy, use the following\n[`gcloud compute instances update` command](/sdk/gcloud/reference/compute/instances/update): \n\n```\ngcloud compute instances update VM_NAME \\\n ( --node=NODE \\\n --node-group=NODE_GROUP \\\n --node-affinity-file=NODE_AFFINITY_FILE )\n```\n\nReplace the following:\n\n- \u003cvar translate=\"no\"\u003eVM_NAME\u003c/var\u003e: the name of the VM to update the node affinity labels for.\n\nReplace exactly one of the following:\n\n- \u003cvar translate=\"no\"\u003eNODE\u003c/var\u003e: the name of the node to live migrate the VM\n to.\n\n- \u003cvar translate=\"no\"\u003eNODE_GROUP\u003c/var\u003e: the name of the node group to live\n migrate the VM to.\n\n- \u003cvar translate=\"no\"\u003eNODE_AFFINITY_FILE\u003c/var\u003e: the name of a JSON file\n containing a configuration of nodes on which this VM could be scheduled.\n For more information, see [Configure node affinity\n labels](/compute/docs/nodes/provisioning-sole-tenant-vms#configure_node_affinity_labels).\n\n### REST\n\nTo manually live migrate VMs into or within sole-tenancy, use the following\n[`instances.update` method](/compute/docs/reference/rest/v1/instances/update): \n\n```\nPUT https://compute.googleapis.com/compute/v1/projects/PROJECT_ID/zones/ZONE/instances/VM_NAME\n\n{\n ...\n \"scheduling\": {\n \"nodeAffinities\": [\n {\n \"key\": \"NODE_AFFINITY_LABEL_KEY\",\n \"operator\": \"IN\",\n \"values\": [\n \"[NODE_AFFINITY_LABEL_VALUE]\"\n ]\n }\n ]\n }\n ...\n}\n```\n\nReplace the following:\n\n- \u003cvar translate=\"no\"\u003ePROJECT_ID\u003c/var\u003e: the ID of the project that has the VM\n to update the node affinity labels for.\n\n- \u003cvar translate=\"no\"\u003eZONE\u003c/var\u003e: the zone of the VM to update the node\n affinity labels for.\n\n- \u003cvar translate=\"no\"\u003eVM_NAME\u003c/var\u003e: the name of the VM to update the node\n affinity labels for.\n\n- \u003cvar translate=\"no\"\u003eNODE_AFFINITY_LABEL_KEY\u003c/var\u003e: one of the following\n strings that specifies whether to live migrate the VM to a node group or\n node:\n\n- \u003cvar translate=\"no\"\u003eNODE_AFFINITY_LABEL_VALUE\u003c/var\u003e: the name of the node\n group or node to live migrate the VM to.\n\nWhat's next\n-----------\n\n- [Learn how to move a VM out of sole-tenancy](/compute/docs/nodes/updating-vm-tenancy#moving_a_vm_out_of_sole-tenancy)"]] -