Three replicas on one node are one failure away from zero. Replicas only protect you when they sit in different failure domains, and the scheduler doesn't guarantee that unless you ask. Its built-in spreading is a soft preference with a wide tolerance, so a Deployment with no placement rules can end up with every replica on the same node or in the same zone, and stay that way.
Kubernetes has two mechanisms for asking: pod anti-affinity and topology spread constraints. They overlap, and they fail in different ways.
Who this is for: engineers running replicated services across several nodes or zones, and anyone who has found all replicas of a service on the node that just failed.
What you walk away with: what each mechanism guarantees, where each one stops working, a decision table, and two baseline snippets for zone and node spreading.
The text follows the Kubernetes v1.37 documentation. The examples were run on 17 November 2026 on a single-zone GKE v1.36.4 cluster, with a node label standing in for zones. Taints, the Descheduler and scheduler configuration were not tested.
The mistakes, and what they cause
Assuming replicas are spread because there are several of them. Without rules of your own, the scheduler applies default constraints of maxSkew: 3 per node and maxSkew: 5 per zone, both with ScheduleAnyway. They are scoring hints and exclude nothing.
Required anti-affinity on hostname. This allows one pod per node, and so caps the workload at the number of eligible nodes. When the HorizontalPodAutoscaler asks for a ninth replica on eight nodes, the pod stays Pending. During a rolling update, the surge pod needs a node with no pod of the same app on it. With as many replicas as nodes there is none, so the rollout only moves if an old pod may be removed first. Reasoning from the defaults (maxSurge 25% rounded up, maxUnavailable 25% rounded down): three replicas on three nodes give maxUnavailable 0 and the rollout stalls; four on four give 1 and it proceeds.
Preferred anti-affinity, taken for a guarantee. It is one score among several. When the cluster is tight, the scheduler places the pod wherever it fits, and nothing tells you the preference lost.
A label selector that doesn't match the pod's own labels. The constraint counts other pods but never the pod it belongs to, so every replica looks like the first one. The documentation calls these "ghost pods": they pile up in one domain because placing them never changes the calculation.
Expecting the spread to be maintained. Both mechanisms act only when a pod is scheduled. Nothing moves a running pod because the distribution got worse later.
Rolling updates that leave a skew. During a rollout, old and new pods both match the selector. New pods are placed to balance the total, and when the old pods go, what remains can be lopsided.
What "domain" means
Both mechanisms work on a topologyKey, which is a node label. Nodes with the same value for that label form one domain. The two keys that matter most:
kubernetes.io/hostname: each node is a domain. Protects against node failure and drains.topology.kubernetes.io/zone: each zone is a domain. Protects against a zone outage.
Check that your nodes carry both labels, because the scheduler bypasses nodes that lack a constraint's topology key: their pods aren't counted and no pod with that constraint is placed there.
kubectl get nodes -L topology.kubernetes.io/zone,kubernetes.io/hostname
Pod anti-affinity
Anti-affinity says: don't put this pod in a domain that already holds a pod matching this selector.
affinity:
podAntiAffinity:
requiredDuringSchedulingIgnoredDuringExecution:
- labelSelector:
matchLabels:
app: checkout
topologyKey: kubernetes.io/hostname
Required is a hard filter. The documentation states the consequence: in this mode "only a single Pod can be scheduled into a single topology domain". On hostname that means one per node. On zone it means one per zone, so three zones give you three replicas at most.
Preferred (preferredDuringSchedulingIgnoredDuringExecution) takes a weight from 1 to 100 and adds it to the node's score. In the documentation's words, you "lose the ability to enforce the constraint".
The scheduler applies a required rule in both directions: a running pod that has one also keeps out incoming pods matching its selector, whether or not they carry the rule. The selector only sees the pod's own namespace unless you add namespaces or namespaceSelector. matchLabelKeys and mismatchLabelKeys exist here too; their gate has been stable since v1.33, though the documentation's prose still says beta.
Anti-affinity has no middle setting. It can't express "at most two per node" or "as even as possible across zones, but always schedule".
It also costs scheduler time. The documentation warns that inter-pod affinity and anti-affinity "require substantial amounts of processing which can slow down scheduling in large clusters significantly" and advises against them in clusters larger than several hundred nodes. The scheduler article explains where that work happens.
There is still a place for it. Required anti-affinity is the direct way to say "never two of these on one node" when exactly that is the requirement, as with members of a quorum-based store. It also works across workloads, to keep one application away from another, which spread constraints don't do.
Topology spread constraints
A spread constraint limits how uneven the distribution may be:
topologySpreadConstraints:
- maxSkew: 1
topologyKey: topology.kubernetes.io/zone
whenUnsatisfiable: DoNotSchedule
labelSelector:
matchLabels:
app: checkout
maxSkew is the largest permitted difference between the number of matching pods in a domain and the global minimum, which is the count in the eligible domain with the fewest. With maxSkew: 1 and three zones holding 2, 2 and 1 pods, the next pod must go to the third zone: anywhere else would make the difference 2.
whenUnsatisfiable decides what the constraint is:
DoNotSchedulemakes it a filter. A pod that can't be placed within the skew stays Pending.ScheduleAnywaymakes it a score. The scheduler prefers nodes that reduce the skew and places the pod regardless.
Set the field explicitly: the documentation labels DoNotSchedule "(default)", but the API server rejects a constraint without it.
With ScheduleAnyway, maxSkew no longer acts as a limit. The documentation only says the scheduler "gives higher precedence to topologies that would help reduce the skew".
A pod can have several constraints, and they are combined with AND: a node has to satisfy all of them. Only one constraint is allowed per combination of topologyKey and whenUnsatisfiable. Only pods in the same namespace as the incoming pod are counted.
Unlike required anti-affinity, a spread constraint puts no ceiling on replicas. Twelve replicas across three zones with maxSkew: 1 is four per zone.
The fields that change the result
| Field | State in v1.37 | Default when unset |
|---|---|---|
minDomains |
Stable since v1.30 | Behaves as 1 |
nodeAffinityPolicy |
Stable since v1.33 | Honor |
nodeTaintsPolicy |
Stable since v1.33 | Ignore |
matchLabelKeys |
Beta, enabled by default since v1.27 | Not set |
minDomains
The scheduler only knows the domains that currently have nodes. If two of your three zones have nodes at the moment, maxSkew: 1 is satisfied by splitting evenly across two zones, and nothing tells you the third is missing.
minDomains: 3 changes that. While fewer than three eligible domains exist, the global minimum is treated as zero, so no domain may hold more than maxSkew pods. Further pods stay Pending, so the gap is visible. Whether a node autoscaler then adds capacity there depends on the autoscaler. minDomains can only be used with DoNotSchedule.
nodeAffinityPolicy and nodeTaintsPolicy
These decide which nodes count when the skew is calculated.
nodeAffinityPolicy defaults to Honor: only nodes that match the pod's nodeSelector and node affinity are included.
nodeTaintsPolicy defaults to Ignore: tainted nodes are included even when the pod doesn't tolerate the taint. This default causes Pending pods. Suppose a zone's only nodes are tainted for another team. The zone counts as a domain with zero matching pods, the global minimum is zero, and with maxSkew: 1 the other zones may hold one pod each. The second pod per zone can't be placed, and the pod that would even things out can't go to the tainted zone either.
Setting nodeTaintsPolicy: Honor includes only untainted nodes and tainted nodes the pod tolerates. If your cluster has dedicated node pools, as described in taints and tolerations, set it explicitly on every DoNotSchedule constraint.
matchLabelKeys
This is the fix for rollout skew. You list label keys, and the API server looks up the incoming pod's values for them and merges them into the labelSelector. With pod-template-hash, which the Deployment controller puts on every pod, each revision is spread on its own:
topologySpreadConstraints:
- maxSkew: 1
topologyKey: kubernetes.io/hostname
whenUnsatisfiable: DoNotSchedule
labelSelector:
matchLabels:
app: checkout
matchLabelKeys:
- pod-template-hash
Three rules apply: matchLabelKeys needs a labelSelector, the same key may not appear in both, and keys missing from the pod's labels are ignored. Since v1.34 the merge is done explicitly at pod creation, so you can see the resulting selector on the pod. That step has its own kube-apiserver gate, MatchLabelKeysInPodTopologySpreadSelectorMerge, beta and on by default. The documentation advises against using it with labels that might be edited directly on pods, because the merged selector isn't updated.
The field is beta and on by default, but a cluster can have the MatchLabelKeysInPodTopologySpread gate disabled, so check before relying on it.
What neither mechanism does
No rebalancing. The documentation lists this as a known limitation: there is "no guarantee that the constraints remain satisfied when Pods are removed". The documentation's example is that scaling down a Deployment may leave the pods imbalanced. A zone that fails and returns comes back empty, and the replicas that moved stay where they went. The Descheduler project has a RemovePodsViolatingTopologySpreadConstraint plugin that evicts pods so they can be rescheduled. Its evictions are disruptions like any other, so pair it with PodDisruptionBudgets.
No knowledge of domains without nodes. A node group scaled to zero doesn't exist as far as the scheduler is concerned. The documentation's suggested answer is a node autoscaler that understands spread constraints and knows the full set of domains.
No capacity planning. A hard zone constraint ties your scaling to the zone with the least room. If one zone can't get nodes, because of a provider shortage or a quota, then with DoNotSchedule and maxSkew: 1 the workload can grow from an even spread by one pod per remaining zone and then stops, however much capacity the others have. An HPA scaling up under load meets this at the worst time. This is the main argument for ScheduleAnyway, or a larger maxSkew, on the zone constraint of anything that has to scale during an incident.
Cluster-level defaults
The built-in defaults, when nobody has configured others, are:
defaultConstraints:
- maxSkew: 3
topologyKey: "kubernetes.io/hostname"
whenUnsatisfiable: ScheduleAnyway
- maxSkew: 5
topologyKey: "topology.kubernetes.io/zone"
whenUnsatisfiable: ScheduleAnyway
They apply only to pods that define no constraints of their own and belong to a Service, ReplicaSet, StatefulSet or ReplicationController. A cluster administrator can replace them through the PodTopologySpread plugin arguments in the scheduler configuration:
apiVersion: kubescheduler.config.k8s.io/v1
kind: KubeSchedulerConfiguration
profiles:
- schedulerName: default-scheduler
pluginConfig:
- name: PodTopologySpread
args:
defaultConstraints:
- maxSkew: 1
topologyKey: topology.kubernetes.io/zone
whenUnsatisfiable: ScheduleAnyway
defaultingType: List
Two limits apply. As soon as a pod defines one constraint of its own, none of the defaults apply to it. And on a managed control plane you may not have access to the scheduler configuration, which leaves per-workload constraints or a policy that injects them at admission.
Which one to use
| You need | Use | Why |
|---|---|---|
| Even spread across zones, any replica count | Spread constraint on zone | Anti-affinity can't express "even" |
| No two replicas on one node, replica count well below node count | Spread constraint on hostname with DoNotSchedule, or required anti-affinity |
Both work; anti-affinity stays at one per node even when that leaves pods Pending |
| Strictly one per node, by design | Required anti-affinity on hostname | That is exactly what it says |
| Best effort, never block scheduling | Spread constraint with ScheduleAnyway |
Considers the whole distribution; preferred anti-affinity only penalises co-location |
| Clean spread after rollouts | Spread constraint with matchLabelKeys |
Counts each revision separately |
| Keep workload A away from workload B | Anti-affinity | Spread constraints only balance one set of pods |
| A large cluster | Spread constraints | Anti-affinity carries a documented scheduling cost |
For spreading replicas of one workload, spread constraints are the default choice. Anti-affinity remains for exclusion rules.
Two baselines
For a stateless service that must keep scheduling under pressure, with a hard rule per node and a soft one per zone:
spec:
replicas: 6
selector:
matchLabels:
app: checkout
template:
metadata:
labels:
app: checkout
spec:
topologySpreadConstraints:
- maxSkew: 1
topologyKey: topology.kubernetes.io/zone
whenUnsatisfiable: ScheduleAnyway
labelSelector:
matchLabels:
app: checkout
matchLabelKeys:
- pod-template-hash
- maxSkew: 1
topologyKey: kubernetes.io/hostname
whenUnsatisfiable: DoNotSchedule
nodeTaintsPolicy: Honor
labelSelector:
matchLabels:
app: checkout
matchLabelKeys:
- pod-template-hash
The hostname constraint is hard, but with maxSkew: 1 it allows two pods on a node once every eligible node has one, so the constraint itself sets no ceiling on replicas. One catch follows from the skew formula: an eligible node with no room for the pod still counts as a domain at zero and holds every other node to one pod. The constraint also needs enough nodes to be useful: on two nodes, six replicas become three and three.
For a workload where a zone outage should take about a third of capacity, and Pending pods are preferable to a bad placement:
topologySpreadConstraints:
- maxSkew: 1
topologyKey: topology.kubernetes.io/zone
whenUnsatisfiable: DoNotSchedule
minDomains: 3
nodeTaintsPolicy: Honor
labelSelector:
matchLabels:
app: ledger
matchLabelKeys:
- pod-template-hash
That is exact only when the replica count is a multiple of three: four replicas are placed 2/1/1, so one zone holds half. The cost of this one is the capacity coupling described above. Use it when an unschedulable pod is the better failure, and alert on Pending pods. The Pending pods decision tree covers the scheduler messages you get.
With StatefulSets, pod-template-hash doesn't exist. Their pods carry controller-revision-hash instead (the StatefulSet controller sets it), but the documentation only suggests pod-template-hash, for Deployments. Leave matchLabelKeys out there.
Checking the result
# Replicas per node and per zone
kubectl get pods -l app=checkout -o custom-columns=NODE:.spec.nodeName --no-headers | sort | uniq -c
kubectl get pods -l app=checkout -o json \
| jq -r '.items[].spec.nodeName' \
| xargs -I{} kubectl get node {} -o jsonpath='{.metadata.labels.topology\.kubernetes\.io/zone}{"\n"}' \
| sort | uniq -c
Run this after a rollout, a scale-down and a node pool upgrade: the distribution changes then, and no constraint is evaluated for the pods that remain.
Checklist
- Nodes carry both topology labels.
- Every replicated service has a spread constraint, or the cluster defaults have been tightened.
- The selector matches the pod's own labels.
DoNotScheduleconstraints setnodeTaintsPolicy: Honorwhere tainted pools exist.- Zone constraints that must hold use
minDomains. - Deployments use
matchLabelKeys: [pod-template-hash]. - Required anti-affinity is only used where one-per-domain is intended, with replica count and surge checked against the number of domains.
- Someone looks at the actual distribution after rollouts and scale-downs, or a descheduler does.