本文說明暫停和停止的虛擬機器 (VM) 執行個體待命集區運作方式,以及如何使用待命集區加速代管執行個體群組 (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
-
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.
-
Install the Google Cloud CLI.
-
If you're using an external identity provider (IdP), you must first sign in to the gcloud CLI with your federated identity.
-
To initialize the gcloud CLI, run the following command:
gcloud init
-
If you're using a local shell, then create local authentication credentials for your user account:
gcloud auth application-default login
You don't need to do this if you're using Cloud Shell.
If an authentication error is returned, and you are using an external identity provider (IdP), confirm that you have signed in to the gcloud CLI with your federated identity.
- 如果 VM 需要耗時的記憶體初始化作業,請使用已暫停的 VM,因為這類 VM 會保留記憶體狀態。確認應用程式可以暫停及繼續。 保留記憶體狀態需要額外儲存空間,可能會產生額外費用。
- 如果 VM 初始化作業主要著重於初始化儲存在永久磁碟中的資料,請使用已停止的 VM。
前往 Google Cloud 控制台的「Instance groups」(執行個體群組) 頁面。
在清單的「Name」(名稱) 欄底下,找到要編輯待命政策的執行個體群組並按一下其名稱。
按一下「編輯」,修改這個代管執行個體群組。
按一下「顯示進階設定」。
在「待命集區」部分的「模式」下方,選取「擴充」。
在「Initial delay」(初始延遲) 欄位中,輸入 MIG 暫停或停止 VM 前應等待的秒數。初始延遲時間可讓初始化指令碼準備 VM,以便快速擴充。
按一下 [儲存]。
MIG_NAME
:MIG 的名稱。DELAY
:MIG 暫停或停止 VM 前應等待的秒數。初始延遲時間可讓初始化指令碼準備 VM,以便快速擴充。REGION
:如果是區域 MIG,則為 MIG 所在的區域。ZONE
:如果是可用區 MIG,則為 MIG 所在的可用區。PROJECT_ID
:您的專案 ID。ZONE
:如果是可用區 MIG,則為 MIG 所在的可用區。- 如果是區域 MIG,請將
zones/ZONE
替換為regions/REGION
,並指定 MIG 的區域。
- 如果是區域 MIG,請將
MIG_NAME
:MIG 的名稱。DELAY
:MIG 暫停或停止 VM 前應等待的秒數。初始延遲時間可讓初始化指令碼準備 VM,以便快速擴充。前往 Google Cloud 控制台的「Instance groups」(執行個體群組) 頁面。
在清單的「Name」(名稱) 欄底下,按一下要編輯待命集區大小的執行個體群組名稱。
按一下「編輯」,修改這個代管執行個體群組。
按一下「顯示進階設定」。
在「待命集區」部分,於「已暫停的 VM」和「已停止的 VM」欄位中輸入新大小。
按一下 [儲存]。
MIG_NAME
:要暫停執行個體的 MIG 名稱。SUSPENDED_SIZE
:MIG 在任何指定時間應維護的暫停 VM 數量。STOPPED_SIZE
:MIG 在任何指定時間應維持的已停止 VM 數量。REGION
:如果是區域 MIG,則為 MIG 所在的區域。ZONE
:如果是可用區 MIG,則為 MIG 所在的可用區。PROJECT_ID
:要求的專案 ID。ZONE
:如果是區域 MIG,則為 MIG 所在的區域。- 如果是區域 MIG,請將
zones/ZONE
替換為regions/REGION
,並指定 MIG 的區域。
- 如果是區域 MIG,請將
MIG_NAME
:要停止執行個體的 MIG 名稱。SUSPENDED_SIZE
:MIG 在任何指定時間應維護的暫停 VM 數量。STOPPED_SIZE
:MIG 在任何指定時間應維持的已停止 VM 數量。- 瞭解如何手動暫停或停止 MIG 中的 VM。
- 進一步瞭解MIG 中已暫停和停止的 VM 更新。
Terraform
如要在本機開發環境中使用本頁的 Terraform 範例,請安裝並初始化 gcloud CLI,然後使用使用者憑證設定應用程式預設憑證。
詳情請參閱 Set up authentication for a local development environment。
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」。
使用 MIG API 進行自動調度
建議您使用 Compute Engine 自動調度器,自動調度 MIG。不過,如果基於某些原因,您偏好使用其他自動調整程式,請使用 MIG API 的端點處理暫停和停止的 VM。
透過 MIG API,您可以連結自動調度資源器 (例如 GKE 自動調度資源器),並使用 VM 待命集區,加快擴充速度。
選擇要暫停或停止的集區
選擇暫停或停止集區取決於您的特定用途。為獲得最佳效能,您應針對擴充情境實驗不同的待命集區類型,找出最符合您需求的類型。不同工作負載可能會顯示較短的服務時間,但選項不同。 在某些情況下,將記憶體狀態從儲存空間複製到 VM 的作業,可能比重新啟動 VM 或從頭建立新 VM 更耗時。
如要找出最佳方法,請先參閱下列指南:
編輯 MIG 中的待命政策
本節說明如何設定初始延遲,以及如何設定待命集區模式來擴充集區。
主控台
gcloud
使用
instance-groups managed update
指令,並指定作業模式和初始延遲。gcloud compute instance-groups managed update MIG_NAME \ --standby-policy-mode=scale-out-pool \ --standby-policy-initial-delay=DELAY \ [--region=REGION | --zone=ZONE]
更改下列內容:
Terraform
下列範例會建立具有待命政策的可用區 MIG。使用
standby_policy
區塊設定初始延遲,並將模式設為SCALE_OUT_POOL
。本範例使用
google_compute_instance_group_manager
資源。如果是區域 MIG,請使用google_compute_region_instance_group_manager
資源。如要瞭解如何套用或移除 Terraform 設定,請參閱「基本 Terraform 指令」。
REST
使用
instanceGroupManager.patch
方法,並在要求主體中指定作業模式和初始延遲時間。如果是區域 MIG,請使用regionInstanceGroupManager.patch
方法。PATCH https://compute.googleapis.com/compute/v1/projects/PROJECT_ID/zones/ZONE/instanceGroupManagers/MIG_NAME { // ... "standbyPolicy": { "mode": "SCALE_OUT_POOL", "initialDelaySec": DELAY } // ... }
更改下列內容:
調整 MIG 中的待命集區大小
本節說明如何調整 MIG 中暫停和停止 VM 的待命集區大小。
主控台
gcloud
使用
instance-groups managed update
指令,並加上--suspended-size
和--stopped-size
旗標。gcloud compute instance-groups managed update MIG_NAME \ --suspended-size=SUSPENDED_SIZE \ --stopped-size=STOPPED_SIZE \ [--region=REGION | --zone=ZONE]
更改下列內容:
Terraform
下列範例會建立可用區 MIG,並為暫停和停止的 VM 設定目標大小。如要設定目標大小,請使用
target_suspended_size
和target_stopped_size
引數。本範例使用
google_compute_instance_group_manager
資源。如果是區域 MIG,請使用google_compute_region_instance_group_manager
資源。如要瞭解如何套用或移除 Terraform 設定,請參閱「基本 Terraform 指令」。
REST
使用
instanceGroupManager.patch
方法,並在要求主體中指定已暫停和已停止 VM 集區的大小。如果是區域 MIG,請使用regionInstanceGroupManager.patch
方法。PATCH https://compute.googleapis.com/compute/v1/projects/PROJECT_ID/zones/ZONE/instanceGroupManagers/MIG_NAME { // ... "targetSuspendedSize": SUSPENDED_SIZE, "targetStoppedSize": STOPPED_SIZE // ... }
更改下列內容:
後續步驟
除非另有註明,否則本頁面中的內容是採用創用 CC 姓名標示 4.0 授權,程式碼範例則為阿帕契 2.0 授權。詳情請參閱《Google Developers 網站政策》。Java 是 Oracle 和/或其關聯企業的註冊商標。
上次更新時間:2025-07-11 (世界標準時間)。
-