在單一區域的多個可用區中建立含有 VM 的 MIG


本文說明如何建立代管執行個體群組 (MIG),其 VM 分散於一個區域的多個可用區。讓應用程式負載分散在多個可用區,可防止工作負載受到可用區故障的影響。如果發生可用區故障,應用程式會繼續從同一區域中其他可用區域中運作的執行個體提供服務。

這種 MIG 又稱為「區域 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

    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.

      5. 詳情請參閱 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 驗證說明文件中的「驗證 REST 使用權」。

限制

如要查看 MIG 限制的完整清單 (視您使用的設定而異),請參閱「MIG 限制」。

在單一區域的多個可用區中建立含有 VM 的 MIG

使用 Google Cloud consolegcloud CLI、Terraform 或 REST

主控台

  1. 前往 Google Cloud 控制台的「Instance groups」(執行個體群組) 頁面。

    前往「Instance groups」(執行個體群組) 頁面

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

  2. 按一下「Create Instance Group」(建立執行個體群組) 以建立新的執行個體群組。
  3. 選取「New managed instance group」(新增代管執行個體群組選項) 選項:
  4. 指定執行個體群組的名稱,並自行選擇是否提供相關說明。
  5. 為執行個體群組選擇執行個體範本,或建立新的範本
  6. 在「Number of instances」(執行個體數量) 或「Autoscaling」(自動調度資源) 中,指定這個群組的執行個體數量。請務必佈建充足的 VM,以便在可用區失效時維持應用程式運作。
  7. 「Location」(位置) 中選取「Multiple zones」(多可用區)
  8. 選擇區域,並選取您要使用的可用區。 如果選擇區域執行個體範本,系統將根據範本的區域自動選取「Region」(區域)
  9. 在「Target distribution shape」(目標分配型態) 之下,選取「Even」(平均分配)。如要選取其他型態,請參閱「在區域性 MIG 中為 VM 設定目標分配型態」一節。
  10. 如要停用主動式執行個體重新分配,在「Instance redistribution」(執行個體重新分配) 之下,請取消選取「Allow instance redistribution」(允許重新分配執行個體) 核取方塊。
  11. 繼續進行其餘的 MIG 建立程序。
  12. 如要建立 MIG,請按一下「Create」(建立)

gcloud

所有 MIG 都需要執行個體範本。如果沒有範本,請建立執行個體範本。舉例來說,下列指令會在 us-east1 地區中,使用預設執行個體屬性建立執行個體範本:

gcloud compute instance-templates create example-template --instance-template-region us-east1

建立 MIG 時,您可以指定執行個體範本的 ID 或名稱。Google 建議您指定 ID,因為與名稱不同,執行個體範本 ID 無法重複使用。如要查看您建立的執行個體範本專屬 ID,請使用 describe 指令

gcloud compute instance-templates describe example-template --region us-east1 --format="value(id)"

接著,請使用 instance-groups managed create 指令,並加上 --region 旗標。舉例來說,下列指令會在 us-east1 地區內的三個區域中建立區域 MIG:

gcloud compute instance-groups managed create example-rmig \
    --template projects/my-project/regions/us-east1/instanceTemplates/1234567890 \
    --size 30 \
    --region us-east1

如要選取該群組應使用的特定區域,請提供 --zones 標記:

gcloud compute instance-groups managed create example-rmig \
    --template projects/my-project/regions/us-east1/instanceTemplates/1234567890 \
    --size 30 \
    --zones us-east1-b,us-east1-c

如果不需要使用特定區域,您可以指定所有區域,並使用均衡分配形狀。詳情請參閱「在各區域中分散 VM 的配置」。

如要停用主動式執行個體重新分配功能,請參閱「主動式執行個體重新分配」。

Terraform

如果您尚未建立執行個體範本 (用於指定 MIG 中每個 VM 的機器類型、開機磁碟映像檔、網路和其他 VM 屬性),請建立執行個體範本

如要建立區域性 MIG,您可以使用 google_compute_region_instance_group_manager 資源

resource "google_compute_region_instance_group_manager" "default" {
  name                      = "example-rmig"
  region                    = "us-east1"
  distribution_policy_zones = ["us-east1-b", "us-east1-c"]
  target_size               = 30
  base_instance_name        = "instance"
  version {
    instance_template = google_compute_instance_template.default.id
  }
}

如要瞭解如何套用或移除 Terraform 設定,請參閱「基本 Terraform 指令」。

REST

所有 MIG 都需要執行個體範本。如果沒有範本,請建立執行個體範本

