本页介绍如何使用实例模板创建虚拟机实例。实例模板是定义虚拟机实例属性的 API 资源。您可以在实例模板中定义机器类型、操作系统映像、永久性磁盘配置、元数据、启动脚本等属性,然后使用实例模板创建个别虚拟机实例或托管实例组。
从实例模板创建虚拟机实例时,默认行为是创建与模板中指定的属性相同的虚拟机实例,但虚拟机实例名称和实例所在的地区除外。或者,如果要更改实例模板的特定属性以供特定用途,还可以选择在实例创建期间替换某些字段。
本文档假定您具有现成可使用的实例模板。如果您没有实例模板,请按照说明创建新的实例模板。
准备工作
- 阅读实例模板文档。
- 创建实例模板。
-
如果您尚未设置身份验证,请进行设置。身份验证是通过其进行身份验证以访问 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.
在 Google Cloud 控制台中,前往创建实例页面。
在
虚拟机创建自… 菜单中,选择实例模板。在随即显示的从模板创建虚拟机窗口中,执行以下操作:
选择模板。
要创建并启动该虚拟机,请点击创建。
VM_NAME
:实例的名称。INSTANCE_TEMPLATE_URL
:您要用于在 MIG 中创建虚拟机的实例模板的网址。网址可以包含实例模板的 ID 或名称。请指定以下某个值:- 对于区域级实例模板:
projects/PROJECT_ID/regions/REGION/instanceTemplates/INSTANCE_TEMPLATE_ID
- 对于全球实例模板:
INSTANCE_TEMPLATE_ID
- 对于区域级实例模板:
在 Google Cloud 控制台中,前往创建实例页面。
在
虚拟机创建自… 菜单中,选择实例模板。在随即显示的从模板创建虚拟机窗口中,选择模板后点击
自定义。可选:指定其他配置选项。如需了解详情,请参阅实例创建期间的配置选项。
要创建并启动该实例,请点击创建。
- 如果您替换了某个基本字段,则实例模板中的对应基本字段将替换为请求中的基本字段值。基本字段包括
machineType
、sourceImage
、name
等。 - 如果您替换重复字段,则对应属性的所有重复值都会替换为请求中提供的相应值。重复字段通常是
list
类型的属性。例如,disks
和networkInterfaces
是重复字段。 - 如果您替换
nested object
,则实例模板中的对象会与请求中的相应对象规范合并。请注意,如果嵌套对象位于重复字段中,则将按照重复字段的规则处理该字段。标签是此规则的一个例外,即使它是object
类型,也会被视为重复字段。
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 时进行身份验证。
使用实例模板创建虚拟机实例
您可以使用区域级或全球实例模板创建虚拟机实例。若要完全按照实例模板中的描述创建实例,请按照以下说明操作。
控制台
gcloud
如需通过区域级或全球实例模板创建虚拟机,请使用在创建常规实例时将会使用的
gcloud compute instances create
命令,但要添加--source-instance-template
标志:gcloud compute instances create VM_NAME \ --source-instance-template INSTANCE_TEMPLATE_URL
请替换以下内容:
例如:
gcloud compute instances create example-instance \ --source-instance-template 1234567890
Go
Java
Node.js
Python
REST
如需通过区域级或全球实例模板创建虚拟机,请构造一个常规的实例创建请求,但要在该请求中依次添加
sourceInstanceTemplate
查询参数和实例模板的限定路径。POST https://compute.googleapis.com/compute/v1/projects/ PROJECT_ID/zones/ZONE/ instances?sourceInstanceTemplate=INSTANCE_TEMPLATE_URL
在请求正文中,提供虚拟机实例的
name
:{ "name": "example-instance" }
例如,以下代码段包含模板的完全限定路径:
https://compute.googleapis.com/compute/v1/projects/myproject/global/instanceTemplates/1234567890
。POST https://compute.googleapis.com/ compute/v1/projects/myproject/zones/us-central1-a/instances?sourceInstanceTemplate= https://compute.googleapis.com/compute/v1/projects/myproject/global/ instanceTemplates/1234567890
{ "name": "example-instance" }
使用替换功能以基于实例模板创建虚拟机实例
使用实例模板启动虚拟机实例时,默认行为是完全按照实例模板中的描述创建虚拟机实例,但实例名称和地区除外。
如果您希望主要根据实例模板创建实例,但伴随一些更改,则可以使用替换行为。要使用替换行为,请在创建实例时传入要替换现有实例模板的特性。
控制台
gcloud
通过 gcloud CLI 发出一个请求,以使用
--source-instance-template
标志创建实例并使用适当的gcloud
标志替换所需的任何属性。如需查看适用标志的列表,请参阅gcloud
参考文档。例如,提供以下标志以替换实例模板的机器类型、元数据、操作系统、Persistent Disk 启动磁盘和辅助磁盘:
gcloud compute instances create example-instance \ --source-instance-template 1234567890 --machine-type e2-standard-2 \ --image-family debian-9 --image-project debian-cloud \ --metadata bread=butter --disk=boot=no,name=my-override-disk
Go
Java
Node.js
Python
REST
在 API 中,构建一个创建实例的标准请求时,请使用
sourceInstanceTemplate
查询参数,并在请求正文中提供要替换的任何字段。API 中的替换行为遵循 JSON 合并补丁规则,如 RFC 7396 所述。
具体而言:
例如,假设您有一个带有两个非启动磁盘的实例模板,但您想要替换其中一个磁盘。那么必须在请求中提供整个
disks
规范,包括要保留的任何磁盘。此请求的网址:
POST https://compute.googleapis.com/compute/v1/projects/myproject/zones/us-central1-a/instances? sourceInstanceTemplate=https://compute.googleapis.com/compute/v1/projects/myproject/global/instanceTemplates/1234567890
请求正文:
{ "disks": [ { # Since you are overriding the repeated disk property, you must # specify a boot disk in the request, even if it is already # specified in the instance template "autoDelete": true, "boot": true, "initializeParams": { "sourceImage": "projects/debian-cloud/global/images/family/debian-8" }, "mode": "READ_WRITE", "type": "PERSISTENT" }, { # New disk you want to use "autoDelete": false, "boot": false, "mode": "READ_WRITE", "source": "zones/us-central1-f/disks/my-override-disk", "type": "PERSISTENT" }, { # Assume this disk is already specified in instance template, but # you must specify it again since you are overriding the disks # property "autoDelete": false, "boot": false, "mode": "READ_WRITE", "source": "zones/us-central1-f/disks/my-other-disk-to-keep", "type": "PERSISTENT" } ], "machineType": "zones/us-central1-f/machineTypes/e2-standard-2", "name": "example-instance" }
后续步骤
如未另行说明,那么本页面中的内容已根据知识共享署名 4.0 许可获得了许可,并且代码示例已根据 Apache 2.0 许可获得了许可。有关详情,请参阅 Google 开发者网站政策。Java 是 Oracle 和/或其关联公司的注册商标。
最后更新时间 (UTC):2025-06-23。
-