本页面介绍了如何获取、列出和删除实例模板。
准备工作
-
如果您尚未设置身份验证,请进行设置。身份验证是通过其进行身份验证以访问 Google Cloud 服务和 API 的过程。如需从本地开发环境运行代码或示例,您可以选择以下任一选项向 Compute Engine 进行身份验证:
Select the tab for how you plan to use the samples on this page:
When you use the Google Cloud console to access Google Cloud services and APIs, you don't need to set up authentication.
-
Install the Google Cloud CLI, then initialize it by running the following command:
gcloud init
- Set a default region and zone.
- Install the Google Cloud CLI.
-
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.
- Install the Google Cloud CLI.
-
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.
- Install the Google Cloud CLI.
-
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.
- Install the Google Cloud CLI.
-
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.
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 的凭据。
Install the Google Cloud CLI, then initialize it by running the following command:
gcloud init
如需了解详情,请参阅 Google Cloud 身份验证文档中的使用 REST 时进行身份验证。
-
获取有关实例模板的信息
- 在 Google Cloud 控制台中,前往“实例模板”页面。
- 点击实例模板的名称以查看模板的详细信息。
如需获取有关区域级或全球实例模板的信息,请使用 instance-templates describe command
。
对于区域实例模板,请使用以下命令:
gcloud compute instance-templates describeINSTANCE_TEMPLATE_NAME \ --region=REGION
对于全局实例模板,请使用以下命令:
gcloud compute instance-templates describeINSTANCE_TEMPLATE_NAME
如需获取有关区域实例模板的信息,请使用 regionInstanceTemplates.get
方法,如下所示:
GET https://compute.googleapis.com/compute/v1/projects/PROJECT_ID /regions/REGION /instanceTemplates/INSTANCE_TEMPLATE_NAME
如需获取有关全局实例模板的信息,请使用 instanceTemplates.get
,如下所示:
GET https://compute.googleapis.com/compute/v1/projects/[PROJECT_ID]/global/instanceTemplates/[INSTANCE_TEMPLATE]
列出实例模板
要获取您创建的实例模板列表,请执行以下操作:
“实例模板”页面列出了项目中的所有实例模板。
如需获取所有区域级实例模板的列表,请使用以下命令:
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
如需获取区域实例模板列表,请发出 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
删除一个实例模板
删除实例模板会将其从模板列表中移除。如果托管式实例组引用了实例模板,则无法删除实例模板。
- 在 Google Cloud 控制台中,前往“实例模板”页面。
- 选择要删除的实例模板。
- 点击删除。
使用 Google Cloud CLI 运行以下命令:
gcloud compute instance-templates deleteINSTANCE_TEMPLATE_NAME
对于区域级实例模板,INSTANCE_TEMPLATE_NAME 必须包含模板的完整网址。例如 https://www.googleapis.com/compute/v1/projects/example-project/regions/us-central1/instanceTemplates/example-regional-instance-template
。
如需删除区域实例模板,请发出 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 中的虚拟机。