將 GKE Pod 放置在特定區域

本頁說明如何使用區域拓撲,指示 Google Kubernetes Engine (GKE) 在特定 Google Cloud 區域的節點上執行 Pod。這類刊登位置適用於下列情況:

  • Pod 必須存取儲存在區域 Compute Engine 永久磁碟中的資料。
  • Pod 必須與其他區域性資源 (例如 Cloud SQL 執行個體) 一併執行。

您也可以搭配拓撲感知流量路徑,使用區域放置位置,減少用戶端和工作負載之間的延遲。如要進一步瞭解拓撲感知流量導向,請參閱「拓撲感知導向」。

使用區域拓撲控管 Pod 放置位置是 Kubernetes 的進階機制,只有在需要 Pod 在特定區域中執行的情況下才應使用。在大多數正式環境中,建議您盡可能使用區域資源 (GKE 預設值)。

區域刊登位置配置方式

Kubernetes 內建區域拓撲,並使用 topology.kubernetes.io/zone: ZONE 節點標籤。如要告知 GKE 將 Pod 放置在特定區域,請使用下列任一方法:

  • nodeAffinity:在 Pod 規格中,為一或多個 Google Cloud 區域指定 nodeAffinity 規則。這個方法比 nodeSelector 更具彈性,因為您可以在多個區域中放置 Pod。
  • nodeSelector:在單一 Google Cloud 區域的 Pod 規格中指定 nodeSelector。

  • 運算類別:設定 Pod 使用 GKE 運算類別。這種方法可讓您定義一組優先順序清單,列出 Google Cloud 區域。當這些區域有可用節點時,這項功能可讓工作負載動態移至最偏好的區域組合。詳情請參閱「關於自訂運算類別」。

注意事項

使用區域拓撲進行區域 Pod 放置時,請注意下列事項:

  • 叢集必須與要求的區域位於相同 Google Cloud 區域。
  • 在 Standard 叢集中,您必須使用節點自動佈建功能,或在要求的可用區中建立節點集區。Autopilot 叢集會自動為您管理這項程序。
  • 標準叢集必須是區域叢集。

定價

可用區拓撲是 Kubernetes 排程功能,在 GKE 中使用無須額外付費。

如需定價詳細資料,請參閱 GKE 定價

事前準備

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

  • 啟用 Google Kubernetes Engine API。
  • 啟用 Google Kubernetes Engine API
  • 如要使用 Google Cloud CLI 執行這項工作,請安裝初始化 gcloud CLI。如果您先前已安裝 gcloud CLI,請執行 gcloud components update 指令,取得最新版本。較舊的 gcloud CLI 版本可能不支援執行本文件中的指令。
  • 確認您在與要放置 Pod 的區域相同的Google Cloud 地區中,已有 GKE 叢集。如要建立新叢集,請參閱「建立 Autopilot 叢集」。

使用 nodeAffinity 將 Pod 放置在多個可用區中

Kubernetes nodeAffinity 提供彈性的排程控制機制,支援多個標籤選取器和邏輯運算子。如要讓 Pod 在一組可用區中執行 (例如 us-central1-aus-central1-f),請使用 nodeAffinity。

  1. 將下列資訊清單儲存為 multi-zone-affinity.yaml

    apiVersion: apps/v1
    kind: Deployment
    metadata:
      name: nginx-deployment
    spec:
      replicas: 3
      selector:
        matchLabels:
          app: nginx-multi-zone
      template:
        metadata:
          labels:
            app: nginx-multi-zone
        spec:
          containers:
          - name: nginx
            image: nginx:latest
            ports:
            - containerPort: 80
          affinity:
            nodeAffinity:
              requiredDuringSchedulingIgnoredDuringExecution:
                nodeSelectorTerms:
                - matchExpressions:
                  - key: topology.kubernetes.io/zone
                    operator: In
                    values:
                    - us-central1-a
                    - us-central1-f
    

    這份資訊清單會建立含有三個副本的 Deployment,並根據節點可用性,將 Pod 放置在 us-central1-aus-central1-f 中。

    確認叢集位於 us-central1 區域。如果叢集位於不同區域,請將資訊清單值欄位中的區域變更為叢集區域中的有效區域。

    選用:如果您要佈建 TPU VM,請使用 AI 區域,例如 us-central1-ai1a。AI 可用區是經過最佳化調整的專用位置,適用於 Google Cloud 區域內的 AI/機器學習工作負載。

  2. 建立 Deployment:

    kubectl create -f multi-zone-affinity.yaml
    

    GKE 會在其中一個指定區域的節點中建立 Pod。 多個 Pod 可能會在同一節點上執行。您可以視需要使用 Pod 反相依性,指示 GKE 將每個 Pod 放在不同的節點上。

