查看及套用閒置 VM 最佳化建議


本頁說明如何使用閒置 VM 最佳化建議,找出並停止閒置的 VM 執行個體,以減少資源浪費,並降低專案的運算帳單。

事前準備

  • 請參閱限制條件,確認您的 VM 是否支援閒置 VM 最佳化建議。
  • 如果尚未設定,請先設定驗證機制。驗證是指驗證身分,以便存取 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. 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.

    2. Set a default region and zone.

    REST

    To use the REST API samples on this page in a local development environment, you use the credentials you provide to the 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.

    For more information, see Authenticate for using REST in the Google Cloud authentication documentation.

定價

閒置 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:專案 ID
  • ZONE:包含要列出最佳化建議的執行個體的可用區

例如:

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

如果位置中沒有閒置的 VM,回應會是空白。否則,回應會針對每個建議包含下列各欄位:


---
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 最佳化建議作業群組包含兩個作業:

  1. 測試作業,用於驗證 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"
     }
    
  2. 取代作業,用於變更 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 控制台gcloud CLIREST 停止執行個體,並視需要刪除。

如果您停止執行個體,但未刪除該執行個體及其磁碟,仍須支付磁碟費用。

主控台

  1. 前往 Google Cloud 控制台的「VM instances」(VM 執行個體) 頁面

    前往「VM instances」(VM 執行個體) 頁面

  2. 選取要停止的執行個體。
  3. 按一下「停止」

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:專案 ID
  • ZONE:包含要停止的執行個體的區域
  • VM_NAME:要停止的 VM 執行個體名稱

如果您確定可以移除 VM 執行個體及其磁碟,可以刪除執行個體

後續步驟