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


本文說明如何查看及套用永久磁碟磁碟區、IP 位址和自訂磁碟映像檔的閒置資源建議。

Compute Engine 可協助您找出閒置的永久磁碟磁碟區、IP 位址和自訂磁碟映像檔,並提供相關建議,協助您盡量減少浪費,避免產生不必要的費用。

如要瞭解閒置虛擬機器 (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」。

定價

使用閒置資源建議不會產生任何費用。

生成建議

如果資源已 15 天未附加至 VM 或其他資源,建議引擎就會將該資源歸類為閒置。如要進一步瞭解將資源歸類為閒置的條件和建議採取的行動,請參閱下表:

資源 資源分類為閒置的條件 建議做法 存款
Persistent Disk 必須符合下列所有條件:
  • 永久磁碟的建立時間至少在 15 天前。
  • Persistent Disk 從未附加至 VM。
  • 永久磁碟空白。
  • 永久磁碟未繫結至 GKE Pod。
刪除 節省該磁碟 100% 的費用。詳情請參閱「磁碟和映像檔定價」。
Persistent Disk 必須符合下列所有條件:
  • 永久磁碟已卸離至少 15 天。
  • 永久磁碟未繫結至 GKE Pod。
建立永久磁碟的快照,然後刪除磁碟。
將該磁碟的維護成本從 35% 降低至 92%。詳情請參閱「磁碟和映像檔定價」。
自訂映像檔 必須符合下列所有條件:
  • 至少 15 天未曾使用該映像檔建立磁碟。
  • 映像檔未用於任何執行個體範本。
刪除 節省 100% 的圖片費用。詳情請參閱「磁碟和映像檔定價」。
外部 IP 位址 IP 位址已至少 15 天未附加至任何資源。 刪除
節省該 IP 位址的 100% 費用。詳情請參閱外部 IP 位址定價

建議頻率

閒置資源建議會在資源建立 15 天後開始顯示,且每 24 小時更新一次。

查看建議

如要查看閒置資源的建議,請使用 gcloud CLI 或 REST。

控制台

前往 Google Cloud 控制台的「所有最佳化建議」頁面。

前往「最佳化建議」

所有閒置資源都會列在「未使用的 Compute Engine 資源」下方。

gcloud

使用 gcloud recommender recommendations list 指令,並加上 --recommender=RECOMMENDER_ID 旗標:

gcloud recommender recommendations list \
    --project=PROJECT_ID \
    --location=LOCATION \
    --recommender=RECOMMENDER_ID \
    --format=yaml

更改下列內容:

  • PROJECT_ID:您的專案 ID。
  • LOCATION:包含您要取得建議的資源位置。
    • 如果是永久磁碟磁碟區,請指定可用區或區域,例如 us-central1-c
    • IP:
      • 如要列出區域 IP,請指定區域。
      • 如要列出通用 IP,請指定 global。 如要進一步瞭解區域和全域 IP 位址資源,請參閱「IP 位址」。
    • 如為自訂圖片,請指定 global
  • RECOMMENDER_ID建議者的 ID
    • 如為磁碟,請指定 google.compute.disk.IdleResourceRecommender
    • 如為映像檔,請指定 google.compute.image.IdleResourceRecommender
    • 如為 IP,請指定 google.compute.address.IdleResourceRecommender

舉例來說,下列指令會列出 test-projectus-central1-c 的閒置 Persistent Disk 建議:

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


---
content:
    operationGroups:
    - operations:
      - action: add
        resource: //compute.googleapis.com/projects/test-project/global/snapshots/$snapshot-name
        resourceType: compute.googleapis.com/Disk
        path: /
        value:
        - name: $snapshot-name
          sourceDisk: projects/test-project/locations/us-central1-c/disks/pd-name
          storageLocations: us-central1
      - action: remove
        resource: //compute.googleapis.com/projects/test-project/zones/us-central1-c/disks/pd-name
        resourceType: compute.googleapis.com/Disk
        path: /
description: Save cost by snapshotting and then deleting idle persistent disk 'pd-name'
name: projects/test-project/locations/us-central1-c/recommenders/google.compute.disk.IdleResourceRecommender/recommendations/0fd31b24-cc05-4132-8431-ed54a22dd4f1
recommenderSubtype: SNAPSHOT_AND_DELETE_DISK
lastRefreshTime:
  seconds: 1543912652
primaryImpact:
  category: COST
  costProjection:
    cost:
      currencyCode: USD
      units: '-50'
    duration:
      seconds: 2592000
stateInfo: ACTIVE
etag: "cb0e6ac2cfc0b591"

進一步瞭解如何使用 gcloud CLI 處理建議

REST

呼叫 recommendations.list 方法,並指定建議事項 ID,例如 google.compute.disk.IdleResourceRecommender

GET https://recommender.googleapis.com/v1/projects/PROJECT_ID/locations/LOCATION/recommenders/RECOMMENDER_ID/recommendations

更改下列內容:

  • PROJECT_ID:您的專案 ID。
  • LOCATION:包含您要取得建議的資源位置。
    • 如果是永久磁碟磁碟區,請指定可用區或區域,例如 us-central1-c
    • IP:
      • 如要列出區域 IP,請指定區域。
      • 如要列出通用 IP,請指定 global。 如要進一步瞭解區域和全域 IP 位址資源,請參閱「IP 位址」。
    • 如為自訂圖片,請指定 global
  • RECOMMENDER_ID建議者的 ID
    • 如為磁碟,請指定 google.compute.disk.IdleResourceRecommender
    • 如為映像檔,請指定 google.compute.image.IdleResourceRecommender
    • 如為 IP,請指定 google.compute.address.IdleResourceRecommender

以下範例說明如何使用 curl 傳送要求,以及相關聯的回應範例。

PROJECT_ID=test-project
LOCATION=us-central1-c
RECOMMENDER_ID=google.compute.disk.IdleResourceRecommender

curl -H "Authorization: Bearer $(gcloud auth print-access-token)" \
  https://recommender.googleapis.com/v1/projects/$PROJECT_ID/locations/$LOCATION/recommenders/$RECOMMENDER_ID/recommendations

閒置永久磁碟建議的 JSON 回應範例:

{
 "description" : "Save cost by deleting idle persistent disk 'pd-name'",
 "name": "projects/test-project/locations/us-central1-c/recommenders/"
         "google.compute.disk.IdleResourceRecommender/"
         "recommendations/0fd31b24-cc05-4132-8431-ed54a22dd4f1",
 "recommenderSubtype" : "SNAPSHOT_AND_DELETE_DISK",
 "lastRefreshTime": {
   "seconds": 1543912652
 },
 "primaryImpact": {
   "category": COST,
   "costProjection": {
    "cost": {"currencyCode": "USD", "units": -50},
    "duration": { "seconds": 2592000 }
   }
 },
 "stateInfo": ACTIVE,
 "content": {
   "operationGroups" : [
    {
      "operations" : [
       {
         "action": "add",
         "resourceType": "compute.googleapis.com/Disk",
         "resource" : "//compute.googleapis.com/projects/test-project/global/snapshots/$snapshot-name",
         "value": {
           "name": "$snapshot-name",
           "sourceDisk": "projects/test-project/zones/us-central1-c/disks/pd-name",
           "storageLocations": ["us-central1"],
         }
       },
       {
         "action": "remove",
         "resourceType": "compute.googleapis/Disk",
         "resource": "//compute.googleapis.com/projects/test-project/"
                     "zones/us-central1-c/disks/pd-name"
       }
      ]
    }
   ]
 },
 "associatedInsights": [
    {
      "insight": "projects/test-project/locations/us-central1-c/insightTypes/google.compute.disk.IdleResourceInsight/insights/31326443-bcc3-4776-9b86-48879fddb656"
    }
  ],
 "etag": "cb0e6ac2cfc0b591"
}

解讀建議回應

透過 gcloud CLI 或 REST 接收的每項建議都包含作業群組,您可以按順序執行作業來套用建議。舉例來說,閒置永久磁碟建議作業群組包含一或兩項作業:

  • 如果閒置的永久磁碟含有資料,建議會包含 add 作業,用來建立快照以備份永久磁碟。例如:

       {
         "action": "add",
         "resourceType": "compute.googleapis.com/Disk",
         "resource" : "//compute.googleapis.com/projects/test-project/global/snapshots/$snapshot-name",
         "path": "/",
         "value": {
           "name": "$snapshot-name",
           "sourceDisk": "projects/test-project/zones/us-central1-c/disks/pd-name",
           "storageLocations": ["us-central1"]
         }
       }
    
  • remove 作業,可永久刪除閒置的永久磁碟。例如:

       {
         "action": "remove",
         "resourceType": "compute.googleapis.com/Disk",
         "resource" : "//compute.googleapis.com/projects/test-project/zones/us-central1-c/disks/pd-name",
         "path": "/"
       }
    

查看閒置資源建議的洞察資料

Recommender 會根據資源洞察生成建議。查看資源的洞察資料,可進一步瞭解這些資源,例如資源與 VM 分離的時間長度,或是永久磁碟是否空白。

如要查看產生最佳化建議的洞察,可以使用 gcloud CLI 或 REST。

gcloud

使用 insights list 指令

gcloud beta recommender insights list --project=PROJECT_NAME \
  --location=LOCATION --insight-type=INSIGHT_TYPE

更改下列內容:

  • LOCATION:包含您要取得洞察資料的資源位置。
    • 如果是 PD,請指定區域或地區,例如 us-central1-c
    • IP:
      • 如要列出區域 IP,請指定區域。
      • 如要列出通用 IP,請指定 global。 如要進一步瞭解區域和全域 IP 位址資源,請參閱「IP 位址」。
    • 如為自訂圖片,請指定 global
  • INSIGHT_TYPE洞察類型的 ID
    • 如為磁碟,請指定 google.compute.disk.IdleResourceInsight
    • 如為映像檔,請指定 google.compute.image.IdleResourceInsight
    • 如為 IP,請指定 google.compute.address.IdleResourceInsight

REST

請使用 insights.list 方法

GET https://recommender.googleapis.com/v1beta1/projects/PROJECT_ID/locations/LOCATION/insightTypes/INSIGHT_TYPE/insights

更改下列內容:

  • PROJECT_ID:您的專案 ID。
  • LOCATION:包含您要取得洞察資料的資源位置。
    • 如果是永久磁碟磁碟區,請指定可用區或區域,例如 us-central1-c
    • IP:
      • 如要列出區域 IP,請指定區域。
      • 如要列出通用 IP,請指定 global。 如要進一步瞭解區域和全域 IP 位址資源,請參閱「IP 位址」。
    • 如為自訂圖片,請指定 global
  • INSIGHT_TYPE洞察類型的 ID
    • 如為磁碟,請指定 google.compute.disk.IdleResourceInsight
    • 如為映像檔,請指定 google.compute.image.IdleResourceInsight
    • 如為 IP,請指定 google.compute.address.IdleResourceInsight

以下是閒置的 Persistent Disk 資源洞察資訊回應範例:

{
  "name": "projects/test-project/locations/us-central1-c/insightTypes/google.compute.disk.IdleResourceInsight/insights/0ec21a13-bb04-3121-7321-dc43a11cc3e2",
  "description": "Disk 'pd-name' in zone 'us-central1-c' was last used 17 days ago. Consider taking snapshot and delete it.",
  "targetResources": [
    "//compute.googleapis.com/projects/test-project/zones/us-central1-c/disks/pd-name"
  ],
  "insightSubtype": "IDLE_DISK",
  "content": {
    "diskLastUseTime": "2019-10-01 13:00:00",
    "isBlank": false,
  },
  "lastRefreshTime": "2019-10-10 13:00:00",
  "observationPeriod": "15 days",
  "stateInfo": {
    "state": "ACTIVE"
  },
  "category": "COST",
  "etag": "fds421j2349",
  "associatedRecommendations": [
    {
      "projects/test-project/locations/us-central1-c/recommenders/"
      "google.compute.disk.IdleResourceRecommender/"
      "recommendations/0fd31b24-cc05-4132-8431-ed54a22dd4f1"
    }
  ]
}

範例回應包含下列欄位:

  • diskLastUseTime:磁碟上次連結至 VM 的時間。如果磁碟從未附加至 VM,系統會將這個值設為磁碟的建立時間。
  • isBlank:如果磁碟空白且從未附加至 VM,這個欄位會設為 true。否則為 false

如要進一步瞭解洞察資料,請參閱參考文件

套用閒置資源建議

如果您收到閒置資源的建議,並決定要套用該建議,請按照下一節的指示操作。

刪除閒置的 IP 位址

請參閱 addresses.delete 方法gcloud compute addresses delete 指令或「釋放靜態外部 IP 位址」說明文件。

刪除閒置的自訂映像檔

請參閱 images.delete 方法gcloud compute images delete 指令,或「刪除映像檔」說明文件。

套用閒置永久磁碟建議

如要在刪除永久磁碟前建立快照,請參閱「建立永久磁碟快照」。

如要刪除閒置的永久磁碟,請參閱 gcloud compute disks delete 指令

如要瞭解如何從快照還原資料並重新建立永久磁碟,請參閱還原快照