Pod Topology Spread Constraints Guide
Use topologySpreadConstraints to distribute pods evenly across zones, nodes, and failure domains for high availability in Kubernetes.
π‘ Quick Answer:
topologySpreadConstraintsensures pods are evenly distributed across topology domains (zones, nodes) usingmaxSkewto limit imbalance andwhenUnsatisfiableto control scheduling behavior.
The Problem
Pod anti-affinity prevents co-location but doesnβt ensure even distribution. Without topology spread constraints, the scheduler may place all pods in one zone β causing cascading failures during zone outages.
The Solution
Spread Across Zones
apiVersion: apps/v1
kind: Deployment
metadata:
name: web-app
spec:
replicas: 6
selector:
matchLabels:
app: web-app
template:
metadata:
labels:
app: web-app
spec:
topologySpreadConstraints:
- maxSkew: 1
topologyKey: topology.kubernetes.io/zone
whenUnsatisfiable: DoNotSchedule
labelSelector:
matchLabels:
app: web-app
- maxSkew: 1
topologyKey: kubernetes.io/hostname
whenUnsatisfiable: ScheduleAnyway
labelSelector:
matchLabels:
app: web-app
containers:
- name: app
image: web-app:3.0
resources:
requests:
cpu: 250m
memory: 128MiWith Node Affinity Combined
topologySpreadConstraints:
- maxSkew: 1
topologyKey: topology.kubernetes.io/zone
whenUnsatisfiable: DoNotSchedule
labelSelector:
matchLabels:
app: web-app
nodeAffinityPolicy: Honor
nodeTaintsPolicy: HonorMinimum Domains (1.30+)
topologySpreadConstraints:
- maxSkew: 1
topologyKey: topology.kubernetes.io/zone
whenUnsatisfiable: DoNotSchedule
labelSelector:
matchLabels:
app: web-app
minDomains: 3graph TD
subgraph Zone-A
N1[Node 1: 2 pods]
N2[Node 2: 1 pod]
end
subgraph Zone-B
N3[Node 3: 2 pods]
N4[Node 4: 1 pod]
end
subgraph Zone-C
N5[Node 5: 1 pod]
N6[Node 6: 1 pod]
end
S[Scheduler] -->|maxSkew=1| Zone-A
S -->|maxSkew=1| Zone-B
S -->|maxSkew=1| Zone-CCommon Issues
Pods stuck Pending with DoNotSchedule If topology domains are unbalanced or insufficient, pods canβt be placed. Use ScheduleAnyway for soft preference:
kubectl get pods -o wide | grep Pending
kubectl describe pod <pending-pod> | grep -A5 EventsmaxSkew calculation confusion maxSkew is relative to the domain with the minimum pod count. With zones having [3, 2, 2] pods and maxSkew: 1, the max any zone can have is min(2) + maxSkew(1) = 3.
Interaction with pod anti-affinity Topology spread and anti-affinity are both evaluated. If anti-affinity is Required, it takes precedence.
Best Practices
- Use
DoNotSchedulefor zone spread (hard requirement for HA) - Use
ScheduleAnywayfor node spread (soft preference) - Set
maxSkew: 1for most even distribution - Combine zone + node spread for multi-level distribution
- Add
minDomainswhen you need guaranteed multi-zone presence - Always include a
labelSelectormatching the same pods
Key Takeaways
topologyKeydefines the domain (zone, node, rack, etc.)maxSkewis the maximum allowed difference between domain pod countsDoNotScheduleblocks scheduling;ScheduleAnywayapplies a scoring penalty- Multiple constraints are ANDed β all must be satisfiable
nodeAffinityPolicy: Honorrespects node affinity when computing skew- Cluster-level defaults can be set via scheduler profiles

Recommended
Kubernetes Recipes β The Complete Book100+ production-ready patterns with detailed explanations, best practices, and copy-paste YAML. Everything in one place.
Get the Book βLearn by Doing
CopyPasteLearn β Hands-on Cloud & DevOps CoursesMaster Kubernetes, Ansible, Terraform, and MLOps with interactive, copy-paste-run lessons. Start free.
Browse Courses βπ Deepen Your Skills β Hands-on Courses
Courses by CopyPasteLearn.com β Learn IT by Doing
