查看及套用閒置 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.
      3. 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 驗證說明文件中的「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:您的專案 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 consolegcloud CLIREST 停止執行個體,並視需要刪除執行個體。

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

主控台

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

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

  2. 選取要停止的執行個體。
  3. 按一下「停止」停止 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:您的專案 ID
  • ZONE:包含要停止執行個體的區域
  • VM_NAME:要停止的 VM 執行個體名稱

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

後續步驟