使用 Spot VM 以較低的費用執行容錯工作負載

本頁面說明如何在 Google Kubernetes Engine (GKE) 叢集和節點集區中使用 Spot VM,以較低的成本執行容錯、無狀態或批次工作負載。

總覽

Spot VM 是 Compute Engine 虛擬機器 (VM),價格低於預設標準 VM,但不保證可用性。Spot VM 提供的機器類型和選項與標準 Compute Engine VM 相同。如果系統事件 (例如標準 VM 需要資源) 導致 Compute Engine 必須回收 Spot VM,

如要進一步瞭解 GKE 中的 Spot VM,請參閱「Spot VM」。

Spot VM 可取代先占 VM,執行無狀態、批次或容錯工作負載。先占 VM 會在 24 小時後過期,但 Spot VM 沒有過期時間。當 Compute Engine 需要資源來執行標準 VM 時,就會終止 Spot VM。

GKE Autopilot 叢集也支援透過Spot Pod 使用 Spot VM。Autopilot 會使用 Spot Pod,自動排程及管理 Spot VM 上的工作負載。

限制

  • Spot VM 不支援 Windows Server 節點集區。

事前準備

開始之前,請確認您已完成下列工作:

  • 啟用 Google Kubernetes Engine API。
  • 啟用 Google Kubernetes Engine API
  • 如要使用 Google Cloud CLI 執行這項工作,請安裝初始化 gcloud CLI。如果您先前已安裝 gcloud CLI,請執行 gcloud components update 指令,取得最新版本。較舊的 gcloud CLI 版本可能不支援執行本文件中的指令。

在 GKE 中佈建 Spot VM

您可以建立叢集,並將 Spot VM 用於預設節點集區,即可佈建 Spot VM。或者,您也可以建立使用 Spot VM 的節點集區。

使用 gcloud CLI 設定 Spot VM 正常關機的參數。如要進一步瞭解如何設定正常關機參數 (這項功能已在搶先版中推出),請參閱「Spot VM 的終止和正常關機」一文。

建立具有 Spot VM 的叢集

您可以使用 Google Cloud CLI 或 Google Cloud 控制台,建立使用 Spot VM 的新叢集。

gcloud

建立新的叢集,在預設節點集區中使用 Spot VM,而非標準 VM:

gcloud container clusters create CLUSTER_NAME \
    --spot
    [--system-config-from-file=SYSTEM_CONFIG_PATH]

CLUSTER_NAME 替換為新叢集的名稱。

如要為預設節點集區設定正常關機的參數 (預覽版),請將 SYSTEM_CONFIG_PATH 替換為包含 kubelet 設定的檔案路徑。詳情請參閱「在 GKE 中正常終止 Spot VM」一文。您也可以在建立節點集區後更新這些參數,但 GKE 必須重新建立節點,變更才會生效。

控制台

如要建立包含節點集區的新叢集 (使用 Spot VM),請執行下列步驟:

  1. 前往 Google Cloud 控制台的「Create a Kubernetes cluster」(建立 Kubernetes 叢集) 頁面。

    前往「建立 Kubernetes 叢集」

  2. 在導覽選單的「節點集區」部分,按一下要設定的節點集區名稱,然後按一下「節點」

  3. 勾選「啟用 Spot VM」核取方塊。

  4. 視需要設定叢集,然後按一下「建立」

建立具有 Spot VM 的節點集區

您可以使用 gcloud CLI 或 Google Cloud 控制台,建立具有 Spot VM 的新節點集區。您只能在新節點集區中啟用 Spot VM。您無法在現有節點集區中啟用或停用 Spot VM。

gcloud

使用 Spot VM 建立新的節點集區:

gcloud container node-pools create POOL_NAME \
    --cluster=CLUSTER_NAME \
    --spot
    [--system-config-from-file=SYSTEM_CONFIG_PATH]

POOL_NAME 替換成新節點集區的名稱。

如要設定正常關機的參數 (預覽),請將 SYSTEM_CONFIG_PATH 替換為包含 kubelet 設定的檔案路徑。詳情請參閱「在 GKE 中正常終止 Spot VM」。您也可以在建立節點集區後更新這些參數,但 GKE 必須重新建立節點,變更才會生效。

控制台

如要使用 Spot VM 建立新的節點集區,請執行下列步驟:

  1. 前往 Google Cloud 控制台的「Google Kubernetes Engine」頁面。

    前往「Google Kubernetes Engine」

  2. 在叢集清單中,按一下您要修改的叢集名稱。

  3. 按一下「Nodes」(節點) 分頁標籤。

  4. 按一下「建立由使用者管理的節點集區」

  5. 按一下導覽選單中的「節點」

  6. 勾選「啟用 Spot VM」核取方塊。

  7. 視需要設定節點集區,然後按一下「建立」

