AWS Community Builders  profile image Hari Karthigasu

Kubernetes Topology Spread Constraints 可讓 Pod 均勻排程到各節點、區域與可用區。

以下 TopologySpreadConstraints 確保在任何時間點,兩個節點之間的 Pod 數量差異不會超過 2 個。

範例:如果您在 3 個節點上執行 5 個 Pod,Kubernetes 排程器會根據 topologyKey: kubernetes.io/hostname 將它們排程為 1:1:32:2:1

option 1: Maximum difference is 3-1=2
option 2: Maximum difference is 2-1=1

Enter fullscreen mode Exit fullscreen mode

它絕不會被排程為 3:2:0,因為最大差異已達到 3-0=3

如果條件未滿足,Pod 將不會被排程。whenUnsatisfiable: DoNotSchedule

同樣地,我們可以設定多個條件。排程器會對它們執行 AND 運算,以找出最佳組合。

      topologySpreadConstraints:
      - maxSkew: 2
        topologyKey: kubernetes.io/hostname
        whenUnsatisfiable: DoNotSchedule
        labelSelector:
          matchLabels:
            app.kubernetes.io/application: backend
            app.kubernetes.io/name: backend
_      # Setting maxSkew to 1 ensures that pods are (almost) evenly distributed across the AZs.  _      
      - maxSkew: 1
        topologyKey: topology.kubernetes.io/zone
_        # Setting ScheduleAnyway ensures that during an AZ failure or resource starvation, pods will be scheduled to one of the active AZs by bypassing maxSkew._
        whenUnsatisfiable: ScheduleAnyway
        labelSelector:
          matchLabels:
            app.kubernetes.io/application: backend
            app.kubernetes.io/name: backend

Enter fullscreen mode Exit fullscreen mode