本页面介绍了如何使用预留建议来识别和删除空闲的按需预留,从而避免对未使用的资源付费。
- 如需详细了解 Compute Engine 如何生成预留建议,请参阅检测空闲和未充分利用的预留的工作原理。
- 如需详细了解如何配置预留 Recommender 以接收更多或更少的建议,请参阅配置空闲预留建议或配置未充分利用的预留建议。
准备工作
- 查看限制,以检查预留是否支持空闲预留建议。
-
如果您尚未设置身份验证,请进行设置。身份验证是通过其进行身份验证以访问 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.
- 将
ZONE
替换为包含要为其列出建议的预留的可用区。 - 将
PROJECT_ID
替换为项目的 ID。 operationGroups
:您可以执行的若干组操作,以便应用建议description
:建议的说明- google.compute.IdleResourceRecommender
- 详细了解 Compute Engine 预留的工作原理。
- 了解如何配置空闲预留建议。
- 了解如何配置未充分利用的预留建议。
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 时进行身份验证。
查看空闲预留建议
如需查看有关空闲预留的建议,请使用以下任一选项。
请使用以下方法之一完成此任务。
控制台
在 Google Cloud 控制台中,前往 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
替换以下内容:
例如:
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
请求,并使用以下建议类型: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" }
应用空闲预留建议
后续步骤
如未另行说明,那么本页面中的内容已根据知识共享署名 4.0 许可获得了许可,并且代码示例已根据 Apache 2.0 许可获得了许可。有关详情,请参阅 Google 开发者网站政策。Java 是 Oracle 和/或其关联公司的注册商标。
最后更新时间 (UTC):2025-07-08。
-