建立啟用自動調度資源功能的 MIG


本文說明如何建立自動調度資源的代管執行個體群組 (MIG),根據群組的平均 CPU 使用率自動新增或移除 VM。舉例來說,如果群組的 CPU 使用率偏低,群組就會自動移除 VM,以節省費用。

您可以根據各種自動調度資源信號,自動調度 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 並啟用自動調度資源功能

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

主控台

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

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

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

  2. 如果您有執行個體群組,請選取該群組,然後按一下「Edit」(編輯)。如果沒有執行個體群組,則請按一下「Create instance group」(建立執行個體群組)
  3. 如為新的執行個體群組,請指派名稱,然後為執行個體群組選擇執行個體範本,或建立新的執行個體
  4. 如果沒有自動調度資源設定,請按一下「Autoscaling」(自動調度資源) 下方的「Configure autoscaling」(設定自動調度資源功能)
  5. 「Autoscaling mode」(自動調度資源模式) 底下,選取「On: add and remove instances to the group」(開啟:在群組中新增和移除執行個體) 即可啟用自動調度資源功能。
  6. 指定您要讓自動配置器在這個群組中建立的執行個體數量下限與上限。
  7. 在「Autoscaling metrics」(自動調度資源指標) 區段中,如果目前沒有「CPU utilization」(CPU 使用率) 指標,請先新增這個指標:
    1. 按一下「Add Metric」(新增指標)
    2. 「Metric type」(指標類型) 下方,選取「CPU utilization」(CPU 使用率)
    3. 在「Target CPU utilization」(目標 CPU 使用率) 中輸入您想要的值,系統會將這個值以百分比格式處理。例如,若需 75% 的 CPU 使用率,請輸入 75
    4. 在「Predictive autoscaling」(預測式自動調度資源) 底下,選取「Off」(關閉)。如要進一步瞭解預測式自動調度資源功能,以及這項功能是否適合您的工作負載,請參閱「根據預測結果調度資源」一文。
    5. 按一下 [完成]
  8. 您可以使用「Initialization period」(初始化期間) 設定初始化期間,讓自動配置器知道應用程式初始化所需的時間。指定正確的初始化期間可提升自動配置器的決策品質。舉例來說,向外擴充時,自動配置器會忽略仍在初始化的 VM 資料,因為這些 VM 可能尚未呈現您應用程式的一般使用情形。預設的初始化週期為 60 秒。
  9. 如要建立 MIG,請按一下「Create」(建立)

gcloud

您必須先建立 MIG,才能啟用自動調度資源功能。請按照操作說明建立 VM 受限於單一可用區的 MIG,或建立 VM 分散於區域內多個可用區的 MIG

接著,使用 set-autoscaling 子指令,為群組啟用自動調度資源功能。舉例來說,下列指令會建立目標 CPU 使用率為 60% 的自動配置器。建立自動配置器時,除了 --target-cpu-utilization 參數,還需要 --max-num-replicas 參數。

您可以選擇設定 --min-num-replicas,指出群組中 VM 的最低數量。如果您未設定最小值,MIG 會將此值設為 2。

您可以使用 --cool-down-period 標記設定初始化期間,讓自動配置器知道應用程式初始化所需的時間。指定正確的初始化期間可提升自動配置器的決策品質。舉例來說,向外擴充時,自動配置器會忽略仍在初始化的 VM 資料,因為這些 VM 可能尚未呈現您應用程式的一般使用情形。預設的初始化週期為 60 秒。

gcloud compute instance-groups managed set-autoscaling example-managed-instance-group \
  --max-num-replicas 20 \
  --target-cpu-utilization 0.60 \
  --cool-down-period 90

如有需要,您可以啟用預測式自動調度資源功能,在預測負載出現前提前擴充。如要瞭解預測式自動調度資源是否適合您的工作負載,請參閱「根據預測結果調度資源」。

您可以使用 instance-groups managed describe 指令來驗證是否已成功啟用自動調度資源功能,該指令會說明對應的 MIG,並提供該群組自動調度資源功能的相關資訊:

gcloud compute instance-groups managed describe example-managed-instance-group

Terraform

您必須先建立 MIG,才能啟用自動調度資源功能。請按照操作說明建立 VM 受限於單一可用區的 MIG,或建立 VM 分散於區域內多個可用區的 MIG

如要在 MIG 中設定自動調度資源,您可以使用 google_compute_autoscaler 資源

以下範例會根據區域 MIG 中的 CPU 使用率設定自動調度資源。

resource "google_compute_autoscaler" "default" {
  name   = "example-autoscaler"
  zone   = "us-central1-f"
  target = google_compute_instance_group_manager.default.id

  autoscaling_policy {
    max_replicas    = 20
    min_replicas    = 0
    cooldown_period = 90

    cpu_utilization {
      target = 0.60
    }
  }
}

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

REST

您必須先建立 VM 受限於單一區域的 MIG,或是建立 VM 分散於區域內多個可用區的 MIG,才能啟用自動調度資源。

如果您有區域 MIG,請向 autoscalers.insert 方法發出 POST 要求。如果您有區域 MIG,請使用 regionAutoscalers.insert 方法

例如:

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

要求主體必須包含 nametargetautoscalingPolicy欄位。autoscalingPolicy 欄位必須定義目標 cpuUtilization 值和 maxNumReplicas 值。

您可以選擇設定 minNumReplicas,指出群組中 VM 的最低數量。如果您未設定最小值,MIG 會將此值設為 2。

您可以使用 coolDownPeriodSec 欄位設定初始化期間,讓自動配置器知道應用程式初始化所需的時間。指定正確的初始化期間可提升自動配置器的決策品質。舉例來說,向外擴充時,自動配置器會忽略仍在初始化的 VM 資料,因為這些 VM 可能尚未呈現您應用程式的一般使用情形。預設的初始化週期為 60 秒。

{
  "name": "example-autoscaler",
  "target": "https://www.googleapis.com/compute/v1/projects/myproject/zones/us-central1-f/instanceGroupManagers/example-managed-instance-group",
  "autoscalingPolicy": {
    "maxNumReplicas": 10,
    "cpuUtilization": {
      "utilizationTarget": 0.6
    },
    "coolDownPeriodSec": 90
  }
}

如有需要,您可以啟用預測式自動調度資源功能,在預測負載出現前提前擴充。如要瞭解預測式自動調度資源是否適合您的工作負載,請參閱「根據預測結果調度資源」。

如要進一步瞭解如何根據 CPU 使用率啟用自動調度資源功能,請參閱「根據 CPU 使用率執行資源調度」一文。

後續步驟