建立 MIG 時,您可以指定執行個體範本的 ID 或名稱。Google 建議您指定 ID,因為與名稱不同,執行個體範本 ID 無法重複使用。如要查看您建立的執行個體範本的專屬 ID,請在您建立的區域全域執行個體範本上使用 get 方法。

接著,建構對 regionInstanceGroupManagers.insert 方法POST 要求。在要求主體中指定群組名稱、群組大小和執行個體範本的網址。視需要指定其他欄位,例如群組中執行個體的基本名稱。

POST https://compute.googleapis.com/compute/v1/projects/PROJECT_ID/regions/REGION/instanceGroupManagers

{
  "baseInstanceName": "BASE_INSTANCE_NAME",
  "instanceTemplate": "INSTANCE_TEMPLATE_URL",
  "name": "INSTANCE_GROUP_NAME",
  "targetSize": "TARGET_SIZE"
}

更改下列內容:

  • PROJECT_ID:這項要求的專案 ID。
  • REGION:群組的區域。
  • BASE_INSTANCE_NAME:(選用) 建立做為群組一部分的每個 VM 執行個體的執行個體名稱。舉例來說,example-instance 這個基礎執行個體名稱會建立名稱類似 example-instance-[RANDOM_STRING] 的執行個體,其中 [RANDOM_STRING] 是由伺服器產生。
  • INSTANCE_TEMPLATE_URL:您要在 MIG 中用來建立 VM 的執行個體範本網址。網址可以包含執行個體範本的ID 或名稱。請指定下列其中一個值:
    • 區域執行個體範本:projects/PROJECT_ID/regions/REGION/instanceTemplates/INSTANCE_TEMPLATE_ID
    • 全域執行個體範本:INSTANCE_TEMPLATE_ID
  • TARGET_SIZE:群組的 VM 目標數量。

如要選取特定區域,或是要在具有少於或超過三個區域的地區中建立 VM,請在要求中加入 distributionPolicy 屬性並提供區域清單。將 ZONE 替換為要在其中建立 VM 的可用區名稱。

POST https://compute.googleapis.com/compute/v1/projects/PROJECT_ID/regions/REGION/instanceGroupManagers

{
  "baseInstanceName": "BASE_INSTANCE_NAME",
  "instanceTemplate": "INSTANCE_TEMPLATE_URL",
  "name": "INSTANCE_GROUP_NAME",
  "targetSize": "TARGET_SIZE",
  "distributionPolicy": {
     "zones": [
       {"zone": "zones/ZONE"},
       {"zone": "zones/ZONE"}
      ]
   }
}

舉例來說,以下指令會建立名為 example-rmig 的區域 MIG,並將 10 個代管執行個體分布在 us-east1-bus-east1-c 區域中:

POST https://compute.googleapis.com/compute/v1/projects/myproject/regions/us-east1/instanceGroupManagers

{
  "instanceTemplate": "projects/my-project/regions/us-east1/instanceTemplates/1234567890",
  "name": "example-rmig",
  "targetSize": 10,
  "distributionPolicy": {
      "zones": [
        {"zone": "zones/us-east1-b"},
        {"zone": "zones/us-east1-c"}
      ]
   }
}

各項政策和動作都可能影響到群組中的執行個體,視您設定或處理 MIG 的方式而定。如要判斷哪些代管執行個體已設定和執行,請參閱檢查代管執行個體狀態一文。

如果各區域中沒有足夠的容量可支援群組的 VM,則 Compute Engine 會盡可能建立最多的 VM,並在有額外容量可用時繼續嘗試建立其餘 VM。

如果您沒有在要求中明確指定個別區域,Compute Engine 就會自動選擇三個區域來建立 VM。如果您需要在超過或少於三個的區域中建立 VM,或是想選擇要使用的區域,可以在要求中提供區域清單。詳情請參閱「區域選取」一節。

各可用區的 VM 分布

根據預設,地區性 MIG 會將 VM 平均分配到所選可用區。由於您要建立的是區域性 MIG,請注意,某些資源是屬於區域的,例如 GPU 和現有的永久磁碟。如果 MIG 的執行個體範本指定區域資源,您必須確保所有資源都存在於所有所選區域中,這樣才能將這些資源連結至由區域 MIG 建立的 VM。或者,如果您想讓群組為您檢查區域資源是否存在,可以設定 MIG 的目標分配型態,只在含有這些資源的區域中建立執行個體。

如需詳細資訊,請參閱下列文件:

主動式執行個體重新分配

根據預設,系統會啟用主動式執行個體重新分配功能。如果您需要手動管理每個區域中的 VM 數量,或是需要將 MIG 的目標分配型態設為 BALANCEDANY_SINGLE_ZONE,則必須停用主動式執行個體重新分配功能。

如需詳細資訊,請參閱下列文件:

後續步驟