查看和应用空闲虚拟机建议


本页面介绍了如何使用空闲虚拟机建议来识别和停止空闲虚拟机实例,以减少资源浪费并降低项目的计算费用。

准备工作

  • 查看限制以检查虚拟机是否支持空闲虚拟机建议。
  • 如果您尚未设置身份验证,请进行设置。身份验证是通过其进行身份验证以访问 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

    1. 安装 Google Cloud CLI,然后通过运行以下命令初始化 Google Cloud CLI:

      gcloud init
    2. Set a default region and zone.
    3. REST

      如需在本地开发环境中使用本页面上的 REST API 示例,请使用您提供给 gcloud CLI 的凭据。

        安装 Google Cloud CLI,然后通过运行以下命令初始化 Google Cloud CLI:

        gcloud init

      如需了解详情,请参阅 Google Cloud 身份验证文档中的使用 REST 时进行身份验证

价格

空闲虚拟机建议是免费提供的。 使用建议来减少资源用量可节省费用。

查看空闲虚拟机实例建议

如需查看有关空闲虚拟机的建议,请使用 gcloud CLI 或 REST。

gcloud

gcloud recommender recommendations list 命令--recommender=google.compute.instance.IdleResourceRecommender 搭配使用:

gcloud recommender recommendations list \
  --project=PROJECT_ID \
  --location=ZONE \
  --recommender=google.compute.instance.IdleResourceRecommender \
  --format=yaml

请替换以下内容:

  • PROJECT_ID:您的项目的 ID
  • ZONE:包含要为其列出建议的实例的可用区

例如:

gcloud recommender recommendations list \
  --project=my-project \
  --location=us-central1-c \
  --recommender=google.compute.instance.IdleResourceRecommender \
  --format=yaml

如果该位置没有空闲虚拟机,则响应为空。否则,响应会为每个建议添加以下字段:


---
content:
...
operationGroups:
- operations:
  - action: test
    path: /status
    resource: //compute.googleapis.com/projects/my-project/zones/us-central1-c/instances/vm-name
    resourceType: compute.googleapis.com/Instance
    value: RUNNING
  - action: replace
    path: /status
    resource: //compute.googleapis.com/projects/my-project/zones/us-central1-c/instances/vm-name
    resourceType: compute.googleapis.com/Instance
    value: TERMINATED
description: Save cost by stopping Idle VM 'vm-name'.
etag: '"83da314c23f634e1"'
lastRefreshTime: '2020-02-24T07:56:40Z'
name: projects/141732092341/locations/us-central1-c/recommenders/google.compute.instance.IdleResourceRecommender/recommendations/0e061a3a-f921-4216-b1b4-62e16942cd1a
primaryImpact:
category: COST
costProjection:
  cost:
    currencyCode: USD
    nanos: -91533961
    units: '-262'
  duration: 2592000s
recommenderSubtype: STOP_VM
stateInfo:
state: ACTIVE

如需详细了解如何使用 gcloud 处理建议,请参阅 gcloud 示例

REST

调用 recommendations.list 方法并使用以下建议类型:

  • google.compute.instance.IdleResourceRecommender

API 调用类似于以下内容:

GET https://recommender.googleapis.com/v1/projects/PROJECT_ID/locations/ZONE/recommenders/google.compute.instance.IdleResourceRecommender/recommendations

请替换以下内容:

  • PROJECT_ID:您的项目的 ID。
  • ZONE:包含要为其列出建议的实例的可用区。

以下示例演示了如何发送包含 curl 的请求以及关联的示例响应。

PROJECT_ID=my-project
ZONE=us-central1-c
RECOMMENDER_ID=google.compute.instance.IdleResourceRecommender

curl -H "Authorization: Bearer $(gcloud auth print-access-token)" \
  -H "x-goog-user-project: $PROJECT_ID" \
  https://recommender.googleapis.com/v1/projects/$PROJECT_ID/locations/$ZONE/recommenders/$RECOMMENDER_ID/recommendations

