本页面介绍了如何获取、列出和删除实例模板。
准备工作
-
如果您尚未设置身份验证,请进行设置。身份验证是通过其进行身份验证以访问 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 控制台中,打开“实例模板”页面。
- 点击实例模板的名称以查看模板的详细信息。
- 在 Google Cloud 控制台中,打开“实例模板”页面。
- 选择要删除的实例模板。
- 点击删除。
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 时进行身份验证。
获取有关实例模板的信息
查看现有实例模板时,您可以查看其名称、唯一 ID、创建时间戳以及其指定的所有实例属性。
控制台
gcloud
如需获取有关区域级或全球实例模板的信息,请使用
instance-templates describe command
。对于区域实例模板,请使用以下命令:
gcloud compute instance-templates describe INSTANCE_TEMPLATE_NAME_OR_ID \ --region=REGION
对于全局实例模板,请使用以下命令:
gcloud compute instance-templates describe INSTANCE_TEMPLATE_NAME_OR_ID
Go
Java
Node.js
Python
REST
如需获取有关区域实例模板的信息,请使用
regionInstanceTemplates.get
方法,如下所示:GET https://compute.googleapis.com/compute/v1/projects/PROJECT_ID/regions/REGION/instanceTemplates/INSTANCE_TEMPLATE_NAME_OR_ID
如需获取有关全球实例模板的信息,请使用
instanceTemplates.get
方法,如下所示:GET https://compute.googleapis.com/compute/v1/projects/[PROJECT_ID]/global/instanceTemplates/[INSTANCE_TEMPLATE_NAME_OR_ID]
列出实例模板
要获取您创建的实例模板列表,请执行以下操作:
控制台
“实例模板”页面列出了项目中的所有实例模板。
gcloud
如需获取所有区域级实例模板的列表,请使用以下命令:
gcloud compute instance-templates list \ --regions
如需获取特定区域中的区域级实例模板的列表,请使用以下命令:
gcloud compute instance-templates list \ --filter="region:(REGION)"
如需获取全球实例模板的列表,请使用以下命令:
gcloud compute instance-templates list \ --global
如需获取所有实例模板(包括区域和全局实例模板)的列表,请使用以下命令:
gcloud compute instance-templates list
Go
Java
Node.js
Python
REST
如需获取区域实例模板列表,请发出
regionInstanceTemplates.list
请求:GET https://compute.googleapis.com/compute/v1/projects/PROJECT/regions/REGION/instanceTemplates
如需获取全局实例模板列表,请发出
instanceTemplates.list
请求:GET https://compute.googleapis.com/compute/v1/projects/PROJECT_ID/global/instanceTemplates
删除一个实例模板
删除实例模板会将其从模板列表中移除。如果托管式实例组引用了实例模板,则无法删除实例模板。
控制台
gcloud
使用 Google Cloud CLI 运行以下命令:
gcloud compute instance-templates delete INSTANCE_TEMPLATE_NAME
对于区域级实例模板,INSTANCE_TEMPLATE_NAME 必须包含模板的完整网址。例如
https://www.googleapis.com/compute/v1/projects/example-project/regions/us-central1/instanceTemplates/example-regional-instance-template
。Go
Java
Node.js
Python
REST
如需删除区域实例模板,请发出
regionInstanceTemplates.delete
请求:DELETE https://compute.googleapis.com/compute/v1/projects/PROJECT_ID/regions/REGION/instanceTemplates/INSTANCE_TEMPLATE_NAME
如需删除全局实例模板,请发出
instanceTemplates.delete
请求:DELETE https://compute.googleapis.com/compute/v1/projects/PROJECT_ID/global/instanceTemplates/INSTANCE_TEMPLATE_NAME
有时,托管式实例组中的虚拟机可能与组中的其余实例不同步,而且使用与组中其余实例不同的实例模板。如果托管式实例组中的虚拟机使用的模板与指定给组的模板不同,则该虚拟机会继续将其模板用于修复,即使该模板已被删除也是如此。如需详细了解如何应用新实例模板,请参阅将新配置应用于 MIG 中的虚拟机。
如未另行说明,那么本页面中的内容已根据知识共享署名 4.0 许可获得了许可,并且代码示例已根据 Apache 2.0 许可获得了许可。有关详情,请参阅 Google 开发者网站政策。Java 是 Oracle 和/或其关联公司的注册商标。
最后更新时间 (UTC):2025-06-25。
-