Six replicas, three zones, and nothing keeps them apart web has 6 replicas and no spread rules. The scheduler does apply built-in defaults to Pods of a Deployment (maxSkew 3 per node, 5 per zone), but they are ScheduleAnyway: only a scoring nudge that other scores can outweigh.
Here zone c happened to be busy when the Pods were created, so all six landed in zones a and b. If zone a fails, half the service goes with it, and zone c sits idle.
topologyKey turns node labels into domains Add a constraint with topologyKey: topology.kubernetes.io/zone. Every distinct value of that node label is one domain, so this cluster has three: a, b and c. With kubernetes.io/hostname, every node is its own domain.
labelSelector (app: web) says which Pods to count. Only Pods in the same namespace count. A node without the topologyKey label is skipped: no Pods land there and its Pods are not counted.
Key idea Check node labels first. A typo in the zone label silently removes a node from the spread.
maxSkew compares each domain with the emptiest one The Deployment is recreated with maxSkew: 1 and whenUnsatisfiable: DoNotSchedule. Skew is the matching Pods in a domain minus the global minimum, the count in the emptiest eligible domain. A placement is allowed only if skew stays at or below maxSkew after it.
After five Pods the counts are 2 / 2 / 1. Putting the sixth in zone a would make 3 − 1 = 2, too much. Only zone c keeps skew at 1, so the Pod goes there.
Zone and node constraints are checked together A second constraint with topologyKey: kubernetes.io/hostname and maxSkew 1 keeps Pods spread across nodes inside each zone too. A node must satisfy every constraint to pass the filter.
Each pair of topologyKey and whenUnsatisfiable may appear only once. A common pattern is zone with DoNotSchedule for resilience, plus hostname with ScheduleAnyway so a full node does not block scheduling.
DoNotSchedule waits, ScheduleAnyway places it anyway web scales to 9 while zone c is out of CPU. Pods 7 and 8 go to a and b, giving 3 / 3 / 2. Pod 9 would make 4 / 3 / 2 in a or b, skew 2. With DoNotSchedule it stays Pending, and the event says the nodes did not match pod topology spread constraints.
With ScheduleAnyway the scheduler places Pod 9 anyway and only prefers nodes that keep skew low. The Pod runs, but the balance you asked for is gone.
Watch out DoNotSchedule is a hard guarantee that can leave Pods Pending. A Pending Pod can at least trigger a node autoscaler to add capacity in zone c.
Zone c fails, and its replacements may not fit Back at 6 replicas, 2 / 2 / 2. Zone c loses power. Its two Pods are evicted and the Deployment creates two replacements. While the dead nodes still exist as Node objects, zone c is still a domain, now with 0 matching Pods.
Placing a replacement in zone a would make 3 − 0 = 3, above maxSkew 1. With DoNotSchedule on the zone constraint, the replacements can stay Pending exactly when you need them. They fit once the dead nodes are removed, or right away if the zone rule is ScheduleAnyway.
Watch out Decide which matters more during an outage: strict balance or running Pods. Plan headroom in the surviving zones either way.
minDomains: count zones that have no nodes yet The scheduler only knows domains from existing nodes. If zone c's node pool is scaled to zero, only a and b exist, and 3 / 3 looks perfectly balanced.
minDomains: 3 says at least three domains should hold Pods. With fewer eligible domains, the global minimum counts as 0, so Pods beyond maxSkew in a and b stay Pending. That gives a node autoscaler a reason to start a node in zone c. minDomains works only with DoNotSchedule.
A rolling update counts old and new Pods together Zone c is back and web is 2 / 2 / 2. A rollout starts. New Pods carry app: web, so they are counted together with the old ones. The scheduler places each new Pod to balance all web Pods, not the new revision alone.
When the old Pods are gone, the new ones can end up 3 / 2 / 1. Add matchLabelKeys: [pod-template-hash]. Its value is merged into labelSelector, so each ReplicaSet revision is spread on its own.
Watch out Scaling down never rebalances either. The scheduler only acts at placement time. A tool like the Descheduler can evict Pods that violate the spread so they are placed again.
Cluster defaults cover Pods that set nothing Pods with no topologySpreadConstraints that belong to a Service, ReplicaSet, StatefulSet or ReplicationController get the scheduler's default constraints. Built in: maxSkew 3 on kubernetes.io/hostname and 5 on topology.kubernetes.io/zone, both ScheduleAnyway.
An operator can replace them in the PodTopologySpread plugin args of KubeSchedulerConfiguration, with defaultingType: List and a defaultConstraints list without labelSelector. Managed clusters often do not let you change scheduler config.
Anti-affinity allows one Pod per domain, spread allows many Required podAntiAffinity on the zone key means no two web Pods in one zone. With 3 zones, the fourth replica is Pending forever. Preferred anti-affinity is only a hint.
Topology spread keeps zones balanced at any replica count: 2 / 2 / 2, then 3 / 3 / 3. Use anti-affinity for strict one-per-node rules, such as two members of a quorum that must never share a node, and spread for balancing.
Count, compare with the emptiest domain, and decide what to do when it fails topologyKey turns node labels into domains, labelSelector picks the Pods to count, and maxSkew caps the gap to the emptiest domain. DoNotSchedule enforces it, ScheduleAnyway only prefers it.
Strict zone spread can block replacements during a zone outage. minDomains counts zones that have no nodes yet. matchLabelKeys with pod-template-hash keeps each rollout balanced on its own.
Summary A common setup: zone maxSkew 1 DoNotSchedule plus hostname ScheduleAnyway, matchLabelKeys pod-template-hash, and spare capacity in every zone.