新增及移除 MIG 中的 VM


建立代管執行個體群組 (MIG) 後,您可能會想在該群組中新增或移除 VM,以滿足工作負載需求。如果您已達到可新增至 MIG 的預設 VM 數量上限,但需要更多 VM,可以增加 MIG 的大小限制。

如要在 MIG 中新增或移除 VM,請按照下列步驟操作:

事前準備

  • 如果尚未設定,請先設定驗證機制。驗證是指驗證身分,以便存取 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 中新增及移除 VM

您可以設定代管執行個體群組,根據 VM 的工作負載自動新增或移除 VM。您的應用程式可緩解流量的增加,並且可在運算資源需求降低時節省成本。如要開始調度代管執行個體群組,請參閱「建立具備自動調度資源功能的 MIG」。

詳情請參閱「自動調度執行個體群組資源」。

手動設定 MIG 大小

若代管執行個體群組尚未設定為自動調度資源,您可手動調整群組大小,來變更群組中的執行個體數。如果您增加大小,代管執行個體群組會使用目前執行個體範本新增執行個體。如果您減少大小,代管執行個體群組會從群組中刪除 VM。群組會先刪除 currentActionDELETINGCREATINGRECREATING 的執行個體,然後再刪除正在無排程動作的情況下執行的執行個體。

如果群組屬於後端服務的一部分,且該服務已啟用連線排除,在連線排除持續時間過後,最長可能需要等待 60 秒才能移除或刪除 VM 執行個體。

主控台

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

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

  2. 在清單的「Name」(名稱) 欄底下,找到要變更群組大小的執行個體群組並按一下其名稱。

  3. 按一下「編輯」,修改這個代管執行個體群組。

  4. 在「Number of instances」(執行個體數) 底下,指定您希望這個代管執行個體群組包含的執行個體數。若已啟用「Autoscaling」(自動調度資源),群組會視需要自動新增或移除執行個體。但是,您可以變更「Minimum number of instances」(執行個體數量下限) 與「Maximum number of instances」(執行個體數量上限) 值,透過自動配置器間接調整群組大小。

  5. 按一下 [Save] (儲存),套用新範本。

gcloud

使用 resize 指令

gcloud compute instance-groups managed resize INSTANCE_GROUP_NAME \
    --size NEW_SIZE \
    --zone ZONE

REST

針對區域區域 MIG 資源呼叫 resize 方法。將新執行個體群組大小指定為參數。

POST https://compute.googleapis.com/compute/v1/projects/PROJECT_ID/zones/ZONE/instanceGroupManagers/INSTANCE_GROUP_NAME/resize?size=NEW_SIZE

如果該執行個體群組是地區代管執行個體群組,請將 zones/ZONE 替換成 regions/REGION

提出調整代管執行個體群組大小的要求之後,VM 會在系統可以佈建或刪除時立即開始運作或停止。此程序可能會花費大量時間,花費時間視群組中的執行個體數而定。 在您的代管執行個體群組中驗證執行個體狀態

調整大小時停用建立重試模式

根據預設,如果初始建立虛擬機器執行個體失敗,代管執行個體群組會繼續重試建立虛擬機器,直到成功建立虛擬機器為止。不過,如果您不想使用自動建立重試,可在調整執行個體群組大小時停用建立重試模式。在這個模式下,代管執行個體群組只會進行一次嘗試,來建立所有執行個體。如果在建立執行個體期間發生錯誤,代管執行個體群組會放棄這個執行個體,將其從群組的代管執行個體清單中移除,並減少代管執行個體群組的目標大小。

只有在第一次嘗試建立 VM 時,才會套用這個模式。如果在啟用這個模式時成功建立 VM,VM 的行為會與使用一般調整大小要求建立的其他所有 VM 一樣。特別是,如果執行中的 VM 在日後意外中斷,且需要重新建立,這個模式不會影響在這種情況下重新建立的行為。

