本文档介绍如何在 Compute Engine 中使用预留。如需了解如何在其他 Google Cloud 产品中使用预留,请参阅以下文档:
在您创建预留或 Compute Engine 自动为您创建预留以满足未来预留后,Compute Engine 会为您保留预留资源。然后,您可以使用这些预留资源来创建与预留属性匹配的 Compute Engine 实例。此操作称为“使用预留”。您可以使用预留容量来创建实例,直到预留容量完全用完为止。
限制
您无法使用预留来创建以下 Compute Engine 资源:
Spot 虚拟机或抢占式实例
单租户节点
准备工作
-
如果您尚未设置身份验证,请进行设置。身份验证是通过其进行身份验证以访问 Google Cloud 服务和 API 的过程。如需从本地开发环境运行代码或示例,您可以通过选择以下选项之一向 Compute Engine 进行身份验证:
Select the tab for how you plan to use the samples on this page:
Console
When you use the Google Cloud console to access Google Cloud services and APIs, you don't need to set up authentication.
gcloud
-
After installing the Google Cloud CLI, initialize it by running the following command:
gcloud init
If you're using an external identity provider (IdP), you must first sign in to the gcloud CLI with your federated identity.
- Set a default region and zone.
-
Install the Google Cloud CLI.
-
If you're using an external identity provider (IdP), you must first sign in to the gcloud CLI with your federated identity.
-
To initialize the gcloud CLI, run the following command:
gcloud init
-
If you're using a local shell, then create local authentication credentials for your user account:
gcloud auth application-default login
You don't need to do this if you're using Cloud Shell.
If an authentication error is returned, and you are using an external identity provider (IdP), confirm that you have signed in to the gcloud CLI with your federated identity.
-
Install the Google Cloud CLI.
-
If you're using an external identity provider (IdP), you must first sign in to the gcloud CLI with your federated identity.
-
To initialize the gcloud CLI, run the following command:
gcloud init
-
If you're using a local shell, then create local authentication credentials for your user account:
gcloud auth application-default login
You don't need to do this if you're using Cloud Shell.
If an authentication error is returned, and you are using an external identity provider (IdP), confirm that you have signed in to the gcloud CLI with your federated identity.
-
Install the Google Cloud CLI.
-
If you're using an external identity provider (IdP), you must first sign in to the gcloud CLI with your federated identity.
-
To initialize the gcloud CLI, run the following command:
gcloud init
-
If you're using a local shell, then create local authentication credentials for your user account:
gcloud auth application-default login
You don't need to do this if you're using Cloud Shell.
If an authentication error is returned, and you are using an external identity provider (IdP), confirm that you have signed in to the gcloud CLI with your federated identity.
-
Install the Google Cloud CLI.
-
If you're using an external identity provider (IdP), you must first sign in to the gcloud CLI with your federated identity.
-
To initialize the gcloud CLI, run the following command:
gcloud init
-
If you're using a local shell, then create local authentication credentials for your user account:
gcloud auth application-default login
You don't need to do this if you're using Cloud Shell.
If an authentication error is returned, and you are using an external identity provider (IdP), confirm that you have signed in to the gcloud CLI with your federated identity.
-
创建预留:项目的
compute.reservations.create
权限 -
创建实例:
- 针对项目的
compute.instances.create
权限 - 使用自定义映像创建虚拟机:针对映像的
compute.images.useReadOnly
权限 - 使用快照创建虚拟机:针对快照的
compute.snapshots.useReadOnly
权限 - 使用实例模板创建虚拟机:针对实例模板的
compute.instanceTemplates.useReadOnly
权限 - 为虚拟机分配旧版网络:针对项目的
compute.networks.use
权限 - 为虚拟机指定静态 IP 地址:针对项目的
compute.addresses.use
权限 - 使用旧版网络时为虚拟机分配外部 IP 地址:针对项目的
compute.networks.useExternalIp
权限 - 为虚拟机指定子网:针对项目或所选子网的
compute.subnetworks.use
权限 - 在使用 VPC 网络时为虚拟机分配外部 IP 地址:针对项目或所选子网的
compute.subnetworks.useExternalIp
权限 - 为虚拟机设置虚拟机实例元数据:针对项目的
compute.instances.setMetadata
权限 - 为虚拟机设置标记:针对虚拟机的
compute.instances.setTags
权限 - 为虚拟机设置标签:针对虚拟机的
compute.instances.setLabels
权限 - 为虚拟机设置要使用的服务账号:针对虚拟机的
compute.instances.setServiceAccount
权限 - 为虚拟机创建新磁盘:针对项目的
compute.disks.create
权限 - 以只读或读写模式挂接现有磁盘:针对磁盘的
compute.disks.use
权限 - 以只读模式挂接现有磁盘:针对磁盘的
compute.disks.useReadOnly
权限
- 针对项目的
-
创建实例模板:针对项目的
compute.instanceTemplates.create
权限 如需创建示例预留,请完成以下步骤:
在 Google Cloud 控制台中,前往预留页面。
其余步骤会自动显示在Google Cloud 控制台中。
在按需预留标签页(默认)上,点击
创建预留。 此时会显示创建预留页面。在名称字段中,输入预留的名称。例如,输入
reservation-01
。选择要预留资源的区域和可用区。例如,分别选择 us-central1 和 us-central1-a。
在共享类型部分中,执行以下操作之一:
如需创建单项目预留,请选择本地。
如需创建共享预留,请选择共享,然后指定要与之共享预留的项目。
在搭配虚拟机实例使用部分中,选择自动使用预留(如果尚未选择)。
在虚拟机实例数量字段中,输入
3
。在通用标签页上,选择 N2。
在机器类型部分的预设标签页(默认)中,选择 n2-standard-32。
展开 CPU 平台和 GPU,然后在 CPU 平台字段中选择 Intel Cascade Lake 或更高版本。
点击创建。
如需创建使用示例预留的实例,请完成以下步骤:
在 Google Cloud 控制台中,前往创建实例页面。
此时将显示创建实例页面,并显示机器配置窗格。
在“机器配置”窗格中,执行以下操作:
在名称字段中,输入实例的名称。在此示例中,请输入
instance-01
。指定预留资源的区域和可用区。 在本示例中,分别选择 us-central1 和 us-central1-a。
在通用标签页上,选择 N2。
在机器类型部分的预设标签页(默认)中,选择 n2-standard-32。
展开高级选项部分,然后在 CPU 平台字段中选择 Intel Cascade Lake 或更高版本。
可选:自动使用匹配的预留是默认设置。不过,如果您想指定此设置,请执行以下操作:
在导航菜单中,点击高级。此时系统会显示高级窗格。
在预留部分中,选择使用自动选择。
点击创建。
如需创建示例预留,请使用
gcloud compute reservations create
命令:gcloud compute reservations create reservation-01 \ --machine-type=n2-standard-32 \ --min-cpu-platform="Intel Cascade Lake" \ --vm-count=3 \ --zone=us-central1-a
如需创建使用示例预留的实例,请使用
gcloud compute instances create
命令,并将--reservation-affinity
标志设置为any
。由于any
是默认配置,因此您也可以省略此标志。gcloud compute instances create instance-01 \ --machine-type=n2-standard-32 \ --min-cpu-platform="Intel Cascade Lake" \ --reservation-affinity=any \ --zone=us-central1-a
如需创建示例预留,请向
reservations.insert
方法发出POST
请求:POST https://compute.googleapis.com/compute/v1/projects/example-project/zones/us-central1-a/reservations { "name": "reservation-01", "specificReservation": { "count": "3", "instanceProperties": { "machineType": "n2-standard-32", "minCpuPlatform": "Intel Cascade Lake", } } }
如需创建使用示例预留的实例,请向
instances.insert
方法发出POST
请求。在请求正文中,添加设置为ANY_RESERVATION
的consumeReservationType
字段。不过,由于ANY_RESERVATION
是默认配置,因此您也可以省略该字段。POST https://compute.googleapis.com/compute/v1/projects/example-project/zones/us-central1-a/instances { "name": "instance-01", "machineType": "zones/us-central1-a/machineTypes/n2-standard-32", "minCpuPlatform": "Intel Cascade Lake", "disks": [ { "boot": true, "initializeParams": { "sourceImage": "projects/debian-cloud/global/images/family/debian-12" } } ], "networkInterfaces": [ { "network": "global/networks/default" } ], "reservationAffinity": { "consumeReservationType": "ANY_RESERVATION" } }
- 将
--reservation-affinity
标志设置为specific
。 - 将
--reservation
标志设置为预留的网址。 - 设置为
SPECIFIC_RESERVATION
的ConsumeReservationType
字段。 - 设置为
compute.googleapis.com/reservation-name
的Key
字段。 Values
字段设置为预留的网址。- 设置为
SPECIFIC_RESERVATION
的setConsumeReservationType
字段。 - 设置为
compute.googleapis.com/reservation-name
的setKey
字段。 addValues
字段设置为预留的网址。- 设置为
SPECIFIC_RESERVATION
的consumeReservationType
字段。 - 设置为
compute.googleapis.com/reservation-name
的key
字段。 values
字段设置为预留的网址。- 设置为
SPECIFIC_RESERVATION
的consume_reservation_type
字段。 - 设置为
compute.googleapis.com/reservation-name
的key
字段。 values
字段设置为预留的网址。如需创建示例预留,请完成以下步骤:
在 Google Cloud 控制台中,前往预留页面。
其余步骤会自动显示在Google Cloud 控制台中。
在按需预留标签页(默认)上,点击
创建预留。 此时会显示创建预留页面。在名称字段中,输入预留的名称。例如,输入
reservation-02
。指定预留资源的区域和可用区。在本示例中,分别选择 us-central1 和 us-central1-a。
在共享类型部分中,执行以下操作之一:
如需创建单项目预留,请选择本地。
如需创建共享预留,请选择共享,然后指定要与之共享预留的项目。
在搭配虚拟机实例使用部分中,选择选择特定预留。
在虚拟机实例数量字段中,输入
3
。在通用标签页上,选择 N2。
在机器类型部分的预设标签页(默认)中,选择 n2-standard-32。
展开 CPU 平台和 GPU,然后在 CPU 平台字段中选择 Intel Cascade Lake 或更高版本。
点击创建。
如需创建使用示例预留的实例,请完成以下步骤:
在 Google Cloud 控制台中,前往创建实例页面。
此时将显示创建实例页面,并显示机器配置窗格。
在“机器配置”窗格中,执行以下操作:
在名称字段中,输入实例的名称。在此示例中,请输入
instance-02
。指定预留资源的区域和可用区。在本示例中,分别选择 us-central1 和 us-central1-a。
在通用标签页上,选择 N2。
在机器类型部分的预设标签页(默认)中,选择 n2-standard-32。
展开高级选项部分,然后在 CPU 平台字段中选择 Intel Cascade Lake 或更高版本。
在导航菜单中,点击高级。此时系统会显示高级窗格。
在预留部分中,选择选择预留,然后点击选择预留。
在显示的选择预留窗格中,执行以下操作:
选择您在上述步骤中创建的特定预留。如果您要使用存在于其他项目中的共享预留,请在项目列表中选择存在该预留的项目。
点击选择。
点击创建。
如需创建示例预留,请将
gcloud compute reservations create
命令与--require-specific-reservation
标志搭配使用:gcloud compute reservations create reservation-02 \ --machine-type=n2-standard-32 \ --min-cpu-platform="Intel Cascade Lake" \ --require-specific-reservation \ --vm-count=3 \ --zone=us-central1-a
如需创建使用示例预留的实例,请将
gcloud compute instances create
命令与--reservation
和--reservation-affinity=specific
标志搭配使用:gcloud compute instances create instance-02 \ --machine-type=n2-standard-32 \ --min-cpu-platform="Intel Cascade Lake" \ --reservation-affinity=specific \ --reservation=RESERVATION_URL \ --zone=us-central1-a
将
RESERVATION_URL
替换为预留的网址。请指定以下某个值:如果您在同一项目中创建了预留:
reservation-02
如果预留属于其他项目:
projects/PROJECT_ID/reservations/reservation-02
如需创建示例实例以使用单项目特定预留,请使用以下代码示例:
如需创建示例实例以使用共享特定预留,请使用以下代码示例:
如需创建示例预留作为单项目预留,并创建实例进行使用,请使用以下代码示例:
如需创建示例预留作为共享预留,并创建实例进行使用,请使用以下代码示例:
如需创建示例预留并将其用作单项目预留,请使用以下代码示例:
如需创建示例预留并将其用作共享预留,请使用以下代码示例:
如需创建示例预留,请向
instances.insert
方法发出POST
请求。在请求正文中,添加设置为true
的specificReservationRequired
字段。POST https://compute.googleapis.com/compute/v1/projects/example-project/zones/us-central1-a/reservations { "name": "reservation-02", "specificReservation": { "count": "3", "instanceProperties": { "machineType": "n2-standard-32", "minCpuPlatform": "Intel Cascade Lake", } }, "specificReservationRequired": true }
如需创建使用示例预留的实例,请向
instances.insert
方法发出POST
请求。在请求正文的reservationAffinity
字段中,添加以下内容:设置为
SPECIFIC_RESERVATION
的consumeReservationType
字段。设置为
compute.googleapis.com/reservation-name
的key
字段。values
字段设置为预留的网址。
请求类似于以下内容:
POST https://compute.googleapis.com/compute/v1/projects/example-project/zones/us-central1-a/instances { "name": "instance-02", "machineType": "zones/us-central1-a/machineTypes/n2-standard-32", "minCpuPlatform": "Intel Cascade Lake", "disks": [ { "boot": true, "initializeParams": { "sourceImage": "projects/debian-cloud/global/images/family/debian-12" } } ], "networkInterfaces": [ { "network": "global/networks/default" } ], "reservationAffinity": { "consumeReservationType": "SPECIFIC_RESERVATION", "key": "compute.googleapis.com/reservation-name", "values": [ "RESERVATION_URL" ] } }
将
RESERVATION_URL
替换为预留的网址。请指定以下某个值:如果您在同一项目中创建了预留:
reservation-02
如果预留属于其他项目:
projects/PROJECT_ID/reservations/reservation-02
创建预留副本,作为针对单个实例的明确定向预留。
创建测试实例以使用预留。
如需查看当前正在使用预留的计算实例数量,以及还有多少个实例可以使用这些预留,请查看预留。
如需监控每 30 分钟更新一次的预留使用数据,并在预留用完或未用完时收到提醒,请监控预留使用。
如需查看每 24 小时更新一次的预留使用数据,请执行以下操作之一:
- 如需在 Cloud Storage 存储桶中接收使用报告以分析使用趋势并确定未用完的预留,请查看预留用量报告。
- 如需查看过去和预测的预留使用,以分析使用趋势并规划未来的容量需求,请使用容量规划工具。
Go
如需在本地开发环境中使用本页面上的 Go 示例,请安装并初始化 gcloud CLI,然后使用您的用户凭证设置应用默认凭证。
如需了解详情,请参阅 Set up authentication for a local development environment。 如需了解详情,请参阅身份验证文档中的为本地开发环境设置 ADC。
Java
如需在本地开发环境中使用本页面上的 Java 示例,请安装并初始化 gcloud CLI,然后使用您的用户凭证设置应用默认凭证。
如需了解详情,请参阅 Set up authentication for a local development environment。 如需了解详情,请参阅身份验证文档中的为本地开发环境设置 ADC。
Node.js
如需在本地开发环境中使用本页面上的 Node.js 示例,请安装并初始化 gcloud CLI,然后使用您的用户凭证设置应用默认凭证。
如需了解详情,请参阅 Set up authentication for a local development environment。 如需了解详情,请参阅身份验证文档中的为本地开发环境设置 ADC。
Python
如需在本地开发环境中使用本页面上的 Python 示例,请安装并初始化 gcloud CLI,然后使用您的用户凭证设置应用默认凭证。
如需了解详情,请参阅 Set up authentication for a local development environment。 如需了解详情,请参阅身份验证文档中的为本地开发环境设置 ADC。
REST
如需在本地开发环境中使用本页面上的 REST API 示例,请使用您提供给 gcloud CLI 的凭据。
After installing the Google Cloud CLI, initialize it by running the following command:
gcloud init
If you're using an external identity provider (IdP), you must first sign in to the gcloud CLI with your federated identity.
如需了解详情,请参阅 Google Cloud 身份验证文档中的使用 REST 时进行身份验证。
所需的角色
如需获取使用预留所需的权限,请让管理员授予您项目的 Compute Instance Admin (v1) (
roles/compute.instanceAdmin.v1
) IAM 角色。 如需详细了解如何授予角色,请参阅管理对项目、文件夹和组织的访问权限。此预定义角色可提供使用预留所需的权限。如需查看所需的确切权限,请展开所需权限部分:
所需权限
使用预留需要以下权限:
使用预留
以下部分中的示例展示了如何通过创建单个计算实例来使用预留。您还可以使用不同的部署选项创建与预留属性匹配的实例,或者更新现有实例的属性以匹配自动使用的预留,从而使用预留。
如需使用预留,请使用以下方法之一:
使用自动使用的预留
当您创建自动使用的预留时,与该预留的属性匹配的计算实例会自动使用该预留。此使用行为适用于新的和现有的正在运行的实例。当您创建预留或 Compute Engine 自动创建预留以满足未来预留时,此预留类型是默认设置。
如果单项目自动预留和共享自动预留的属性匹配,那么项目中的实例会先使用单项目预留,然后再使用共享预留。 如需了解详情,请参阅预留的使用顺序。
如需创建和使用示例自动预留,请选择以下选项之一:
控制台
以下示例展示了如何在区域
us-central1-a
中为 3 个 N2 实例创建自动预留,这些实例均具有 32 个 vCPU,并且 Intel Cascade Lake 作为满足最低要求的 CPU 平台。还介绍了如何创建单个实例以使用预留。如需创建示例自动预留并进行使用,请执行以下操作:
gcloud
以下示例展示了如何在区域
us-central1-a
中为 3 个 N2 实例创建自动预留,这些实例均具有 32 个 vCPU,并且 Intel Cascade Lake 作为满足最低要求的 CPU 平台。还介绍了如何创建单个实例以使用预留。如需创建示例自动预留并进行使用,请执行以下操作:
Go
如需使用实例模板创建示例自动预留,并使用同一模板创建实例以使用该预留,请使用以下代码示例:
Java
以下示例展示了如何在可用区
us-central1-a
中创建具有四个 vCPU 且 Intel Skylake 作为满足最低要求的 CPU 平台的 N1 实例。实例会自动使用匹配的预留。如需创建示例实例,请使用以下代码示例:
Node.js
以下示例展示了如何在可用区
us-central1-a
中创建具有四个 vCPU 且 Intel Skylake 作为满足最低要求的 CPU 平台的 N1 实例。实例会自动使用匹配的预留。如需创建示例实例,请使用以下代码示例:
Python
以下示例展示了如何在可用区
us-central1-a
中为 3 个 N1 实例创建自动预留,其中每个实例具有 1 个 vCPU,并且 Intel Ivy 作为最低 CPU 平台。还介绍了如何创建单个实例以使用预留。如需创建示例自动预留并进行使用,请执行以下操作:
REST
以下示例展示了如何在区域
us-central1-a
中为 3 个 N2 实例创建自动预留,这些实例均具有 32 个 vCPU,并且 Intel Cascade Lake 作为满足最低要求的 CPU 平台。还介绍了如何创建单个实例以使用预留。如需创建示例自动预留并进行使用,请执行以下操作:
使用明确定向预留
明确定向预留允许新计算实例仅在实例以预留为目标时使用匹配的预留。只有当实例的属性与预留的属性匹配时,系统才会成功创建实例。 否则,您会遇到错误。
根据您要使用的创建方法,创建特定的预留和定向这些预留的实例,如下所示:
创建方法 创建预留时 创建实例时 Google Cloud 控制台 在搭配虚拟机实例使用部分中,选择选择特定预留。 在高级窗格的预留部分中,选择选择预留。 Google Cloud CLI 添加 --require-specific-reservation
标志。添加以下标志:
Go 添加设置为 true
的SpecificReservationRequired
字段。在 ReservationAffinity
字段中,添加以下字段:Java 添加设置为 true
的setSpecificReservationRequired
字段。在 ReservationAffinity
字段中,添加以下字段:Node.js 和 REST API 添加设置为 true
的specificReservationRequired
字段。在 reservationAffinity
字段中,添加以下字段:Python 和 Terraform 添加设置为 true
的specific_reservation_required
字段。在 reservation_affinity
字段中,添加以下字段:如需创建示例特定预留和实例以使用该预留,请选择以下选项之一:
控制台
以下示例展示了如何在可用区
us-central1-a
中为 3 个 N2 实例创建特定预留,这些实例均具有 32 个 vCPU,并且 Intel Cascade Lake 作为满足最低要求的 CPU 平台。还介绍了如何创建单个实例以使用预留。如需创建示例特定预留并进行使用,请执行以下操作:
gcloud
以下示例展示了如何在可用区
us-central1-a
中为 3 个 N2 实例创建特定预留,这些实例均具有 32 个 vCPU,并且 Intel Cascade Lake 作为满足最低要求的 CPU 平台。还介绍了如何创建单个实例以使用预留。如需创建示例特定预留并进行使用,请执行以下操作:
Go
以下示例展示了如何在可用区
us-central1-a
中创建具有 32 个 vCPU 且 Intel Cascade Lake 作为满足最低要求的 CPU 平台的 N2 实例,以使用特定的匹配预留:Java
以下示例展示了如何在区域
us-central1-a
中创建具有四个 vCPU 且 Intel Skylake 作为满足最低要求的 CPU 平台的 N1 实例,以使用特定的匹配预留:Node.js
以下示例展示了如何在区域
us-central1-a
中创建具有 4 个 vCPU 且 Intel Skylake 作为满足最低要求的 CPU 平台的 N1 实例,以使用特定的匹配预留:Python
以下示例展示了如何在可用区
us-central1-a
中创建具有 32 个 vCPU 且 Intel Cascade Lake 作为满足最低要求的 CPU 平台的 N2 实例,以使用特定的匹配预留:如需创建示例特定预留并进行使用,请执行以下操作:
REST
以下示例展示了如何在可用区
us-central1-a
中为 3 个 N2 实例创建特定预留,这些实例均具有 32 个 vCPU,并且 Intel Cascade Lake 作为满足最低要求的 CPU 平台。还介绍了如何创建单个实例以使用预留。如需创建示例特定预留并进行使用,请执行以下操作:
测试实例属性是否与自动使用的预留匹配
如需测试计算实例的属性是否与自动使用的预留匹配,请执行以下操作:
如果您可以创建测试实例,则其属性与测试预留的属性匹配。否则,创建实例将失败。
验证预留使用情况
如需验证预留使用情况,您可以执行以下一项或多项操作:
后续步骤
如未另行说明,那么本页面中的内容已根据知识共享署名 4.0 许可获得了许可,并且代码示例已根据 Apache 2.0 许可获得了许可。有关详情,请参阅 Google 开发者网站政策。Java 是 Oracle 和/或其关联公司的注册商标。
最后更新时间 (UTC):2025-06-23。
-