本文档介绍了如何查看代管式实例组 (MIG) 中的实例灵活性配置以及组中每个虚拟机使用的机器类型。
在 MIG 中添加或更改实例灵活性后,您可以执行以下操作:
查看实例灵活性配置,以验证是否已应用配置,或检查 MIG 是否已具有实例灵活性。
查看每个虚拟机使用的机器类型,以确保您要从实例选择中移除的机器类型未被任何虚拟机使用,或检查 MIG 中使用的不同机器类型。
准备工作
-
如果您尚未设置身份验证,请进行设置。身份验证是通过其进行身份验证以访问 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.
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 控制台中,前往实例组页面。
在名称列中,点击要查看其实例灵活性配置的 MIG 的名称。
系统会打开此 MIG 的概览页面。
点击详情标签页。
在实例灵活性部分中,实例选择表会显示实例选择名称、排名以及选择中的机器类型。如果 MIG 未配置实例灵活性,系统不会显示实例灵活性部分。
如需查看实例灵活性配置,请使用 instance-groups managed describe
命令。添加 --format
标志以过滤 instanceFlexibilityPolicy.instanceSelections
属性。
gcloud compute instance-groups managed describeINSTANCE_GROUP_NAME \ --regionREGION \ --format="json(instanceFlexibilityPolicy.instanceSelections)"
以下是输出示例:
{ "instanceFlexibilityPolicy": { "instanceSelections": { "instance-selection-1": { "machineTypes": [ "n1-standard-16", "n2-standard-16", "e2-standard-16" ], "rank": 1 } } } }
如果 MIG 未配置实例灵活性,则输出会返回 null
。
替换以下内容:
INSTANCE_GROUP_NAME
:您要查看其实例灵活性配置的 MIG 的名称。REGION
:MIG 所在的区域。
如需查看实例灵活性配置,请使用 regionInstanceGroupManagers.get
方法。在请求网址中添加 fields
参数以过滤 instanceFlexibilityPolicy.instanceSelections
媒体资源。
GET https://compute.googleapis.com/compute/v1/projects/PROJECT_ID /regions/REGION /instanceGroupManagers/INSTANCE_GROUP_NAME ?fields=instanceFlexibilityPolicy.instanceSelections
以下是输出示例:
{ "instanceFlexibilityPolicy": { "instanceSelections": { "instance-selection-1": { "machineTypes": [ "n1-standard-16", "n2-standard-16", "e2-standard-16" ], "rank": 1 } } } }
如果 MIG 未配置实例灵活性,则输出会返回 null
。
替换以下内容:
PROJECT_ID
:您的项目 ID。REGION
:MIG 所在的区域。INSTANCE_GROUP_NAME
:您要查看其实例灵活性配置的 MIG 的名称。
查看每个虚拟机使用的机器类型
在 Google Cloud 控制台中,前往实例组页面。
在名称列中,点击您要查看其中每个虚拟机所用机器类型的 MIG 的名称。
系统会打开 MIG 的概览页面,其中列出了该组中的所有虚拟机、其机器类型和其他详细信息。
如需查看 MIG 中每个虚拟机使用的机器类型,请使用 instance-groups managed list-instances
命令列出虚拟机,并添加 --format
标志以过滤 propertiesFromFlexibilityPolicy.machineType
字段。您还可以根据需要过滤其他字段。
gcloud compute instance-groups managed list-instancesINSTANCE_GROUP_NAME \ --regionREGION \ --format='(name,instanceStatus,currentAction,propertiesFromFlexibilityPolicy.machineType,lastAttempt.errors.errors)'
以下是输出示例:
NAME: example-mig-0md2 STATUS: RUNNING ACTION: NONE MACHINE_TYPE: n1-standard-16 LAST_ERROR: NAME: example-mig-vbgg STATUS: RUNNING ACTION: NONE MACHINE_TYPE: n1-standard-16 LAST_ERROR:
替换以下内容:
REGION
:MIG 所在的区域。INSTANCE_GROUP_NAME
:您要查看其实例灵活性配置的 MIG 的名称。
如需查看 MIG 中每个虚拟机使用的机器类型,请使用 listManagedInstances
方法列出虚拟机,然后检查响应中的 propertiesFromFlexibilityPolicy.machineType
字段。
POST https://compute.googleapis.com/compute/v1/projects/PROJECT_ID /regions/REGION /instanceGroupManagers/INSTANCE_GROUP_NAME /listManagedInstances
以下是输出示例;查找用于设置虚拟机 machineType
字段的 propertiesFromFlexibilityPolicy
字段。
{ "managedInstances": [ { "instance": "https://www.googleapis.com/compute/v1/projects/example-project/zones/us-central1-b/instances/example-mig-0md2", "instanceStatus": "RUNNING", "id": "2656095434923471959", "version": { "instanceTemplate": "https://www.googleapis.com/compute/v1/projects/example-project/global/instanceTemplates/example-instance-template" }, "targetStatus": "RUNNING", "name": "example-mig-0md2", "propertiesFromFlexibilityPolicy": { "machineType": "n1-standard-16" } }, { "instance": "https://www.googleapis.com/compute/v1/projects/example-project/zones/us-central1-b/instances/example-mig-vbgg", "instanceStatus": "RUNNING", "currentAction": "NONE", "id": "9171259522409694366", "version": { "instanceTemplate": "https://www.googleapis.com/compute/v1/projects/example-project/global/instanceTemplates/example-instance-template" }, "targetStatus": "RUNNING", "name": "example-mig-vbgg", "propertiesFromFlexibilityPolicy": { "machineType": "n1-standard-16" } } ] }
替换以下内容:
PROJECT_ID
:您的项目 ID。REGION
:MIG 所在的区域。INSTANCE_GROUP_NAME
:您要查看其实例灵活性配置的 MIG 的名称。
后续步骤
如果您的 MIG 不支持实例灵活性,请添加实例灵活性。