如果您的系統會自動建立不需要確切 VM 數量的 VM 群組,停用建立重試模式將特別有用。您可能會希望快速穩定代管執行個體群組的大小,並靈活調整群組中的 VM 數量,而不是無限期等待所有要求的執行個體建立完成,因為這可能會因為配額錯誤或其他不相關的問題,而導致暫時或永久延遲。

限制

  • 您無法為使用 EVEN 目標分配形狀的區域性 MIG 停用建立重試功能。
  • 如果是採用 BALANCEDANY_SINGLE_ZONE 目標分配型態的區域 MIG,您必須先停用主動式執行個體重新分配功能,才能停用建立重試模式。
  • 您無法停用自動調度資源的 MIG 的建立重試功能。

如要在建立重試模式停用時調整代管執行個體群組,請使用 Google Cloud CLI 或 REST。

gcloud

使用 beta resize 指令,並加上 --no-creation-retries 標記。

gcloud beta compute instance-groups managed resize INSTANCE_GROUP_NAME \
    --size NEW_SIZE \
    --no-creation-retries \
    [--region REGION | --zone ZONE]

更改下列內容:

  • INSTANCE_GROUP_NAME:您要調整大小的 MIG 名稱。
  • NEW_SIZE:代管執行個體群組中執行中的執行個體目標數量。
  • ZONE:如果是區域性 MIG,則為 MIG 所在的區域。
  • REGION:如果是區域 MIG,則為 MIG 所在的區域。

REST

如果是區域 MIG,請使用 beta instanceGroupManagers.resizeAdvanced 方法;如果是區域 MIG,請使用 beta regionInstanceGroupManagers.resizeAdvanced 方法。在要求主體中指定新的群組大小和 noCreationRetries 欄位。

舉例來說,請發出下列呼叫,以便在建立重試模式停用時調整區域 MIG 的大小。

POST https://compute.googleapis.com/compute/beta/projects/PROJECT_ID/zones/ZONE/instanceGroupManagers/INSTANCE_GROUP_NAME/resizeAdvanced

{
 "targetSize": NEW_SIZE,
 "noCreationRetries": true
}

更改下列內容:

  • PROJECT_ID:您的專案 ID。
  • ZONE:MIG 所在區域。如果是區域 MIG,請將 zones/ZONE 替換為 regions/REGION,並指定區域。詳情請參閱「關於區域 MIG」。
  • INSTANCE_GROUP_NAME:您要調整大小的 MIG 名稱。
  • NEW_SIZE:代管執行個體群組中執行中的執行個體目標數量。

    您可以使用 listManagedInstances 方法查看正在建立的執行個體以及執行的模式。在建立重試模式停用時,您建立的執行個體會具有 CREATING_WITHOUT_RETRIEScurrentAction

新增、移除或放棄特定例項

在 MIG 中使用指定名稱建立執行個體

如果您有依賴特定名稱的系統,可以將具有特定名稱的 VM 新增至現有 MIG。

如果 MIG 重新建立 VM,您指派給這些代管執行個體的名稱會保留下來。如要進一步瞭解如何保留 MIG 執行個體的狀態,請參閱「有狀態的 MIG」。

限制

如果您建立具有特定名稱的執行個體:

  • 您必須在建立執行個體的過程中關閉自動調度資源功能。
  • 您必須將 MIG 的更新政策設為「投機」
  • 如果您要更新區域性 MIG:
  • 您無法在專案中使用重複的 VM 執行個體名稱。

您可以使用 gcloud CLIREST 將特定名稱的 VM 新增至現有的 MIG。

gcloud

使用 instance-groups managed create-instance 指令

gcloud compute instance-groups managed create-instance INSTANCE_GROUP_NAME \
    --instance INSTANCE_NAME \
    [--region REGION | --zone ZONE]

更改下列內容:

  • INSTANCE_GROUP_NAME 是您要新增新執行個體的區域或區域 MIG 名稱。
  • INSTANCE_NAME 是您要指派給新執行個體的名稱。

REST

請向 regionInstanceGroupManagers.createInstances 方法傳送 POST 要求。對於區域代管執行個體群組,請使用 instanceGroupManagers.createInstances 方法。在要求主體中,包含 instances 欄位,其中包含一或多個執行個體的名稱。

