Skip to main content
Cluster operationsLesson 2 of 16

Spreading Pods Across Zones (Topology Spread Constraints)

Six replicas in one zone survive no zone outage. Follow the scheduler as topologySpreadConstraints count Pods per zone and per node, refuse placements that tip the balance, and run into a dead zone and a rolling update.

Pods a / b / c3 / 3 / 0
Skew3
SchedulerNo rule
zone azone bzone cDeployment web6 replicaskube-schedulerPodTopologySpreadScheduler configdefaultConstraintsnode-a1zone aweb ×2node-a2zone aweb ×1node-b1zone bweb ×2node-b2zone bweb ×1node-c1zone cnode-c2zone cPending PodwebDeschedulerOptional add-on
RequestWatchAppears in a later step

What you will learn

  • Domains from topologyKey and the Pods labelSelector counts
  • How maxSkew is measured against the global minimum
  • DoNotSchedule versus ScheduleAnyway
  • Zone failure and minDomains
  • Rolling updates and matchLabelKeys
  • Cluster defaults and when podAntiAffinity is the better tool

Fields at a glance​

FieldWhat it doesWatch out
topologyKeyNode label whose values form domains (topology.kubernetes.io/zone, kubernetes.io/hostname)Nodes without the label are skipped entirely
maxSkewLargest allowed gap between a domain and the global minimumMust be at least 1
whenUnsatisfiableDoNotSchedule (default) filters nodes. ScheduleAnyway only scoresDoNotSchedule can leave Pods Pending during an outage
labelSelectorWhich Pods are counted, same namespace onlyThe Pod should match its own selector, or it does not count itself
matchLabelKeysAdds this Pod's values for these label keys to the selector, e.g. pod-template-hashDo not use labels that get edited on running Pods
minDomainsTreat the global minimum as 0 until this many domains existOnly with DoNotSchedule
nodeAffinityPolicyHonor (default): only nodes matching the Pod's node affinity countIgnore counts all nodes
nodeTaintsPolicyIgnore (default): taints do not matter. Honor: skip nodes with untolerated taintsDefault counts tainted nodes as domains

Try it yourself​

kubectl get nodes -L topology.kubernetes.io/zone # Which zone each node is in
kubectl explain pod.spec.topologySpreadConstraints # Fields your cluster version supports

# Pods per zone for app=web
for n in $(kubectl get pods -l app=web -o jsonpath='{.items[*].spec.nodeName}'); do
kubectl get node "$n" -o jsonpath='{.metadata.labels.topology\.kubernetes\.io/zone}{"\n"}'
done | sort | uniq -c

kubectl get pods -l app=web --field-selector=status.phase=Pending
kubectl describe pod <pending-pod> # "didn't match pod topology spread constraints"
kubectl get deploy web -o jsonpath='{.spec.template.spec.topologySpreadConstraints}'

Check yourself

  1. Counts are 3 / 2 / 2 across zones with maxSkew 1 and DoNotSchedule. Where can the next Pod go?
  2. You want 5 replicas balanced over 3 zones. Why is topology spread a better fit than required podAntiAffinity on the zone key?
  3. After a rollout, new Pods sit 3 / 2 / 1 even though maxSkew is 1. What is the most likely reason?

Further reading: Pod Topology Spread Constraints · Well-known labels · Descheduler · Which Node Gets the Pod (Scheduling) · When One Availability Zone Goes Down · Zero-Downtime Deploys (Rolling Update)