本文說明如何建立使用先占虛擬機器 (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.
-
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.
-
前往控制台的「Instance Templates」(執行個體範本) 頁面。
前往「Instance templates」(執行個體範本) 頁面
其餘步驟會顯示在 Google Cloud 控制台。
-
點選
「Create instance template」(建立執行個體範本) 。 - 視需求填寫執行個體範本的屬性。
-
按一下
「Advanced options」(進階選項) 並展開「Management」(管理) 部分。 -
在「可用性政策」下方的
「VM provision model」(VM 佈建模型) 清單中,選擇「Spot」。 -
按一下
「Create」(建立) ,建立範本。 - 設定以應用程式為準的自動修復功能,定期確認應用程式在每個 MIG 的 VM 上是否能正常回應,並自動重新建立沒有回應的 VM。
- 啟用自動調度資源,根據負載的增減,自動新增或刪除 MIG 中的 VM。
- 瞭解如何設定及套用新的執行個體範本、所有執行個體設定或個別執行個體設定,將新設定套用至 MIG 中的所有 VM 或部分 VM。
- 透過新增有狀態設定,在干擾性事件 (例如重新建立、自動修復及更新 VM) 發生期間保留磁碟、中繼資料和 IP 位址。
- 瞭解如何使用代管執行個體,例如刪除、捨棄及重新建立 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 限制的完整清單 (因您使用的設定而異),請參閱「MIG 限制」。
建立含有先占 VM 的執行個體範本
您可以使用可用區或區域性 MIG 快速建立多個先占 VM,如此可減少代管執行個體群組中 VM 的費用。例如,您可以建立先占 VM 群組,使用這些 VM 執行批次處理工作,然後在工作完成時刪除這個群組。
如要建立一組先占 VM,請在執行個體範本中設定先占選項,然後使用範本建立 MIG。
主控台
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" } } ] } }
建立執行個體範本後,請使用該範本建立 MIG,將 VM 限制在單一可用區,或將 VM 分散於同一區域內的多個可用區。
後續步驟
除非另有註明,否則本頁面中的內容是採用創用 CC 姓名標示 4.0 授權,程式碼範例則為阿帕契 2.0 授權。詳情請參閱《Google Developers 網站政策》。Java 是 Oracle 和/或其關聯企業的註冊商標。
上次更新時間:2025-07-11 (世界標準時間)。
-