POST https://compute.googleapis.com/compute/v1/projects/PROJECT_ID/regions/REGION/instanceGroupManagers/INSTANCE_GROUP_NAME/createInstances
{
  "instances": [
    {
      "name": "INSTANCE_NAME_1"
    },
    {
      "name": "INSTANCE_NAME_2"
    }
  ]
}

刪除群組中的特定執行個體

您可以刪除代管執行個體群組中的個別 VM 執行個體。刪除執行個體會縮減執行個體群組的指定 targetSize,並且會將 VM 從所屬的任何目標集區中移除。

從代管執行個體群組中刪除特定執行個體,並不會變更任何指定的自動配置器設定。如果您從代管執行個體群組刪除執行個體,自動配置器可能會偵測到群組中其他執行個體的工作負載增加,並將群組大小重新增加到先前的等級。為避免發生此問題,請先停止自動配置器,然後再嘗試刪除執行個體。

如果群組屬於後端服務的一部分,且該服務已啟用連線排除,在連線排除持續時間過後,最長可能需要等待 60 秒才能移除或刪除 VM 執行個體。

主控台

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

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

  2. 在清單的「Name」(名稱) 欄底下,按一下個別待刪除執行個體所屬執行個體群組的名稱。按一下名稱會開啟頁面,其中包含執行個體群組的屬性及群組內執行個體的清單。

  3. 在「成員」部分,從執行個體清單中選取一或多個要刪除的執行個體。

  4. 按一下「Delete instance」。即可刪除您所選取的執行個體。

gcloud

如要使用 gcloud CLI 刪除執行個體,請使用 instance-groups managed delete-instances 指令

gcloud compute instance-groups managed delete-instances INSTANCE_GROUP_NAME \
    --instances INSTANCE_NAME_1,INSTANCE_NAME_2 \ 
    [--region REGION | --zone ZONE]

指令會傳回每個執行個體的作業狀態:

  • 如果 gcloud CLI 刪除執行個體,系統會傳回 SUCCESS
  • 如果您在要求中指定的任何執行個體並非群組成員、任何成員已遭到刪除或捨棄,或是要求因其他原因失敗,則系統會針對所有執行個體傳回 FAIL
  • 只有在您使用 --skip-instances-on-validation-error 標記,且執行個體不是群組成員,或已刪除或放棄時,系統才會傳回 SKIPPED
  • 如果 gcloud CLI 無法從執行個體名稱解析區域,就只會針對區域群組傳回 MEMBER_NOT_FOUND

如果您預期刪除要求可能與並行作業相衝突,請使用 --skip-instances-on-validation-error 標記。這個旗標會指定即使要求包含非群組成員的執行個體,或已刪除或放棄的執行個體,要求是否應繼續進行,例如由自動調整器刪除的執行個體。根據預設,如果您省略這個標記,且要求中指定了這類執行個體,作業就會失敗。如果要求包含格式不正確的執行個體名稱,或是參照的執行個體位於群組區域或地區以外的區域或地區,作業一律會失敗。

舉例來說,下列指令要求刪除四個執行個體,但其中三個已不在群組中:

gcloud compute instance-groups managed delete-instances example-mig \
    --instances=example-instance-1,example-instance-2,example-instance-3,example-instance-4 \
    --region=us-central1 \
    --skip-instances-on-validation-error

在這個範例中,有三個例項未通過驗證,因此遭到略過,而 example-instance-3 則遭到刪除:

