設定閒置 VM 最佳化建議


本頁說明如何查看及修改閒置 VM 最佳化建議的設定。您可能需要修改設定,以增加收到的建議數量,或避免將工作負載較小 (但並非閒置) 的 VM 誤分類為閒置 VM。

事前準備

  • 如果尚未設定,請先設定驗證機制。驗證是指驗證身分,以便存取 Google Cloud 服務和 API 的程序。如要在本機開發環境中執行程式碼或範例,您可以選取下列任一選項,向 Compute Engine 進行驗證:

    Select the tab for how you plan to use the samples on this page:

    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 最佳化建議,請執行下列步驟:

  1. 取得目前的設定,以取得設定的 JSON 檔案。
  2. 編輯 JSON 格式的設定檔。
  3. 上傳新的設定檔,套用變更。

取得目前設定

您可以使用 Google Cloud CLI 或 REST,取得專案閒置 VM 最佳化建議的目前設定。

gcloud

如要取得目前的設定參數,請使用 gcloud recommender recommender-config describe 指令,並將 google.compute.instance.IdleResourceRecommender 做為推薦者 ID:

gcloud recommender recommender-config describe google.compute.instance.IdleResourceRecommender \
  --project=PROJECT_ID \
  --location=ZONE

更改下列內容:

  • PROJECT_ID:專案 ID
  • ZONE:要取得目前設定的區域

例如:

gcloud recommender recommender-config describe google.compute.instance.IdleResourceRecommender \
  --project=my-project \
  --location=us-central1-c

這個指令會傳回推薦者設定,包括可修改的子欄位 params.observation_period

{
  "name": "projects/PROJECT_ID/locations/ZONE/recommenders/google.compute.instance.IdleResourceRecommender/config",
  "recommenderGenerationConfig": {
    "params": {
      "observation_period": "1209600s"
    }
  },
  "etag": "\"2f3c9b65cda6a4ba\"",
  "updateTime": "2021-02-03T04:41:15.330351Z"
}

REST

如要擷取目前的設定,請使用 recommenders.getConfig 方法,並將 google.compute.instance.IdleResourceRecommender 指定為推薦者 ID:

GET https://recommender.googleapis.com/v1/projects/PROJECT_ID/locations/ZONE/recommenders/google.compute.instance.IdleResourceRecommender/config

更改下列內容:

  • PROJECT_ID:您要擷取推薦者設定的專案 ID。
  • ZONE:包含專案的區域。

在指令列執行下列指令,取得專案的推薦者設定:

PROJECT_ID=PROJECT_ID
ZONE=ZONE
curl -X GET -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/google.compute.instance.IdleResourceRecommender/config

這項指令會傳回推薦器設定,包括可修改的子欄位:params.observation_period

{
  "name": "projects/PROJECT_ID/locations/ZONE/recommenders/google.compute.instance.IdleResourceRecommender/config",
  "recommenderGenerationConfig": {
    "params": {
      "observation_period": "1209600s"
    }
  },
  "etag": "\"2f3c9b65cda6a4ba\"",
  "updateTime": "2021-02-03T04:41:15.330351Z"
}

更新專案的設定

如要更新閒置 VM 最佳化建議的設定,請修改 observation_period 的值,並為專案上傳新的設定。

您可以使用包含總秒數的字串,並在後方加上字母 s,將觀察期設為 1 天到 14 天之間的值。

  • 如要設定 1 天的觀察期間,請使用 "86400s"
  • 如果觀測期間為 14 天,請使用 "1209600s"

上傳新設定後,新設定大約需要 48 小時才會生效。在此之前,所有產生的推薦內容都會使用先前的設定。

gcloud

如要更新設定參數,請使用 gcloud recommender recommender-config update 指令,並使用 google.compute.instance.IdleResourceRecommender 做為推薦者 ID:

gcloud recommender recommender-config update google.compute.instance.IdleResourceRecommender\
  --project=PROJECT_ID \
  --location=ZONE \
  --config-file=CONFIG_FILE_PATH \
  --etag=ETAG

更改下列內容:

  • PROJECT_ID:專案 ID
  • ZONE:要取得目前設定的區域
  • CONFIG_FILE_PATH:含有目前設定的 JSON 檔案路徑
  • ETAG:目前設定的 etag 值

在設定檔中,請只指定含有更新後設定參數的 recommenderGenerationConfig 物件。舉例來說,如要指定觀察期為五天,請使用下列設定檔:

{
  "params": {
    "observation_period": "432000s"
  }
}

如果指令成功,則會傳回類似以下的回應主體,顯示使用您剛剛修改的參數的新設定。

{
  "name": "projects/PROJECT_ID/locations/ZONE/recommenders/google.compute.instance.IdleResourceRecommender/config",
  "recommenderGenerationConfig": {
    "params": {
      "observation_period": "432000s"
    }
  },
  "etag": "\"1245f9435cd01ea8\"",
  "updateTime": "2021-02-03T05:00:05Z"
}

REST

如要更新設定,請使用 recommenders.updateConfig 方法,並將 google.compute.instance.IdleResourceRecommender 指定為推薦者 ID:

PATCH https://recommender.googleapis.com/v1/projects/PROJECT_ID/locations/ZONE/recommenders/google.compute.instance.IdleResourceRecommender/config

{
  "name": "projects/PROJECT_ID/locations/ZONE/recommenders/google.compute.instance.IdleResourceRecommender/config",
  "recommenderGenerationConfig": {
    "params": {
      "observation_period": "1209600s"
    }
  },
  "etag": "\"2f3c9b65cda6a4ba\""
}

更改下列內容:

  • PROJECT_ID:您要修改推薦者設定的專案 ID。
  • ZONE:包含專案的區域。

將要求主體儲存在檔案中,例如 config.json。接著,您可以使用類似下方的指令,在更新專案的推薦器設定時參照該檔案:

curl -X PATCH \
-H "Authorization: Bearer $(gcloud auth print-access-token)" \
-H "Content-Type: application/json; charset=utf-8" \
-H "x-goog-user-project: ${PROJECT_ID}" \
-d @config.json \
https://recommender.googleapis.com/v1/projects/$PROJECT_ID/locations/$ZONE/recommenders/google.compute.instance.IdleResourceRecommender/config

POST 指令會傳回類似以下的回應主體,顯示使用您剛剛修改的參數的新設定。

{
  "name": "projects/PROJECT_ID/locations/ZONE/recommenders/google.compute.instance.IdleResourceRecommender/config",
  "recommenderGenerationConfig": {
    "params": {
      "observation_period": "432000s"
    }
  },
  "etag": "\"1245f9435cd01ea8\"",
  "updateTime": "2021-02-03T05:00:05Z"
}

後續步驟