本文說明如何建立使用先占虛擬機器 (VM) 執行個體的代管執行個體群組 (MIG)。如果您的工作負載可以容許服務中斷,並希望利用先占 VM 可省下的成本,先占 VM 就能派上用場。
您也可以參閱其他建立 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 限制的完整清單 (視您使用的設定而異),請參閱「MIG 限制」。
建立含有先占 VM 的執行個體範本
您可以使用可用區或區域性 MIG 快速建立多個先占 VM,如此可減少代管執行個體群組中的 VM 費用。例如,您可以建立先佔 VM 群組,使用這些 VM 執行批次處理工作,然後在工作完成時刪除這個群組。
如要建立一組先占 VM,請在執行個體範本中設定可先占選項,然後使用範本建立 MIG。
主控台
-
前往控制台的「Instance Templates」(執行個體範本) 頁面。
前往「Instance templates」(執行個體範本)
其餘步驟會顯示在 Google Cloud 控制台中。
-
點選
「Create instance template」(建立執行個體範本) 。 - 視需求填寫執行個體範本的屬性。
-
按一下
「Advanced options」(進階選項) 並展開「Management」(管理) 部分。 -
在「可用性政策」下方的
「VM provision model」(VM 佈建模型) 清單中,選擇「Spot」。 -
按一下
「Create」(建立) ,建立範本。
gcloud
使用 instance-templates create
指令建立執行個體範本。加上 --preemptible
標記。
gcloud compute instance-templates create INSTANCE_TEMPLATE \ --preemptible
Terraform
以下範例會建立全域執行個體範本。如要提供可預取的選項,請加入 scheduling
區塊。如要進一步瞭解範例中使用的資源,請參閱 google_compute_instance_template
資源。如要建立地區執行個體範本,請使用 google_compute_region_instance_template
資源。
如要瞭解如何套用或移除 Terraform 設定,請參閱「基本 Terraform 指令」。
REST
呼叫 instanceTemplates.insert
方法建立新的執行個體範本。加入 scheduling.preemptible
屬性,並將其設為 true
。
{ "name": "INSTANCE_TEMPLATE", "properties": { "machineType": "zones/ZONE/machineTypes/MACHINE_TYPE", "networkInterfaces": [ { "network": "global/networks/default", "accessConfigs": [ { "name": "external-IP", "type": "ONE_TO_ONE_NAT" } ] } ], "scheduling": { "preemptible": true }, "disks": [ { "type": "PERSISTENT", "boot": true, "mode": "READ_WRITE", "initializeParams": { "sourceImage": "projects/debian-cloud/global/images/family/debian-9" } } ] } }
建立執行個體範本後,您可以使用該範本建立VM 受限於單一可用區的 MIG,或是VM 分散於區域內多個可用區。
後續步驟
- 設定以應用程式為準的自動修復功能,定期確認應用程式在每個 MIG 的 VM 上是否能正常回應,並自動重新建立沒有回應的 VM。
- 啟用自動調度資源功能,根據負載的增減,自動新增或刪除 MIG 中的 VM。
- 瞭解如何設定並套用新的執行個體範本、所有執行個體設定或個別執行個體設定,將新的設定套用至 MIG 中的所有 VM 或部分 VM。
- 新增有狀態設定,在干擾性事件 (例如重新建立、自動修復及更新 VM) 發生期間保留磁碟、中繼資料和 IP 位址。
- 瞭解如何使用代管執行個體,例如在 MIG 中刪除、捨棄及重新建立 VM。
- 查看 MIG 和相關 VM 的資訊。