關閉 MIG 中的維修功能


本文件說明如何在代管執行個體群組 (MIG) 中關閉修復功能,以及如果已關閉,如何將 MIG 重新設定為修復 VM。

根據預設,MIG 會重新建立群組中的失敗 VM,以自動修復該 VM。如果您已設定以應用程式為準的健康狀態檢查,MIG 就會修復應用程式未通過健康狀態檢查的不良 VM。

如果不想讓 MIG 修復失敗或不健康的 VM,可以關閉修復功能。在您想排除 VM 故障問題、實作自己的修復邏輯,或監控應用程式健康狀態,而不需要 MIG 修復不良 VM 的情況下,關閉修復功能就很實用。

如要確認是否已在 MIG 中關閉修復功能,請參閱檢查是否已在 MIG 中關閉修復功能

如要進一步瞭解 MIG 中的修復作業,請參閱「關於修復高可用性 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.

限制

如果群組包含下列項目,就無法在 MIG 中關閉修復功能:

關閉 MIG 中的維修功能

在 MIG 中關閉修復功能後,系統就會關閉自動修復功能,並且不會根據以應用程式為準的健康狀態檢查 (自動修復) 進行修復。

如要關閉修復功能,請使用 Google Cloud 控制台gcloud CLIREST

主控台

  1. 前往 Google Cloud 控制台的「Instance groups」(執行個體群組) 頁面。

    前往「Instance groups」(執行個體群組)

  2. 如果您已擁有 MIG,請執行下列操作:

    1. 按一下要關閉修復功能的 MIG 名稱。
    2. 按一下 [編輯]
  3. 如果您還沒有 MIG,請執行下列操作:

    1. 按一下「建立執行個體群組」
    2. 輸入名稱
    3. 選取執行個體範本
    4. 針對其他設定,您可以使用預設值或修改欄位。
  4. 在「VM 執行個體生命週期」專區中,將「失敗時的預設動作」設為「沒有動作」

  5. 完成後,如果是現有 MIG,請按一下「儲存」,如果是新 MIG,請按一下「建立」

gcloud

如要關閉現有 MIG 中的修復作業,請使用 update 指令

例如,使用下列指令關閉區域性 MIG 中的修復作業:

gcloud compute instance-groups managed update MIG_NAME \
    --zone=ZONE \
    --default-action-on-vm-failure=do_nothing

如要在建立 MIG 時關閉修復功能,請使用 create 指令

例如,在建立區域性 MIG 時,使用下列指令關閉修復作業:

gcloud compute instance-groups managed create MIG_NAME \
    --template=INSTANCE_TEMPLATE \
    --size=SIZE \
    --zone=ZONE \
    --default-action-on-vm-failure=do_nothing

更改下列內容:

  • MIG_NAME:執行個體群組的名稱。
  • INSTANCE_TEMPLATE:要用於群組的執行個體範本名稱。
  • SIZE:執行個體群組的目標大小。
  • ZONE:MIG 所在區域。如為區域 MIG,請使用 --region 標記。

REST

如要關閉現有 MIG 中的修復作業,請使用下列 API 方法:

舉例來說,請發出以下呼叫,在現有的區域性 MIG 中關閉修復作業:

PATCH https://compute.googleapis.com/compute/v1/projects/PROJECT_ID/zones/ZONE/instanceGroupManagers/MIG_NAME

{
  "instanceLifecyclePolicy":
    {
      "defaultActionOnFailure": "DO_NOTHING"
    }
}

如要在建立 MIG 時關閉修復功能,請使用下列 API 方法:

舉例來說,請在建立區域性 MIG 時,發出以下呼叫來關閉修復作業:

POST https://compute.googleapis.com/compute/v1/projects/PROJECT_ID/zones/ZONE/instanceGroupManagers

{
  "versions": [
    {
      "instanceTemplate": "INSTANCE_TEMPLATE_URL"
     }
  ],
  "name": "MIG_NAME",
  "targetSize": SIZE,
  "instanceLifecyclePolicy":
   {
     "defaultActionOnFailure": "DO_NOTHING"
   }
}

更改下列內容:

  • PROJECT_ID:您的專案 ID
  • MIG_NAME:執行個體群組的名稱。
  • INSTANCE_TEMPLATE_URL:您要用於在群組中建立 VM 的執行個體範本的部分網址。例如:
    • 區域執行個體範本:projects/example-project/regions/us-central1/instanceTemplates/example-template
    • 全域執行個體範本:projects/example-project/global/instanceTemplates/example-template
  • SIZE:執行個體群組的目標大小。
  • ZONE:MIG 所在區域。如果是區域 MIG,請在網址中使用 regions/REGION

設定 MIG 以修復失敗和不健康的 VM

根據預設,MIG 會修復失敗和不健康的 VM。如果您已在 MIG 中關閉修復功能,可以將其重新設為修復 VM。

主控台

  1. 前往 Google Cloud 控制台的「Instance groups」(執行個體群組) 頁面。

    前往「Instance groups」(執行個體群組)

  2. 按一下要設定修復作業的 MIG 名稱。

  3. 按一下 [編輯]

  4. 在「VM 執行個體生命週期」部分,將「失敗時的預設動作」設為「修復執行個體」

  5. 完成後,按一下「儲存」套用變更。

gcloud

如要設定 MIG 以修復 VM,請使用 update 指令

gcloud compute instance-groups managed update MIG_NAME \
    --default-action-on-vm-failure=repair

MIG_NAME 替換為 MIG 的名稱。

REST

如要設定 MIG 來修復 VM,請使用下列 API 方法:

舉例來說,如要關閉區域 MIG 中的修復作業,請發出下列呼叫:

PATCH https://compute.googleapis.com/compute/v1/projects/PROJECT_ID/zones/ZONE/instanceGroupManagers/MIG_NAME

{
  "instanceLifecyclePolicy":
  {
    "defaultActionOnFailure": "REPAIR"
 }
}

更改下列內容:

  • PROJECT_ID:您的專案 ID
  • MIG_NAME:執行個體群組的名稱。
  • ZONE:MIG 所在區域。如果是區域 MIG,請在網址中使用 regions/REGION

後續步驟