本文件說明如何設定 MIG,以便在修復作業期間重新建立 VM 時,套用最新的執行個體範本和個別執行個體設定。在修復作業期間套用最新可用的設定也稱為修復時更新。本文件也說明如何檢查 MIG 是否已啟用修復時更新功能,以及如何在啟用後停用修復時更新功能。
根據預設,在修復期間,MIG 會使用當初用來建立 VM 的相同執行個體設定來重新建立 VM。如果群組的執行個體範本或個別執行個體設定可供更新,您可以選擇將這些變更套用至要修復的 VM。
如果您想自動將設定更新套用至所有或一組 VM (PROACTIVE
),或是選擇性更新特定執行個體 (OPPORTUNISTIC
),請參閱「將新的設定套用至 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
-
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.
- 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.
-
設定維修更新
如要設定修復時更新功能,請使用 Google Cloud 控制台、gcloud CLI 或 REST。
主控台
前往 Google Cloud 控制台的「Instance groups」(執行個體群組) 頁面。
按一下要設定修復時更新的 MIG 名稱。
按一下「編輯」即可修改 MIG。
在「VM 執行個體生命週期」區段中,針對「VM 執行個體修復期間的更新設定」,選取「更新執行個體設定」。
按一下 [儲存]。
gcloud
如果是現有的 MIG,請使用 update
指令:
gcloud compute instance-groups managed update MIG_NAME \ --force-update-on-repair
針對新的 MIG,請使用 create
指令:
gcloud compute instance-groups managed create MIG_NAME \ --template INSTANCE_TEMPLATE \ --size SIZE \ --force-update-on-repair
更改下列內容:
MIG_NAME
:執行個體群組的名稱。INSTANCE_TEMPLATE
:要用於群組的執行個體範本名稱。SIZE
:執行個體群組的目標大小。
REST
如果是現有的區域 MIG,請使用 instanceGroupManagers.patch
方法;如果是現有的地區 MIG,請使用 regionInstanceGroupManagers.patch
方法。
請發出以下呼叫,在現有的區域 MIG 中設定修復時更新功能:
PATCH https://compute.googleapis.com/compute/v1/projects/PROJECT_ID/zones/ZONE/instanceGroupManagers/MIG_NAME { "instanceLifecyclePolicy": { "forceUpdateOnRepair": YES } }
如果是新的區域 MIG,請使用 instanceGroupManagers.insert
方法;如果是新的區域 MIG,請使用 regionInstanceGroupManagers.insert
方法。
建立可用區 MIG 時,請發出以下呼叫,設定修復時的更新功能:
POST https://compute.googleapis.com/compute/v1/projects/PROJECT_ID/zones/ZONE/instanceGroupManagers { "name": "MIG_NAME", "instanceTemplate": "INSTANCE_TEMPLATE", "targetSize": SIZE, "instanceLifecyclePolicy": { "forceUpdateOnRepair": YES } }
更改下列內容:
PROJECT_ID
:您的專案 ID。ZONE
:MIG 所在區域。MIG_NAME
:執行個體群組的名稱。INSTANCE_TEMPLATE
:要用於群組的執行個體範本名稱。SIZE
:執行個體群組的目標大小。
檢查是否已啟用修復時更新功能
根據預設,MIG 不會在修復期間更新 VM。如要確認是否已為 MIG 啟用修復時更新功能,請使用 Google Cloud 控制台、gcloud CLI 或 REST 查看修復時更新設定。
主控台
前往 Google Cloud 控制台的「Instance groups」(執行個體群組) 頁面。
按一下要檢查設定的 MIG 名稱。
按一下「詳細資料」分頁標籤。
在「VM 執行個體生命週期」區段中,確認「VM 執行個體修復期間的更新設定」所選的選項。如果選取「更新執行個體設定」,系統就會啟用修復時更新功能。
gcloud
使用 describe
指令,如下所示:
gcloud compute instance-groups managed describe MIG_NAME \ --format="(instanceLifecyclePolicy)"
在回應主體中,檢查 forceUpdateOnRepair
欄位,該欄位會包含下列其中一個值:
NO
:預設值。MIG 不會在修復 VM 時更新 VM。YES
:MIG 會在修復期間更新 VM。
以下是輸出內容範例:
instanceLifecyclePolicy: forceUpdateOnRepair: YES
REST
如果是區域 MIG,請使用 instanceGroupManagers.get
方法;如果是區域 MIG,請使用 regionInstanceGroupManagers.get
方法。
舉例來說,在區域性 MIG 中,請使用下列指令:
GET https://compute.googleapis.com/compute/v1/PROJECT_ID/zones/ZONE/instanceGroupManagers/MIG_NAME
在回應主體中,檢查 instanceLifecyclePolicy.forceUpdateOnRepair
欄位,該欄位包含下列其中一個值:
NO
:預設值。MIG 不會在修復 VM 時更新 VM。YES
:MIG 會在修復期間更新 VM。
以下是回應範例:
{ ... "name": "example-mig", "targetSize": 12, ... "instanceLifecyclePolicy": { "forceUpdateOnRepair": "YES" }, ... }
更改下列內容:
PROJECT_ID
:您的專案 ID。ZONE
:MIG 所在區域。MIG_NAME
:執行個體群組的名稱。
停用維修時的更新功能
當 MIG 修復 VM 時,如果您希望 MIG 使用用來建立 VM 的原始執行個體範本或個別執行個體設定,則必須停用修復時的更新功能。根據預設,MIG 的維修更新功能會停用。
使用 Google Cloud 控制台、gcloud CLI 或 REST 停用修復時更新功能。
主控台
前往 Google Cloud 控制台的「Instance groups」(執行個體群組) 頁面。
按一下要停用修復時更新功能的 MIG 名稱。
按一下「編輯」即可修改 MIG。
在「VM 執行個體生命週期」部分,針對「VM 執行個體修復期間的更新設定」,選取「保留相同的執行個體設定」。
按一下 [儲存]。
gcloud
使用 update
指令設定 --no-force-update-on-repair
標記,如下所示:
gcloud compute instance-groups managed update MIG_NAME \ --no-force-update-on-repair
REST
如果是區域 MIG,請使用 instanceGroupManagers.patch
方法;如果是區域 MIG,請使用 regionInstanceGroupManagers.patch
方法。
舉例來說,如要停用區域 MIG 的修復更新功能,請使用下列指令:
PATCH https://compute.googleapis.com/compute/v1/projects/PROJECT_ID/zones/ZONE/instanceGroupManagers/MIG_NAME { "instanceLifecyclePolicy": { "forceUpdateOnRepair": NO } }
更改下列內容:
PROJECT_ID
:您的專案 ID。ZONE
:MIG 所在區域。MIG_NAME
:執行個體群組的名稱。
後續步驟
- 設定應用程式健康狀態檢查和自動修復。
- 如果您已為 MIG 設定以應用程式為準的健康狀態檢查,請監控 VM 健康狀態變更。