在 Spot VM 上排定工作負載

GKE 會將 cloud.google.com/gke-spot=truecloud.google.com/gke-provisioning=spot (適用於執行 GKE 1.25.5-gke.2500 以上版本的節點) 標籤新增至使用 Spot VM 的節點。您可以在 Pod 規格中使用 Pod 規格中的 nodeSelector 欄位或節點親和性,篩選這個標籤。

在下列範例中,您會建立具有兩個節點集區的叢集,其中一個節點集區使用 Spot VM。接著,您可以使用 nodeSelector 控制 GKE 放置 Pod 的位置,將無狀態 nginx 應用程式部署到 Spot VM 上。

  1. 使用標準 VM 建立預設節點集區的新叢集:

    gcloud container clusters create CLUSTER_NAME
    

    CLUSTER_NAME 替換為新叢集的名稱。

  2. 取得叢集的憑證:

    gcloud container clusters get-credentials CLUSTER_NAME
    
  3. 使用 Spot VM 建立節點集區:

    gcloud container node-pools create POOL_NAME \
        --num-nodes=1 \
        --spot
    

    POOL_NAME 替換為新節點集區的名稱。

  4. 將下列資訊清單儲存為名為 pi-app.yaml 的檔案:

    apiVersion: batch/v1
    kind: Job
    metadata:
      name: pi
    spec:
      template:
        metadata:
          labels:
            app: pi
        spec:
          nodeSelector:
            cloud.google.com/gke-spot: "true"
          terminationGracePeriodSeconds: 25
          containers:
          - name: pi
            image: perl:5.34.0
            command: ["perl",  "-Mbignum=bpi", "-wle", "print bpi(2000)"]
          restartPolicy: Never
      backoffLimit: 4
    

    在這個資訊清單中,nodeSelector 欄位會告知 GKE,只在採用 Spot VM 的節點上排定 Pod。

  5. 將資訊清單套用至叢集:

    kubectl apply -f pi-app.yaml
    
  6. 描述 Pod:

    kubectl describe pod pi
    

    輸出結果會與下列內容相似:

    Name:         pi-kjbr9
    Namespace:    default
    Priority:     0
    Node:         gke-cluster-2-spot-pool-fb434072-44ct
    ...
    Labels:       app=pi
                  job-name=pi
    Status:       Succeeded
    ...
    Controlled By:  Job/pi
    Containers:
    ...
    Conditions:
      Type              Status
      Initialized       True 
      Ready             False 
      ContainersReady   False 
      PodScheduled      True 
    Volumes:
    ...
    Node-Selectors:              cloud.google.com/gke-spot=true
    Tolerations:                 node.kubernetes.io/not-ready:NoExecute op=Exists for 300s
                                node.kubernetes.io/unreachable:NoExecute op=Exists for 300s
    Events:
      Type    Reason     Age    From               Message
      ----    ------     ----   ----               -------
      Normal  Scheduled  4m3s   default-scheduler  Successfully assigned default/pi-kjbr9 to gke-cluster-2-spot-pool-fb434072-44ct
      Normal  Pulling    4m2s   kubelet            Pulling image "perl:5.34.0"
      Normal  Pulled     3m43s  kubelet            Successfully pulled image "perl:5.34.0" in 18.481761978s
      Normal  Created    3m43s  kubelet            Created container pi
      Normal  Started    3m43s  kubelet            Started container pi
    

    Node 欄位顯示 GKE 只會在採用 Spot VM 的節點上排定 Pod。

使用 Spot VM 的 taint 和容許條件

最佳做法是建立叢集時,至少要有一個不含 Spot VM 的節點集區,以便放置 DNS 等系統工作負載。您可以使用節點 taint 和對應的容許條件,告知 GKE 避免將特定工作負載放在 Spot VM 上。

  1. 如要建立節點集區,並讓節點使用 Spot VM 且具有節點汙點,請在建立節點集區時使用 --node-taints 標記:

    gcloud container node-pools create POOL_NAME \
        --node-taints=cloud.google.com/gke-spot="true":NoSchedule
        --spot
    
  2. 如要將相應的容許條件新增至要排程至 Spot VM 的 Pod,請修改部署作業,並在 Pod 規格中新增下列內容:

    tolerations:
    - key: cloud.google.com/gke-spot
      operator: Equal
      value: "true"
      effect: NoSchedule
    

    GKE 只會將具有這項容許條件的 Pod 排程至新增節點 Taint 的 Spot VM。

後續步驟