Topology Spread Constraints — controlling Pod distribution across zones/nodes.
Problem: By default, scheduler may place all Pods on a single node/zone.
Even distribution across zones:
1apiVersion: apps/v12kind: Deployment3metadata:4 name: myapp5spec:6 replicas: 67 template:8 spec:9 topologySpreadConstraints:10 - maxSkew: 111 topologyKey: topology.kubernetes.io/zone12 whenUnsatisfiable: DoNotSchedule13 labelSelector:14 matchLabels:15 app: myapp16 - maxSkew: 117 topologyKey: kubernetes.io/hostname18 whenUnsatisfiable: ScheduleAnyway19 labelSelector:20 matchLabels:21 app: myapp
Parameters:
DoNotSchedule — do not schedule.ScheduleAnyway — schedule, but with priority.Distribution across nodes (anti-affinity alternative):
1topologySpreadConstraints:2 - maxSkew: 13 topologyKey: kubernetes.io/hostname4 whenUnsatisfiable: DoNotSchedule5 labelSelector:6 matchLabels:7 app: myapp8 matchLabelKeys:9 - pod-template-hash
Topology Spread vs Pod Anti-Affinity: Topology Spread is more flexible and predictable.