查看和应用空闲预留建议


本页面介绍了如何使用预留建议来识别和删除空闲的按需预留,从而避免对未使用的资源付费。

准备工作

  • 查看限制,检查您的预留是否支持空闲预留建议。
  • 如果您尚未设置身份验证,请进行设置。身份验证是通过其进行身份验证以访问 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. Install the Google Cloud CLI, then initialize it by running the following command:

      gcloud init
    2. Set a default region and zone.
    3. 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 控制台中,前往 FinOps Hub

转到 FinOps Hub

gcloud

如需查看有关空闲预留的建议,请将 gcloud recommender recommendations list 命令--recommender=google.compute.IdleResourceRecommender 标志搭配使用:

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

替换以下内容:

  • ZONE 替换为包含要列出建议的预留的可用区。
  • PROJECT_ID 替换为项目的 ID。

例如:

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

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


---
associatedInsights:
- insight: projects/953727763714/locations/us-central1-f/insightTypes/google.compute.IdleResourceInsight/insights/ecf77776-4a2a-4e6e-aad2-b7f9c632e5f9
content:
operationGroups:
- operations:
  - action: remove
    path: /
    resource: //compute.googleapis.com/projects/committed-use-discount-test/zones/us-central1-f/reservations/test-reservation
    resourceType: compute.googleapis.com/Reservation
overview:
  currentReservation:
    machineType: n1-standard-1
    reservationName: test-reservation
    reservedMachineCount: '1'
    zone: us-central1-f
description: Save cost by deleting the idle reservation test-reservation
etag: '"aff724ab8e40e1ef"'
lastRefreshTime: '2024-06-10T07:00:00Z'
name: projects/953727763714/locations/us-central1-f/recommenders/google.compute.IdleResourceRecommender/recommendations/56f8ac5d-8380-4cb8-a76f-bbc270fb4b8e
primaryImpact:
category: COST
costProjection:
  cost:
    currencyCode: USD
    nanos: -199820000
    units: '-1072'
  costInLocalCurrency:
    currencyCode: USD
    nanos: -199820000
    units: '-1072'
  duration: 2592000s
priority: P2
recommenderSubtype: DELETE_RESERVATION
stateInfo:
state: ACTIVE
targetResources:
- //compute.googleapis.com/projects/committed-use-discount-test/zones/us-central1-f/reservations/test-reservation

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

REST

如需查看有关空闲预留的建议,请向 recommendations.list 方法发出 GET 请求,并使用以下建议类型:

  • google.compute.IdleResourceRecommender

API 调用类似于以下内容:

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

替换以下变量: - PROJECT_ID:项目的 ID。 - ZONE:包含要为其列出建议的实例的可用区。

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

PROJECT_ID=my-project
ZONE=us-central1-c
RECOMMENDER_ID=google.compute.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 响应示例:

{
  "associatedInsights": [
    {
      "insight": "projects/9531234763714/locations/us-central1-f/insightTypes/google.compute.IdleResourceInsight/insights/ecf77776-4a2a-4e6e-aad2-b7f9c632e5f9"
    }
  ],
  "content": {
    "operationGroups": [
      {
        "operations": [
          {
            "action": "remove",
            "path": "/",
            "resource": "//compute.googleapis.com/projects/committed-use-discount-test/zones/us-central1-f/reservations/test-reservation",
            "resourceType": "compute.googleapis.com/Reservation"
          }
        ]
      }
    ],
    "overview": {
      "currentReservation": {
        "machineType": "n1-standard-1",
        "reservationName": "test-reservation",
        "reservedMachineCount": "1",
        "zone": "us-central1-f"
      }
    }
  },
  "description": "Save cost by deleting the idle reservation test-reservation",
  "etag": "\"8d5603cac8fa9342\"",
  "lastRefreshTime": "2024-06-11T07:00:00Z",
  "name": "projects/9531234763714/locations/us-central1-f/recommenders/google.compute.IdleResourceRecommender/recommendations/56f8ac5d-8380-4cb8-a76f-bbc270fb4b8e",
  "primaryImpact": {
    "category": "COST",
    "costProjection": {
      "cost": {
        "currencyCode": "USD",
        "nanos": -199820000,
        "units": "-1072"
      },
      "costInLocalCurrency": {
        "currencyCode": "USD",
        "nanos": -199820000,
        "units": "-1072"
      },
      "duration": "2592000s"
    }
  },
  "priority": "P2",
  "recommenderSubtype": "DELETE_RESERVATION",
  "stateInfo": {
    "state": "ACTIVE"
  },
  "targetResources": [
    "//compute.googleapis.com/projects/committed-use-discount-test/zones/us-central1-f/reservations/test-reservation"
  ]
}

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

解读建议响应

您通过 gcloud CLI 或 REST 收到的每条建议都包含一个操作组,其中有一些可按顺序执行以应用该建议的操作。对于空闲预留,操作组包含用于删除预留的 remove 操作:

{
  "action": "remove",
  "path": "/",
  "resource": "//compute.googleapis.com/projects/committed-use-discount-test/zones/us-central1-f/reservations/test-reservation",
  "resourceType": "compute.googleapis.com/Reservation"
}

应用空闲预留建议

根据空闲预留建议,调整预留大小删除预留(如果不再需要)。

后续步骤