本頁說明如何使用閒置 VM 建議,找出並停止閒置的 VM 執行個體,以減少資源浪費,並降低專案的運算費用。
- 如要進一步瞭解 Compute Engine 如何產生閒置 VM 建議,請參閱閒置 VM 執行個體偵測機制。
- 如要進一步瞭解如何設定閒置 VM 建議工具,以便接收更多或更少的建議,請參閱「設定閒置 VM 建議」。
事前準備
- 請參閱限制,確認 VM 是否支援閒置 VM 建議。
-
如果尚未設定驗證,請先完成設定。
驗證可確認您的身分,以便存取 Google Cloud 服務和 API。如要從本機開發環境執行程式碼或範例,請選取下列其中一個選項,向 Compute Engine 進行驗證:
選取這個頁面上的分頁,瞭解如何使用範例:
控制台
使用 Google Cloud 控制台存取 Google Cloud 服務和 API 時,無須設定驗證。
gcloud
-
安裝 Google Cloud CLI。 完成後,執行下列指令來初始化 Google Cloud CLI:
gcloud init若您採用的是外部識別資訊提供者 (IdP),請先使用聯合身分登入 gcloud CLI。
-
- 設定預設地區和區域。
REST
如要在本機開發環境中使用本頁的 REST API 範例,請使用您提供給 gcloud CLI 的憑證。
安裝 Google Cloud CLI。
若您採用的是外部識別資訊提供者 (IdP),請先使用聯合身分登入 gcloud CLI。
詳情請參閱 Google Cloud 驗證說明文件中的「使用 REST 進行驗證」。
定價
閒置 VM 建議功能可免費使用。使用建議減少資源用量,有助於節省成本。
查看閒置 VM 執行個體建議
如要查看閒置 VM 相關建議,請使用 gcloud CLI 或 REST。
gcloud
使用加上 --recommender=google.compute.instance.IdleResourceRecommender 的 gcloud recommender recommendations list 指令:
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 建議後,如果您決定不再需要該執行個體,請使用 Google Cloud console、gcloud CLI 或 REST 停止執行個體,並視需要刪除執行個體。
如果您停止執行個體,但未刪除執行個體和磁碟,仍須支付磁碟費用。
控制台
- 前往 Google 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 建議。