查看及套用閒置資源建議

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

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

如要瞭解閒置虛擬機器 (VM) 執行個體的建議,請參閱「查看及套用閒置 VM 建議」。

事前準備

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

    選取這個頁面上範例的預計用途分頁:

    控制台

    使用 Google Cloud 控制台存取 Google Cloud 服務和 API 時,不需要設定驗證。

    gcloud

    1. 安裝 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 進行驗證」。

定價

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

生成建議

如果資源在 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-project 中 us-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 指令。

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