使用 nodeSelector 將 Pod 放置在單一可用區中

如要將 Pod 放在單一可用區,請在 Pod 規格中使用 nodeSelector。nodeSelector 等於指定單一可用區的 requiredDuringSchedulingIgnoredDuringExecution nodeAffinity 規則。

  1. 將下列資訊清單儲存為 single-zone-selector.yaml

    apiVersion: apps/v1
    kind: Deployment
    metadata:
      name: nginx-singlezone
    spec:
      replicas: 3
      selector:
        matchLabels:
          app: nginx-singlezone
      template:
        metadata:
          labels:
            app: nginx-singlezone
        spec:
          nodeSelector:
            topology.kubernetes.io/zone: "us-central1-a"
          containers:
          - name: nginx
            image: nginx:latest
            ports:
            - containerPort: 80
    

    這份資訊清單會告知 GKE,將 Deployment 中的所有副本都放在 us-central1-a 可用區。

  2. 建立 Deployment:

    kubectl create -f single-zone-selector.yaml
    

使用運算類別,優先在所選區域中放置 Pod

GKE 計算類別提供控制機制,可讓您定義節點設定優先順序清單。可用區偏好設定可讓您定義要 GKE 將 Pod 放置在哪些可用區。如要在運算級別中定義可用區偏好設定,必須使用 GKE 1.33.1-gke.1545000 以上版本。

以下範例會建立運算級別,指定 Pod 的偏好可用區清單。

這些步驟假設您的叢集位於 us-central1 區域。如果叢集位於不同地區,請將資訊清單中的可用區值變更為叢集地區的有效可用區。

  1. 將下列資訊清單儲存為 zones-custom-compute-class.yaml

    apiVersion: cloud.google.com/v1
    kind: ComputeClass
    metadata:
      name: zones-custom-compute-class
    spec:
      priorities:
      - location:
        zones: [us-central1-a, us-central1-b]
      - location:
        zones: [us-central1-c]
      activeMigration:
        optimizeRulePriority: true
      nodePoolAutoCreation:
        enabled: true
      whenUnsatisfiable: ScaleUpAnyway
    

    這項運算類別資訊清單會變更縮放行為,如下所示:

    1. GKE 會嘗試將 Pod 放置在 us-central1-aus-central1-b 中。
    2. 如果 us-central1-aus-central1-b 沒有可用容量, GKE 會嘗試將 Pod 放置在 us-central1-c 中。
    3. 如果 us-central1-c 沒有可用容量,whenUnsatisfiable: ScaleUpAnyway 欄位會讓 GKE 將 Pod 放置在該區域的任何可用區域中。
    4. 如果運算類別中優先順序較高的可用區稍後可用,activeMigration.optimizeRulePriority: true 欄位會讓 GKE 將 Pod 從任何優先順序較低的可用區移至該可用區。這項遷移作業會使用 Pod 中斷預算,確保服務可用性。
  2. 建立自訂運算類別:

    kubectl create -f zones-custom-compute-class.yaml
    

    GKE 會建立工作負載可參照的自訂運算級別。

  3. 將下列資訊清單儲存為 custom-compute-class-deployment.yaml

    apiVersion: apps/v1
    kind: Deployment
    metadata:
      name: nginx-zonal-preferences
    spec:
      replicas: 3
      selector:
        matchLabels:
          app: nginx-zonal-preferences
      template:
        metadata:
          labels:
            app: nginx-zonal-preferences
        spec:
          nodeSelector:
            cloud.google.com/compute-class: "zones-custom-compute-class"
          containers:
          - name: nginx
            image: nginx:latest
            ports:
            - containerPort: 80
    
  4. 建立 Deployment:

    kubectl create -f custom-compute-class-deployment.yaml
    