Updated [https://www.googleapis.com/compute/v1/projects/example-project/regions/us-central1/instanceGroupManagers/example-mig].
PROJECT          ZONE           INSTANCE               STATUS   VALIDATION_ERROR
example-project  us-central1-a  example-instance-1     SKIPPED  Cannot delete instance https://www.googleapis.com/compute/v1/projects/example-project/zones/us-central1-a/instances/example-instance-1 that was already deleted.
example-project  us-central1-a  example-instance-2     SKIPPED  Cannot delete instance https://www.googleapis.com/compute/v1/projects/example-project/zones/us-central1-a/instances/example-instance-2 that was already abandoned.
example-project  us-central1-a  example-instance-3     SUCCESS
example-project  us-central1-a  example-instance-4     SKIPPED  Cannot find instance https://www.googleapis.com/compute/v1/projects/example-project/zones/us-central1-a/instances/example-instance-4.

如果您省略 --skip-instances-on-validation-error 標記,且任何執行個體都未通過驗證,則系統不會刪除任何執行個體:

gcloud compute instance-groups managed delete-instances example-mig \
    --instances=example-instance-1,example-instance-2,example-instance-3,example-instance-4 \
    --region=us-central1

Updated [https://www.googleapis.com/compute/v1/projects/example-project/regions/us-central1/instanceGroupManagers/example-mig].
PROJECT          ZONE             INSTANCE              STATUS
example-project  us-central1-a    example-instance-1    FAIL
example-project  us-central1-a    example-instance-2    FAIL
example-project  us-central1-a    example-instance-3    FAIL
example-project  us-central1-a    example-instance-4    FAIL

REST

請向 regionInstanceGroupManagers.deleteInstances 方法傳送 POST 要求。對於區域代管執行個體群組,請使用 instanceGroupManagers.deleteInstances 方法。

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

{
  "instances": [
    "zones/ZONE/instances/INSTANCE_NAME_1",
    "zones/ZONE/instances/INSTANCE_NAME_2"
   ]
}

如果您預期刪除要求可能與並行作業衝突,請使用 skipInstancesOnValidationError 欄位。這個欄位會指定即使要求包含非群組成員的執行個體,或已刪除或遺棄的執行個體,要求是否仍應繼續進行,例如自動調度資源功能正在刪除的執行個體。根據預設,如果您省略這個標記,且要求中指定了這類執行個體,作業就會失敗。如果要求包含格式不正確的執行個體名稱,或是參照的執行個體位於群組區域或地區以外的區域或地區,作業一律會失敗。

舉例來說,即使其中一個執行個體已刪除或正在刪除,以下要求刪除 2 個執行個體仍會成功:

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

{
 "instances": [
  "zones/ZONE/instances/INSTANCE_NAME_1",
  "zones/ZONE/instances/INSTANCE_NAME_2"
 ],
 "skipInstancesOnValidationError": true
}

您可以查看作業,瞭解作業中略過的執行個體。如果是區域 MIG,請使用 regionOperations.get 方法。如果是區域 MIG,請使用 zoneOperations.get 方法。警告會列在操作的警告欄位中。

提出刪除代管執行個體群組中執行個體的要求之後,執行個體會在系統可以刪除時立即停止。此程序可能會花費大量時間,花費時間視您從群組中刪除的執行個體數而定。 驗證群組的狀態驗證代管執行個體的狀態

從 MIG 捨棄執行個體

您可以將 VM 執行個體與代管執行個體群組區分開,以更輕鬆地找出個別執行個體的問題,而不會影響整個群組。從群組捨棄執行個體也會將執行個體從指派給代管執行個體群組的負載平衡器移除。 手動指派給特定個別執行個體的目標集區不會移除。

您無法將已放棄的 VM 例項加回群組。捨棄執行個體會減少執行個體群組的指定 targetSize,但不會變更任何指定的自動配置器設定。具有自動配置器的代管執行個體群組會視需要繼續自動新增或移除執行個體。如果地區 MIG 的自動調度資源設為 ONLY_SCALE_OUT 模式,當您放棄 VM 執行個體,將 MIG 目標大小縮減至自動配置器建議的大小,自動配置器可能會在放棄的 VM 所在的區域中建立新的 VM 執行個體。在這種情況下,為避免建立新的 VM 執行個體,請嘗試放棄來自其他區域的 VM 執行個體。

如果群組屬於後端服務的一部分,且該服務已啟用連線排除,在連線排除持續時間過後,最長可能需要等待 60 秒才能移除或刪除 VM 執行個體。

主控台

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

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

  2. 確認您要移除哪些執行個體,然後在清單的「Name」(名稱) 欄底下,按一下其所屬執行個體群組的名稱。按一下名稱會開啟頁面,其中包含執行個體群組的屬性及群組內執行個體的清單。

  3. 在「Members」區段中,從執行個體清單中選取一或多個要從群組移除的執行個體。

  4. 按一下 [Remove from group] (從群組中移除)。所選執行個體會退出群組,但仍會繼續在群組外執行。

gcloud

如要從執行個體群組中移除執行個體,「而不刪除 VM」,請使用 instance-groups managed abandon-instances 指令

gcloud compute instance-groups managed abandon-instances INSTANCE_GROUP_NAME \
    --instances INSTANCE_NAME_1,INSTANCE_NAME_2 \
    [--region REGION | --zone ZONE]

REST

請向 regionInstanceGroupManagers.abandonInstances 方法傳送 POST 要求。對於區域代管執行個體群組,請使用 instanceGroupManagers.abandonInstances 方法。

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

{
 "instances": [
  "zones/ZONE/instances/INSTANCE_NAME_1",
  "zones/ZONE/instances/INSTANCE_NAME_2"
 ]
}

提出從代管執行個體群組中捨棄執行個體的要求之後,群組會盡快移除執行個體。 驗證群組的狀態驗證代管執行個體的狀態

提高群組人數上限

根據預設,區域 MIG 的大小限制為 1,000 個 VM,區域性 MIG 的大小限制則為 2,000 個 VM。您可以進一步將這些限制提高至 2,000 個可用區 MIG VM,以及 4,000 個區域 MIG VM。

如要提高限制,請將群組的 listManagedInstancesResults 欄位設為 PAGINATED。如果您仍需要在群組中建立更多 VM,請建立多個 MIG 或與支援團隊聯絡

主控台

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

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

  2. 在清單的「Name」(名稱) 欄底下,按一下要增加大小限制的執行個體群組名稱。

  3. 按一下「編輯」來修改執行個體群組。

  4. 按一下「顯示進階設定」展開進階設定部分。

  5. 在「Managed instance list API call results」下方,選取「Paginated」選項。

  6. 點選「儲存」來套用變更。

gcloud

如果是現有的 MIG,請使用 update 指令

gcloud compute instance-groups managed update INSTANCE_GROUP_NAME \
    --list-managed-instances-results=PAGINATED

針對新的 MIG,請使用 create 指令

gcloud compute instance-groups managed create INSTANCE_GROUP_NAME \
    --template INSTANCE_TEMPLATE \
    --size SIZE \
    --list-managed-instances-results=PAGINATED

Terraform

如要提高 MIG 的大小限制,請將 list_managed_instances_results 引數設為 PAGINATED

以下範例會提高區域性 MIG 的大小限制。如要進一步瞭解範例中使用的資源,請參閱 google_compute_instance_group_manager 資源。如果是區域 MIG,請使用 google_compute_region_instance_group_manager 資源

resource "google_compute_instance_group_manager" "default" {
  name                           = "my-igm"
  base_instance_name             = "test"
  target_size                    = 7
  zone                           = "us-central1-f"
  list_managed_instances_results = "PAGINATED"
  version {
    instance_template = google_compute_instance_template.default.id
    name              = "primary"
  }
}

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

REST

如果是現有的區域 MIG,請使用 instanceGroupManagers.patch 方法;如果是地區 MIG,請使用 regionInstanceGroupManagers.patch 方法,例如:

PATCH https://compute.googleapis.com/compute/v1/projects/PROJECT_ID/region/REGION/regionInstanceGroupManagers/INSTANCE_GROUP_NAME

{
  "listManagedInstancesResults": "PAGINATED",
}

如果是新的區域 MIG,請使用 instanceGroupManagers.insert 方法;如果是區域 MIG,請使用 regionInstanceGroupManagers.insert 方法

如果您想切換回無分頁的清單結果,也必須將群組還原為預設大小限制。如要切換回來,請先確認群組未超過預設大小限制,然後將 listManagedInstancesResults 欄位設為 PAGELESS

後續步驟