本页面介绍了如何使用空闲虚拟机建议来识别和停止空闲虚拟机实例,从而减少资源浪费并降低项目的计算费用。
- 如需详细了解 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.
PROJECT_ID
:您的项目的 IDZONE
:包含要为其列出建议的实例的可用区operationGroups
:您可以按顺序执行(以便应用建议)的若干组操作description
:用户易于阅读的建议说明- google.compute.instance.IdleResourceRecommender
PROJECT_ID
:您的项目的 ID。ZONE
:包含要为其列出建议的实例的可用区。用于验证虚拟机当前
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" }
用于更改虚拟机
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" }
- 在 Google Cloud 控制台中,前往虚拟机实例页面。
- 选择要停止的实例。
- 点击 停止以停止虚拟机。如果没有停止选项,请依次点击 更多操作 > 停止。
VM_NAME
:您要停止的虚拟机实例的名称ZONE
:包含要停止的实例的可用区PROJECT_ID
:您的项目的 IDZONE
:包含要停止的实例的可用区VM_NAME
:您要停止的虚拟机实例的名称- 详细了解停止、重置和删除虚拟机实例对实例及其资源产生的影响。
- 了解如何配置空闲虚拟机建议。
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 时进行身份验证。
价格
空闲虚拟机建议是免费提供的。 使用建议来减少资源用量可节省费用。
查看空闲虚拟机实例建议
如需查看有关空闲虚拟机的建议,请使用 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
请替换以下内容:
例如:
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
方法并使用以下建议类型:API 调用类似于以下内容:
GET https://recommender.googleapis.com/v1/projects/PROJECT_ID/locations/ZONE/recommenders/google.compute.instance.IdleResourceRecommender/recommendations
请替换以下内容:
以下示例演示了如何发送包含
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 收到的每条建议都包含一个操作组,其中有一些可按顺序执行以应用该建议的操作。空闲虚拟机建议操作组包含两个操作:
第一个操作是
test
,这意味着您应该测试resource
是否仍在RUNNING
。您可以通过检查虚拟机状态来执行此操作。第二个操作是
replace
,这意味着您应该将资源的status
替换为新值TERMINATED
。您可以通过停止虚拟机来执行此操作,下文将会介绍。应用空闲虚拟机建议
在您收到空闲虚拟机建议并决定不再需要相应实例后,请使用 Google Cloud console、gcloud CLI 或 REST 停止该实例,还可以删除该实例。
如果您停止但不删除实例及其磁盘,则仍需为其磁盘支付费用。
控制台
gcloud
使用
instances stop
命令并指定要停止的VM_NAME
。gcloud compute instances stop VM_NAME --zone=ZONE
请替换以下内容:
REST
构建用于停止实例的
POST
请求。POST https://compute.googleapis.com/compute/v1/projects/PROJECT_ID/zones/ZONE/instances/VM_NAME/stop
替换以下内容:
如果您确定可以移除虚拟机实例及其磁盘,则可以删除实例。
后续步骤
如未另行说明,那么本页面中的内容已根据知识共享署名 4.0 许可获得了许可,并且代码示例已根据 Apache 2.0 许可获得了许可。有关详情,请参阅 Google 开发者网站政策。Java 是 Oracle 和/或其关联公司的注册商标。
最后更新时间 (UTC):2025-07-08。
-