本文說明如何使用執行個體彈性建立代管執行個體群組 (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.
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
主控台
前往 Google Cloud 控制台的「Instance groups」(執行個體群組) 頁面。
點選「建立執行個體群組」。
在「Name」 欄位中輸入 MIG 的名稱。
在「Instance template」(執行個體範本) 清單中,選取要用於 MIG 的執行個體範本。
您必須先完成下列操作,才能指定「執行個體數量」並新增「執行個體選項」:
設定位置如下:
捲動至「Location」(位置) 專區,然後選取「Multiple zones」(多可用區)。
在「區域」和「可用區」下拉式選單中,選取要建立 MIG 中的 VM 的區域和可用區。如果您選取區域執行個體範本,系統會預設選取該範本的區域。
在「Target distribution shape」欄位中,選取「平衡」或「任何單一可用區」。
在隨即開啟的對話方塊中,按一下「Disable instance redistribution」。
刪除自動調度資源設定的方式如下:
在「Autoscaling」(自動調度資源) 部分,從「Autoscaling mode」(自動調度模式) 下拉式選單中,按一下「Delete autoscaling configuration」(刪除自動調度資源設定)。
在隨即開啟的對話方塊中,按一下「刪除」。
捲動回「Number of instances」欄位。
在「Number of instances」(執行個體數) 欄位中,指定群組中所需的 VM 數量。
在「Instance selections」(執行個體選項) 部分,按一下「Add instance selections」(新增執行個體選項)。
「Instance selections」視窗隨即開啟。
按一下「新增執行個體選取項目」。
在「New instance selection」(新執行個體選取) 專區中,執行以下操作:
在「Name」欄位中,輸入要選取的執行個體名稱。
在「Machine types」部分中,按一下「Add machine type」,選取要新增至執行個體選項的機器類型,然後按一下「Done」。
針對要新增至執行個體選取項目的每個機器類型重複執行這項步驟。
將機器類型新增至執行個體選項後,請按一下「完成」。
在「Instance selections」視窗中,按一下「Done」。
其他欄位則保留預設設定,或視需要進行修改。
按一下 [建立]。
gcloud
如要建立含有多種機器類型的區域 MIG,請使用 instance-groups managed create
指令,如下所示:
gcloud compute instance-groups managed create INSTANCE_GROUP_NAME \ --region REGION \ --size TARGET_SIZE \ --template INSTANCE_TEMPLATE \ --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
標記。
更改下列內容:
INSTANCE_GROUP_NAME
:MIG 名稱。REGION
:要建立 MIG 的區域。TARGET_SIZE
:您希望 MIG 建立及維護的 VM 數量。INSTANCE_TEMPLATE
:執行個體範本的名稱。SHAPE
:目標分配型態。這個值可以是balanced
或any-single-zone
。不支援其他目標發布形狀。MACHINE_TYPE
:您要在 MIG 中設定的機器類型,例如n1-standard-16,n2-standard-16,e2-standard-16
。
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
欄位。
更改下列內容:
PROJECT_ID
:您的專案 ID。REGION
:要建立 MIG 的區域。INSTANCE_GROUP_NAME
:MIG 名稱。TARGET_SIZE
:您希望 MIG 建立及維護的 VM 數量。INSTANCE_TEMPLATE_URL
:執行個體範本的網址。SHAPE
:目標分配型態。這個值可以是BALANCED
或ANY_SINGLE_ZONE
。不支援其他目標發布形狀。INSTANCE_SELECTION
:機器類型清單的名稱。MACHINE_TYPE
:您要在 MIG 中設定的機器類型,例如"n1-standard-16","n2-standard-16","e2-standard-16"
。
後續步驟
- 檢查執行個體靈活性設定。
- 變更執行個體彈性,根據偏好設定使用機器類型。
- 建立含有多個機器類型和偏好的 MIG。
- 移除執行個體彈性。