目標 AI 可用區

AI 可用區是專門用於 AI/機器學習訓練和推論工作負載的區域。這些可用區提供大量機器學習加速器容量。詳情請參閱AI 區域說明文件。

在 GKE 中使用 AI 區域前,請先考量下列特性:

  • AI 可用區與標準可用區在實體上是分開的,可提供額外的儲存空間和電力。這種分離可能會導致延遲時間較長,但一般來說,AI/機器學習工作負載可以容忍這種情況。
  • AI 可用區的尾碼會加上 ai 標記。舉例來說,us-central1 地區中的 AI 區域名稱為 us-central1-ai1a
  • 目前僅支援 TPU VM。
  • 叢集的控制層會在與 AI 可用區相同的區域內,於一或多個標準可用區中執行。
  • 如要在 AI 區域中執行未附加 TPU 的 VM,必須符合下列條件:

    • 您已在同一可用區中執行其他使用 TPU VM 的工作負載。
    • 非 TPU VM 要不是 Spot VM、與預留項目綁定,就是節點集區的一部分,且具有特定的加速器與一般用途 VM 比例。
  • 在同一區域中,AI 可用區與具有相同尾碼的標準可用區共用元件,例如網路連線和軟體推出。對於高可用性工作負載,建議您使用不同區域。舉例來說,請避免同時使用 us-central1-ai1aus-central1-a 來確保高可用性。

根據預設,GKE 不會在 AI 區域部署工作負載。如要使用 AI 專區,請設定下列其中一個選項:

  • (建議) ComputeClasses:將最高優先順序設為在 AI 區域中要求隨選 TPU。您可以透過 ComputeClass 為工作負載定義優先順序清單。如需範例,請參閱「關於 ComputeClass」。
  • 節點自動佈建:在 Pod 規格中使用 nodeSelectornodeAffinity,指示節點自動佈建功能在 AI 可用區中建立節點集區。如果工作負載未明確指定 AI 區域,節點自動佈建功能只會考慮標準區域或--autoprovisioning-locations中的區域,建立新的節點集區。這項設定可確保未執行 AI/ML 模型的作業會留在標準可用區,除非您明確設定其他可用區。如需使用 nodeSelector 的資訊清單範例,請參閱「設定自動建立節點的預設區域」。
  • GKE Standard:如果您直接管理節點集區,請在建立節點集區時,於 --node-locations 旗標中使用 AI 區域。如需範例,請參閱「在 GKE Standard 中部署 TPU 工作負載」。

確認 Pod 放置位置

如要驗證 Pod 的放置位置,請列出 Pod 並檢查節點標籤。單一節點中可能會執行多個 Pod,因此如果您使用 nodeAffinity,可能不會看到 Pod 分散在多個區域中。

  1. 列出 Pod:

    kubectl get pods -o wide
    

    輸出內容會列出執行中的 Pod,以及對應的 GKE 節點。

  2. 描述節點:

    kubectl describe node NODE_NAME | grep "topology.kubernetes.io/zone"
    

    NODE_NAME 替換為節點名稱。

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

    topology.kubernetes.io/zone: us-central1-a
    

如要讓 GKE 將 Pod 平均分散到多個區域,以改善多個故障網域的容錯移轉,請使用 topologySpreadConstraints

後續步驟