本頁說明如何使用閒置 VM 建議找出並停止閒置的 VM 執行個體,以減少資源浪費,並降低專案的運算費用。
- 如要進一步瞭解 Compute Engine 如何產生閒置 VM 建議,請參閱閒置 VM 執行個體偵測機制。
- 如要進一步瞭解如何設定閒置 VM 最佳化建議,以便接收更多或更少的建議,請參閱「設定閒置 VM 最佳化建議」。
事前準備
- 請參閱限制,確認 VM 是否支援閒置 VM 建議。
-
如果尚未設定驗證,請先完成設定。
「驗證」是指驗證身分的程序,確認您有權存取 Trusted Cloud by S3NS 服務和 API。如要從本機開發環境執行程式碼或範例,請選取下列任一選項,向 Compute Engine 進行驗證:
Select the tab for how you plan to use the samples on this page:
Console
When you use the Trusted Cloud console to access Trusted Cloud by S3NS services and APIs, you don't need to set up authentication.
gcloud
-
安裝 Google Cloud CLI,然後 使用同盟身分登入 gcloud CLI。 登入後,執行下列指令初始化 Google Cloud CLI:
gcloud init
- Set a default region and zone.
REST
如要在本機開發環境中使用本頁的 REST API 範例,請使用您提供給 gcloud CLI 的憑證。
安裝 Google Cloud CLI,然後 使用同盟身分登入 gcloud CLI。 登入後,執行下列指令初始化 Google Cloud CLI:
gcloud init
詳情請參閱 Trusted Cloud 驗證說明文件中的「Authenticate for using REST」。
定價
閒置 VM 建議功能可免費使用。使用建議減少資源用量,有助於節省成本。
查看閒置 VM 執行個體建議
如要查看閒置 VM 相關建議,請使用 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
:您的專案 IDZONE
:包含要列出建議的執行個體 的可用區
例如:
gcloud recommender recommendations list \ --project=my-project \ --location=us-central1-c \ --recommender=google.compute.instance.IdleResourceRecommender \ --format=yaml
如果該位置沒有閒置的 VM,回應會是空白。否則,回應會包括每個建議的下列各欄位:
operationGroups
:按順序執行即可套用建議的一組作業description
:使用者可自然閱讀的建議說明
--- 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
閒置 VM 建議的 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 接收的每項建議都包含一組作業,您可以按順序執行這些作業來套用建議。閒置 VM 建議作業群組包含兩項作業:
測試作業,用於驗證 VM 的目前狀態。
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" }
取代作業,用於變更 VM 的
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
。您可以檢查 VM 狀態,確認是否已停止。第二項作業
replace
表示您應將資源的status
替換為新值TERMINATED
。如要這麼做,請停止 VM (詳情請見下文)。套用閒置 VM 建議
收到閒置 VM 建議後,如果您決定不再需要該執行個體,請使用 Trusted Cloud console、gcloud CLI 或 REST 停止執行個體,並視需要刪除執行個體。
如果您停止執行個體,但未刪除執行個體和磁碟,仍須支付磁碟費用。
主控台
- 前往 Trusted Cloud 控制台的「VM instances」(VM 執行個體) 頁面。
- 選取要停止的執行個體。
- 按一下「停止」 停止 VM。如果沒有「停止」選項,請依序點選 「更多動作」> 「停止」。
gcloud
使用
instances stop
指令,並指定要停止的VM_NAME
。gcloud compute instances stop VM_NAME --zone=ZONE
更改下列內容:
VM_NAME
:要停止的 VM 執行個體名稱ZONE
:包含要停止執行個體的區域
REST
建構
POST
要求以停止執行個體。POST https://compute.googleapis.com/compute/v1/projects/PROJECT_ID/zones/ZONE/instances/VM_NAME/stop
更改下列內容:
PROJECT_ID
:您的專案 IDZONE
:包含要停止執行個體的區域VM_NAME
:要停止的 VM 執行個體名稱
如果確定可以移除 VM 執行個體及其磁碟,請刪除執行個體。
後續步驟
- 進一步瞭解停止、重設及刪除 VM 執行個體對執行個體及其資源的影響。
- 瞭解如何設定閒置 VM 建議。
除非另有註明,否則本頁面中的內容是採用創用 CC 姓名標示 4.0 授權,程式碼範例則為阿帕契 2.0 授權。詳情請參閱《Google Developers 網站政策》。Java 是 Oracle 和/或其關聯企業的註冊商標。
上次更新時間:2025-08-18 (世界標準時間)。
[[["容易理解","easyToUnderstand","thumb-up"],["確實解決了我的問題","solvedMyProblem","thumb-up"],["其他","otherUp","thumb-up"]],[["缺少我需要的資訊","missingTheInformationINeed","thumb-down"],["過於複雜/步驟過多","tooComplicatedTooManySteps","thumb-down"],["過時","outOfDate","thumb-down"],["翻譯問題","translationIssue","thumb-down"],["示例/程式碼問題","samplesCodeIssue","thumb-down"],["其他","otherDown","thumb-down"]],["上次更新時間:2025-08-18 (世界標準時間)。"],[[["This page provides guidance on using idle VM recommendations to identify and stop idle VM instances, thereby reducing resource waste and compute costs."],["Idle VM recommendations can be viewed using the gcloud CLI or REST API, requiring specific permissions such as `recommender.computeInstanceIdleResourceRecommendations.list`."],["Applying an idle VM recommendation involves a two-step process: a test operation to check the VM's current status and a replace operation to change the VM's status to TERMINATED."],["To act on a recommendation, you can stop and optionally delete an identified idle VM instance using the Google Cloud console, the gcloud CLI, or REST, which will require permissions such as `compute.instances.stop` and `compute.instances.delete`."],["Idle VM recommendations are provided free of charge, and using them to reduce resource usage can significantly lower costs, however deleting a VM can cause loss of data."]]],[]] -