AWS Community Builders  profile image Hari Karthigasu

The Kubernetes Topology Spread Constraints allow scheduling pods evenly across nodes, regions and availability zones.

The following TopologySpreadConstraints ensure that at any given time, the difference in the number of pods between two nodes never exceeds 2.

Instance: If you run 5 pods on 3 nodes. Kubernetes scheduler will schedule them as either 1:1:3 or 2:2:1 based on topologyKey: kubernetes.io/hostname

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

Enter fullscreen mode Exit fullscreen mode

It'll never be scheduled as 3:2:0 as the maximum difference reaches 3-0=3.

The pods will not be scheduled if the condition is not met. whenUnsatisfiable: DoNotSchedule

Likewise, we can have multiple constraints. Scheduler performs AND operations among them to identify the best combination.

      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