建立具備有狀態磁碟的 MIG


本文說明如何建立代管執行個體群組 (MIG),針對 MIG 的所有 VM,將資料保留在具有特定裝置名稱的磁碟中,即使重新建立 VM 也是如此。舉例來說,如果 MIG 中的 VM 經過自動修復、更新或重新建立,資料仍會保留在磁碟中。保留磁碟對某些工作負載很有幫助,例如資料庫或舊版應用程式。

除了為群組中的所有 VM 保留磁碟外,您還可以為下列項目設定有狀態的 MIG:

  • 您可以新增及保留執行個體專屬的磁碟。
  • 您可以新增及保留執行個體專屬的中繼資料。
  • 您可以新增及保留 IP 位址。

詳情請參閱有狀態 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 有有狀態設定,就無法使用自動調度資源功能。
  • 如果您想使用自動滾動更新,請務必將替換方法設為 RECREATE
  • 針對有狀態的地區 MIG,您必須停用主動式重新分配功能 (將重新分配類型設為 NONE),才能防止自動跨區重新分配功能刪除有狀態的執行個體。
  • 如果您使用所有執行個體設定覆寫執行個體範本屬性,就無法在任何個別執行個體設定中指定這些屬性,同時在群組的所有執行個體設定中指定這些屬性。

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

建立具備有狀態磁碟的 MIG

使用 Google Cloud 控制台gcloud CLI、Terraform 或 REST

主控台

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

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

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

  2. 按一下「Create instance group」(建立執行個體群組)
  3. 選取「New managed instance group (stateful)」(新增代管執行個體群組 (有狀態)) 選項。
  4. 指定執行個體群組的名稱,並自行選擇是否提供相關說明。
  5. 為執行個體群組選擇執行個體範本,或建立新的範本
  6. 「Number of instances」(執行個體數量) 欄位中,指定這個群組所需的 VM 初始數量。
  7. 「Stateful configuration」(有狀態設定) 區段的「Group config」(群組設定) 下方,按一下要設為有狀態的磁碟,然後執行下列步驟。
    1. 在「Stateful」(有狀態) 區段中,選取「Yes」(是)
    2. 在「On permanent instance deletion」(永久刪除執行個體時) 清單中,選擇在永久刪除 VM 時要卸離磁碟還是刪除磁碟。
    3. 如要儲存設定,請按一下「Done」(完成)
  8. 如要建立 MIG,請按一下「Create」(建立)

gcloud

如要指定在建立 MIG 時,哪些執行個體範本磁碟應為有狀態,請使用 --stateful-disk 標記搭配 gcloud compute instance-groups managed create 指令:

gcloud compute instance-groups managed create INSTANCE_GROUP_NAME \
    --template INSTANCE_TEMPLATE \
    --size SIZE \
    --stateful-disk device-name=DEVICE_NAME[,auto-delete=DELETE_RULE]

更改下列內容:

  • INSTANCE_GROUP_NAME:要建立的受管理執行個體群組名稱。
  • 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
  • SIZE:這個群組所需的 VM 初始數量。
  • DEVICE_NAME:在執行個體範本中指定的磁碟裝置名稱。
  • DELETE_RULE:這個值會規定在刪除 VM 時,有狀態磁碟應發生的情況。可用的選項如下:

    • never:(預設)。請勿刪除磁碟,而是在刪除 VM 時卸離磁碟。
    • on-permanent-instance-deletion:當 VM 執行個體從執行個體群組中永久刪除時,系統會刪除磁碟,例如手動刪除代管執行個體,或是群組大小減少時。

    無論刪除規則的值為何,VM 自動修復、更新和重新建立作業一律會保留有狀態磁碟。

Terraform

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

如要指定在建立 MIG 時,哪些磁碟應具有狀態,請加入 stateful_disk 區塊。以下範例會建立具備有狀態磁碟的區域 MIG。如要進一步瞭解在範例中使用的資源,請參閱 google_compute_instance_group_manager 資源

resource "google_compute_instance_group_manager" "default" {
  name               = "igm-stateful-disk-basic"
  zone               = "us-central1-f"
  base_instance_name = "instance"
  target_size        = 1

  version {
    instance_template = google_compute_instance_template.default.id
  }

  stateful_disk {
    device_name = "example-disk"
    delete_rule = "NEVER"
  }

}

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

REST

如要指定哪些執行個體範本磁碟應在 MIG 建立時具有狀態,請在要求主體的 statefulPolicy 欄位中加入這些磁碟。如果是區域 MIG,請使用 instanceGroupManagers.insert 方法;如果是區域 MIG,請使用 regionInstanceGroupManagers.insert 方法

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

{
  "name": "NAME",
  "versions": [
    {
      "instanceTemplate": "global/instanceTemplates/TEMPLATE"
    }
  ],
  "targetSize": SIZE,
  "statefulPolicy": {
    "preservedState": {
      "disks": {
        "DEVICE_NAME": {"autoDelete": "DELETE_RULE" },
        "DEVICE_NAME": {"autoDelete": "DELETE_RULE" }
      }
    }
  }
}

更改下列內容:

  • PROJECT:要求的專案 ID。
  • ZONE:MIG 所在的區域 (適用於可用區 MIG)。
    • 如果是區域 MIG,請將 zones/ZONE 替換為 regions/REGION,並指定 MIG 的區域。
  • NAME:要建立的 MIG 名稱。
  • TEMPLATE:建立執行個體時要使用的執行個體範本名稱。
  • SIZE:這個群組中所需的初始執行個體數量。
  • DEVICE_NAME:在執行個體範本中指定的磁碟裝置名稱。
  • DELETE_RULE:這個值會規定在刪除 VM 執行個體時,應如何處理具有狀態的磁碟。可用的選項如下:

    • never:(預設值)。請勿刪除磁碟,而是在刪除 VM 時卸離磁碟。
    • on_permanent_instance_deletion:當其 VM 從執行個體群組中永久刪除時,刪除具有狀態的磁碟,例如手動刪除代管執行個體或縮減群組大小時。

    無論刪除規則的值為何,系統在執行個體自動修復、更新和重新建立作業時,一律會保留有狀態磁碟。

後續步驟