本文說明如何建立具有執行個體彈性的代管執行個體群組 (MIG),以便在 MIG 中指定多個機型。
執行個體彈性有助於提高資源取得能力,特別是對於可在不同機器類型上運作的應用程式,以及需要大規模容量或高需求硬體的應用程式。詳情請參閱「關於執行個體彈性」。
您也可以參閱建立 MIG 的其他基本情境。
事前準備
- 建立執行個體範本,以便建立代管執行個體群組。
-
如果尚未設定驗證,請先完成設定。
「驗證」是指驗證身分的程序,確認您有權存取 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.
前往 Google Cloud 控制台的「Instance groups」(執行個體群組) 頁面。
點選「建立執行個體群組」。
在「Name」(名稱) 欄位中,輸入 MIG 的名稱。
在「Instance template」(執行個體範本) 清單中,選取要用於 MIG 的執行個體範本。
如要指定「Number of instances」(執行個體數量) 並新增「Instance selections」(執行個體選項) ,請先完成下列步驟:
設定位置資訊,步驟如下:
進一步捲動至「Location」(位置) 專區,然後選取「Multiple zones」(多可用區)。
在「Regions」(區域) 和「Zones」(可用區) 下拉式選單中,選取要建立 MIG 中 VM 的區域和可用區。如果您選取區域執行個體範本,系統會預設選取該範本的區域。
在「目標分配型態」欄位中,選取「平衡」或「任何單一可用區」。
在開啟的對話方塊中,按一下「停用執行個體重新分配」。
請按照下列步驟刪除自動調度資源設定:
在「Autoscaling」(自動調度資源) 區段中,從「Autoscaling mode」(自動調度資源模式) 下拉式選單中,按一下「Delete autoscaling configuration」(刪除自動調度資源設定)。
在開啟的對話方塊中,按一下「刪除」。
捲動返回「Number of instances」(執行個體數量) 欄位。
在「Number of instances」(執行個體數量) 欄位中,指定群組中所需的 VM 數量。
在「執行個體選取項目」部分中,按一下「新增執行個體選取項目」。
「執行個體選取項目」視窗隨即開啟。
按一下「新增執行個體選取項目」。
在「New instance selection」(選取新執行個體) 專區中,執行以下操作:
在「Name」(名稱) 欄位中,輸入執行個體選取項目的名稱。
在「Machine types」(機器類型) 部分,按一下「Add machine type」(新增機器類型),在執行個體選取畫面中選取要新增的機器類型,然後按一下「Done」(完成)。
針對要新增至執行個體選取項目的每個機器類型,重複這個步驟。
將機器類型新增至執行個體選取畫面後,按一下「完成」。
在「執行個體選取項目」視窗中,按一下「完成」。
其他欄位則保留預設值,或視需要修改。
點選「建立」。
INSTANCE_GROUP_NAME
:MIG 的名稱。REGION
:要建立 MIG 的區域。TARGET_SIZE
:MIG 要建立及維護的 VM 數量。INSTANCE_TEMPLATE_URL
:您要用來在 MIG 中建立 VM 的執行個體範本網址。網址可以包含執行個體範本的ID 或名稱。指定下列其中一個值:- 如果是區域執行個體範本:
projects/PROJECT_ID/regions/REGION/instanceTemplates/INSTANCE_TEMPLATE_ID
- 全域執行個體範本:
INSTANCE_TEMPLATE_ID
- 如果是區域執行個體範本:
SHAPE
:目標分配型態。這個值可以是balanced
或any-single-zone
。不支援其他目標分配形狀。MACHINE_TYPE
:您要在 MIG 中設定的機器類型,例如n1-standard-16,n2-standard-16,e2-standard-16
。PROJECT_ID
:您的專案 ID。REGION
:要建立 MIG 的區域。INSTANCE_GROUP_NAME
:MIG 的名稱。TARGET_SIZE
:MIG 要建立及維護的 VM 數量。INSTANCE_TEMPLATE_URL
:您要用來在 MIG 中建立 VM 的執行個體範本網址。網址可以包含執行個體範本的ID 或名稱。指定下列其中一個值:- 如果是區域執行個體範本:
projects/PROJECT_ID/regions/REGION/instanceTemplates/INSTANCE_TEMPLATE_ID
- 全域執行個體範本:
INSTANCE_TEMPLATE_ID
- 如果是區域執行個體範本:
SHAPE
:目標分配型態。這個值可以是BALANCED
或ANY_SINGLE_ZONE
。不支援其他目標分配形狀。INSTANCE_SELECTION
:機器類型清單的名稱。MACHINE_TYPE
:您要在 MIG 中設定的機器類型,例如"n1-standard-16","n2-standard-16","e2-standard-16"
。- 檢查執行個體彈性設定。
- 變更執行個體彈性,根據偏好設定使用機器類型。
- 建立含有多個機器類型和偏好的 MIG。
- 移除執行個體彈性設定。
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
主控台
gcloud
如要建立具有多個機器類型的區域 MIG,請使用
instance-groups managed create
指令,如下所示:gcloud compute instance-groups managed create INSTANCE_GROUP_NAME \ --region REGION \ --size TARGET_SIZE \ --template INSTANCE_TEMPLATE_URL \ --target-distribution-shape SHAPE \ --instance-redistribution-type none \ --instance-selection-machine-types MACHINE_TYPE,MACHINE_TYPE,...
區域 MIG 不支援執行個體彈性。不過,如要在單一可用區中建立 MIG,請將目標分配型態設為
any-single-zone
。此外,如要指定特定區域,請使用any-single-zone
分布形狀並加入--zones ZONE
旗標。更改下列內容:
Terraform
如果您尚未建立執行個體範本,指定 MIG 中每個 VM 要使用的 VM 屬性,請建立執行個體範本。
如要建立具有多種機型的區域 MIG,請使用
google_compute_region_instance_group_manager
資源。如要瞭解如何套用或移除 Terraform 設定,請參閱「基本 Terraform 指令」。
REST
如要建立具有多種機器類型的區域 MIG,請向
regionInstanceGroupManagers.insert
方法發出POST
要求。POST https://compute.googleapis.com/compute/v1/projects/PROJECT_ID/regions/REGION/instanceGroupManagers { "name": "INSTANCE_GROUP_NAME", "targetSize": TARGET_SIZE, "instanceTemplate": "INSTANCE_TEMPLATE_URL", "distributionPolicy": { "targetShape": "SHAPE" }, "updatePolicy": { "instanceRedistributionType": "NONE" }, "instanceFlexibilityPolicy": { "instanceSelections": { "INSTANCE_SELECTION": { "machineTypes": [ "MACHINE_TYPE_1", "MACHINE_TYPE_2", ... ] } } } }
區域 MIG 不支援執行個體彈性。不過,如要在單一可用區中建立 MIG,請將目標分配型態設為
ANY_SINGLE_ZONE
。此外,如要指定區域,請使用ANY_SINGLE_ZONE
分配形狀,並在要求中加入distributionPolicy.zones[].zone
欄位。更改下列內容:
後續步驟
除非另有註明,否則本頁面中的內容是採用創用 CC 姓名標示 4.0 授權,程式碼範例則為阿帕契 2.0 授權。詳情請參閱《Google Developers 網站政策》。Java 是 Oracle 和/或其關聯企業的註冊商標。
上次更新時間:2025-07-21 (世界標準時間)。
-