建立使用先占 VM 的 MIG


本文說明如何建立使用先占虛擬機器 (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

      1. 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.

      2. Set a default region and zone.
      3. Terraform

        如要在本機開發環境中使用本頁的 Terraform 範例,請安裝並初始化 gcloud CLI,然後使用使用者憑證設定應用程式預設憑證。

        1. Install the Google Cloud CLI.

        2. If you're using an external identity provider (IdP), you must first sign in to the gcloud CLI with your federated identity.

        3. To initialize the gcloud CLI, run the following command:

          gcloud init
        4. 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.

        詳情請參閱 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。

主控台

  1. 前往控制台的「Instance Templates」(執行個體範本) 頁面。

    前往「Instance templates」(執行個體範本) 頁面

    其餘步驟會顯示在 Google Cloud 控制台。

  2. 點選「Create instance template」(建立執行個體範本)
  3. 視需求填寫執行個體範本的屬性。
  4. 按一下「Advanced options」(進階選項) 並展開「Management」(管理) 部分。
  5. 在「可用性政策」下方的「VM provision model」(VM 佈建模型) 清單中,選擇「Spot」
  6. 按一下「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 資源

resource "google_compute_instance_template" "default" {
  name         = "preemptible-template"
  machine_type = "n1-standard-1"
  disk {
    source_image = "debian-cloud/debian-11"
  }
  network_interface {
    network = "default"
  }
  scheduling {
    preemptible       = "true"
    automatic_restart = "false"
  }
}

如要瞭解如何套用或移除 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 分散於同一區域內的多個可用區

後續步驟