空闲虚拟机建议的 JSON 响应示例:

{
"description" : "Save cost by stopping Idle VM `vm-name`",
"name": "projects/1574864402/locations/us-central1-c/recommenders/"
        "google.compute.instance.IdleResourceRecommender/"
        "recommendations/0fd31b24-cc05-4132-8431-ed54a22dd4f1",
"lastRefreshTime": {
  "seconds": 1543912652
},
"primaryImpact": {
  "category": COST,
  "costProjection": {
    "cost": {"currencyCode": "USD", "units": -50},
    "duration": { "seconds": 2592000 }
  }
},
"stateInfo": ACTIVE,
"content":
  "groups" : [
    {
      "operations" : [
      {
        "action": "test",
        "resourceType": "compute.googleapis.com/Instance",
        "resource": "//compute.googleapis.com/projects/my-project/"
                    "zones/us-central1-c/instances/vm-name",
        "path": "/status",
        "value": "RUNNING"
      },
      {
        "action": "replace",
        "resourceType": "compute.googleapis.com/Instance",
        "resource": "//compute.googleapis.com/projects/my-project/"
                    "zones/us-central1-c/instances/vm-name",
        "path": "/status",
        "value": "TERMINATED"
      }
      ]
    }
  ]
},
"etag" : "cb0e6ac2cfc0b591"
}

如需详细了解每个字段,请参阅 Recommender API 文档

解读建议响应

您通过 gcloud CLI 或 REST 收到的每条建议都包含一个操作组,其中有一些可按顺序执行以应用该建议的操作。空闲虚拟机建议操作组包含两个操作:

  1. 用于验证虚拟机当前 status 的测试操作。例如:

     {
       "action": "test",
       "resourceType": "compute.googleapis.com/Instance",
       "resource" : "//compute.googleapis.com/projects/my-project/zones/us-central1-c/instances/vm-name",
       "path": "/status",
       "value": "RUNNING"
     }
    
  2. 用于更改虚拟机 status 的替换操作。例如:

     {
       "action": "replace",
       "resourceType": "compute.googleapis.com/Instance",
       "resource" : "//compute.googleapis.com/projects/my-project/zones/us-central1-c/instances/vm-name",
       "path": "/status",
       "value": "TERMINATED"
     }
    

第一个操作是 test,这意味着您应该测试 resource 是否仍在 RUNNING。您可以通过检查虚拟机状态来执行此操作。

第二个操作是 replace,这意味着您应该将资源的 status 替换为新值 TERMINATED。您可以通过停止虚拟机来执行此操作,下文将会介绍。

应用空闲虚拟机建议

在您收到空闲虚拟机建议并决定不再需要相应实例后,请使用 Google Cloud 控制台gcloud CLIREST 停止该实例,还可以删除该实例。

如果您停止但不删除实例及其磁盘,则仍需为其磁盘支付费用。

控制台

  1. 在 Google Cloud 控制台中,前往虚拟机实例页面。

    转到“虚拟机实例”页面

  2. 选择要停止的实例。
  3. 点击停止

gcloud

使用 instances stop 命令并指定要停止的 VM_NAME

gcloud compute instances stop VM_NAME --zone=ZONE

请替换以下内容:

  • VM_NAME:您要停止的虚拟机实例的名称
  • ZONE:包含要停止的实例的可用区

REST

构建用于停止实例的 POST 请求。

POST https://compute.googleapis.com/compute/v1/projects/PROJECT_ID/zones/ZONE/instances/VM_NAME/stop

替换以下内容:

  • PROJECT_ID:您的项目的 ID
  • ZONE:包含要停止的实例的可用区
  • VM_NAME:您要停止的虚拟机实例的名称

如果您确定可以移除虚拟机实例及其磁盘,则可以删除实例

后续步骤