本文說明暫停和停止虛擬機器 (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.
Terraform
To use the Terraform samples on this page in a local development environment, install and initialize the gcloud CLI, and then set up Application Default Credentials with your user credentials.
- 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.
For more information, see Set up authentication for a local development environment.
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 API 進行自動調度
建議您使用 Compute Engine 自動配置器來自動調整 MIG。不過,如果您基於某些原因偏好使用其他自動配置器,請使用 MIG API 的端點處理已暫停和已停止的 VM。
您可以使用 MIG API 連結自動配置器 (例如 GKE 自動配置器),並使用 VM 的待命集區來加快擴展作業。
選擇暫停和停止的集區
您可以根據特定用途,選擇要使用暫停或停止的資源池。為獲得最佳效能,您應針對擴展規模的情況嘗試不同的待命資源池類型,找出最符合需求的類型。不同的工作負載可能會顯示較短的放送時間,具體取決於選用的選項。在某些情況下,從儲存空間將記憶體狀態複製到 VM 的作業可能比重新啟動 VM 或從頭建立新的 VM 耗時更久。
如要找到最佳做法,請先遵循下列指南:
- 如果您的 VM 需要耗時的記憶體初始化,請使用已暫停的 VM,因為已暫停的 VM 會保留記憶體狀態。確認應用程式可暫停及恢復。維持記憶體狀態需要額外的儲存空間,可能會產生額外費用。
- 如果 VM 初始化作業主要著重於初始化儲存在永久磁碟中的資料,請使用已停止的 VM。
編輯 MIG 中的待命政策
本節說明如何設定初始延遲時間,以及如何將待命集區模式設為擴展集區。
主控台
前往 Google Cloud 控制台的「Instance groups」(執行個體群組) 頁面。
在清單的「Name」欄下方,按一下要編輯待命政策的執行個體群組名稱。
按一下「編輯」,修改這個代管執行個體群組。
按一下「顯示進階設定」。
在「待命資源池」部分的「模式」下方,選取「擴展」。
在「Initial delay」欄位中,輸入 MIG 在暫停或停止 VM 前應等待的秒數。初始延遲時間可讓初始化指令碼有時間準備 VM,以便快速擴充。
按一下 [儲存]。
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]
更改下列內容:
MIG_NAME
:MIG 名稱。DELAY
:MIG 在暫停或停止 VM 前應等待的秒數。初始延遲時間可讓初始化指令碼有時間準備 VM,以便快速擴充。REGION
:如果是區域 MIG,則為 MIG 所在的區域。ZONE
:如果是區域 MIG,則為 MIG 所在的可用區。
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 } // ... }
更改下列內容:
PROJECT_ID
:專案 ID。ZONE
:如果是可用區 MIG,則為 MIG 所在的可用區。- 如果是區域 MIG,請將
zones/ZONE
替換為regions/REGION
,並指定 MIG 的區域。
- 如果是區域 MIG,請將
MIG_NAME
:MIG 名稱。DELAY
:MIG 在暫停或停止 VM 前應等待的秒數。初始延遲時間可讓初始化指令碼有時間準備 VM,以便快速擴充。
調整 MIG 中的待命集區大小
本節說明如何調整 MIG 中已暫停及已停止的 VM 待命集區大小。
主控台
前往 Google Cloud 控制台的「Instance groups」(執行個體群組) 頁面。
在清單的「Name」(名稱) 欄底下,按一下要編輯待命集區大小的執行個體群組名稱。
按一下「編輯」,修改這個代管執行個體群組。
按一下「顯示進階設定」。
在「待命集區」部分,請在「已暫停的 VM」和「已停止的 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]
更改下列內容:
MIG_NAME
:要暫停執行個體的 MIG 名稱。SUSPENDED_SIZE
:MIG 在任何特定時間應維護的暫停 VM 數量。STOPPED_SIZE
:MIG 在任何特定時間應維護的已停止 VM 數量。REGION
:如果是區域 MIG,則為 MIG 所在的區域。ZONE
:如果是區域 MIG,則為 MIG 所在的可用區。
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 // ... }
更改下列內容:
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 的更新。