本頁面說明如何在 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。使用 Spot Pod 時,Autopilot 會自動排程及管理 Spot VM 上的工作負載。
限制
- kubelet 節點正常關機功能僅適用於執行 GKE 1.20 以上版本的叢集。如果是 1.20 之前的 GKE 版本,您可以使用 Kubernetes on GCP Node Termination Event Handler,在 Spot VM 遭到搶占時正常終止 Pod。
- Spot VM 不支援 Windows Server 節點集區。
事前準備
開始之前,請確認你已完成下列工作:
- 啟用 Google Kubernetes Engine API。 啟用 Google Kubernetes Engine API
- 如要使用 Google Cloud CLI 執行這項工作,請安裝並初始化 gcloud CLI。如果您先前已安裝 gcloud CLI,請執行
gcloud components update
,取得最新版本。
建立具有 Spot VM 的叢集
您可以使用 Google Cloud CLI 或 Google Cloud 控制台,透過 Spot VM 建立新叢集。
gcloud
建立新叢集,在預設節點集區中使用 Spot VM,而非標準 VM:
gcloud container clusters create CLUSTER_NAME \
--spot
將 CLUSTER_NAME
替換為新叢集的名稱。
控制台
如要建立包含節點集區的新叢集 (使用 Spot VM),請執行下列步驟:
前往 Google Cloud 控制台的「Google Kubernetes Engine」頁面。
按一下「add_box Create」(建立)。
在「建立叢集」對話方塊中,點選「GKE Standard」旁的「設定」。
在導覽選單的「節點集區」部分,按一下要設定的節點集區名稱,然後按一下「節點」。
勾選「啟用 Spot VM」核取方塊。
視需要設定叢集,然後按一下「建立」。
建立具有 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
將 POOL_NAME
替換為新節點集區的名稱。
控制台
如要使用 Spot VM 建立新的節點集區,請執行下列步驟:
前往 Google Cloud 控制台的「Google Kubernetes Engine」頁面。
在叢集清單中,按一下您要修改的叢集名稱。
按一下
「Add node pool」(新增節點集區)。按一下導覽選單中的「節點」。
勾選「啟用 Spot VM」核取方塊。
視需要設定節點集區,然後按一下「建立」。
在 Spot VM 上排定工作負載
GKE 會將 cloud.google.com/gke-spot=true
和 cloud.google.com/gke-provisioning=spot
(適用於執行 GKE 1.25.5-gke.2500 以上版本的節點) 標籤新增至使用 Spot VM 的節點。您可以使用 Pod 規格中的 nodeSelector
欄位或節點親和性,篩選這個標籤。
在下列範例中,您會建立具有兩個節點集區的叢集,其中一個節點集區使用 Spot VM。接著,您可以使用 nodeSelector
,在 Spot VM 上部署無狀態 nginx
應用程式,藉此控管 GKE 放置 Pod 的位置。
使用標準 VM 建立預設節點集區的新叢集:
gcloud container clusters create CLUSTER_NAME
將
CLUSTER_NAME
替換為新叢集的名稱。取得叢集的憑證:
gcloud container clusters get-credentials CLUSTER_NAME
使用 Spot VM 建立節點集區:
gcloud container node-pools create POOL_NAME \ --num-nodes=1 \ --spot
將
POOL_NAME
替換為新節點集區的名稱。將下列資訊清單儲存為名為
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。將資訊清單套用至叢集:
kubectl apply -f pi-app.yaml
描述 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 上。
如要建立節點集區,並讓節點使用 Spot VM 且具有節點汙點,請在建立節點集區時使用
--node-taints
標記:gcloud container node-pools create POOL_NAME \ --node-taints=cloud.google.com/gke-spot="true":NoSchedule --spot
如要將相應的容許條件新增至要排程至 Spot VM 的 Pod,請修改部署作業,並在 Pod 規格中新增下列內容:
tolerations: - key: cloud.google.com/gke-spot operator: Equal value: "true" effect: NoSchedule
GKE 只會將具有這項容許條件的 Pod 排定至已新增節點 Taint 的 Spot VM。