在單一可用區中建立 MIG


本文說明如何在單一可用區中建立代管執行個體群組 (MIG)。將所有 MIG 的 VM 放入單一可用區,有助於降低特定工作負載 (例如批次工作負載) 的延遲情況。

這種 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.

    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.

    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.

    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 限制」。

在單一可用區中建立 MIG

如要在單一區域中建立 MIG,請使用 Google Cloud 控制台gcloud CLI、Terraform 或 REST

主控台

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

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

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

  2. 按一下「Create instance group」(建立執行個體群組)
  3. 如要建立有狀態的 MIG,請選取「New managed instance group (stateful)」(新增代管執行個體群組 (有狀態)) 選項。如需相關說明,請參閱「使用有狀態 MIG 的時機」。
  4. 指定執行個體群組的名稱,並自行選擇是否提供相關說明。
  5. 為執行個體群組選擇執行個體範本,或建立新的範本
  6. 指定這個群組中的 VM 數量。請務必佈建充足的 VM,以便在可用區失效時維持應用程式運作。
  7. 「Location」(位置) 中選取「Single zone」(單一可用區)
  8. 選取要建立 MIG 的「Region」(區域) 和「Zone」(可用區)。如果選擇區域執行個體範本,系統將根據範本的區域自動選取「Region」(區域)
  9. 根據預設,無狀態 MIG 的自動調度資源功能為啟用狀態。有了自動調度資源功能,群組就會依據執行個體使用率自動新增或移除執行個體
  10. 或者,您也可以啟用自動修復功能,針對群組中的 VM 執行應用程式健康狀態檢查。
  11. 按一下「Create」(建立),建立新群組。

gcloud

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

使用 instance-groups managed create 指令建立代管執行個體群組,並指定群組名稱、群組大小、執行個體範本和區域。

gcloud compute instance-groups managed create INSTANCE_GROUP_NAME \
    --size SIZE \
    --template INSTANCE_TEMPLATE \
    --zone ZONE

更改下列內容:

  • INSTANCE_GROUP_NAME:這個執行個體群組的名稱。
  • SIZE:執行個體群組的大小。
  • INSTANCE_TEMPLATE:這個群組要使用的執行個體範本名稱。針對區域執行個體範本,您必須指定範本的完整或部分網址。完整網址的範例為 https://www.googleapis.com/compute/v1/projects/example-project/regions/us-central1/instanceTemplates/example-regional-instance-template,部分網址的範例為 projects/example-project/regions/us-central1/instanceTemplates/example-regional-instance-template
  • ZONE:可用於 Compute Engine 的其中一個區域。如要將 MIG 的 VM 分散至區域內的多個可用區,請參閱「建立區域性 MIG」。

您可以選擇提供 --base-instance-name 標記。由於這些 VM 都是以通用範本為基礎,因此系統會為每個 VM 指派隨機字串,並附加在 VM 名稱中。同時會將基礎名稱加在隨機字串前。舉例來說,如果您將基礎名稱設為 test,VM 的名稱會是 test-yahstest-qtyz。如果您需要指定名稱,請參閱「在 MIG 中使用指定名稱建立執行個體」一文。

舉例來說,下列指令會建立名為 example-group 的執行個體群組,其基礎 VM 名稱為 test。這個群組包含三個執行個體:

gcloud compute instance-groups managed create example-group \
    --base-instance-name test \
    --size 3 \
    --template an-instance-template \
    --zone us-central1-f

Terraform

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

如要建立可用區 MIG,您可以使用 google_compute_instance_group_manager 資源

resource "google_compute_instance_group_manager" "default" {

  name               = "example-group"
  base_instance_name = "test"
  target_size        = 3
  zone               = "us-central1-f"

  version {
    instance_template = google_compute_instance_template.default.id
    name              = "primary"
  }
}

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

REST

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

使用 instanceGroupManagers.insert 方法建立代管執行個體群組。在要求主體中加入群組名稱、群組大小和執行個體範本的網址。

POST https://compute.googleapis.com/compute/v1/projects/PROJECT_ID/zones/ZONE/instanceGroupManagers

{
  "versions": [
    {
      "instanceTemplate": "global/instanceTemplates/INSTANCE_TEMPLATE"
    }
  ],
  "name": "INSTANCE_GROUP_NAME",
  "targetSize": SIZE
}

更改下列內容:

  • PROJECT_ID:要求的專案 ID。
  • ZONE:要求的區域。如果您想將 MIG 的 VM 分散到所屬區域的多個可用區,請將 zones/ZONE 替換為 regions/REGION,並指定區域。詳情請參閱「建立區域性 MIG」。
  • INSTANCE_GROUP_NAME:這個執行個體群組的名稱。
  • SIZE:執行個體群組的大小。
  • INSTANCE_TEMPLATE:這個群組要使用的執行個體範本。

您可以選擇提供 base-name 欄位。由於這些 VM 都是以通用範本為基礎,因此系統會為每個 VM 指派隨機字串,並附加在 VM 名稱中。同時會將基礎名稱加在隨機字串前。舉例來說,如果您將基礎名稱設為 test,VM 的名稱會是 test-yahstest-qtyz。如果您需要指定名稱,請參閱「在 MIG 中使用指定名稱建立執行個體」一文。

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

後續步驟