Configuring Pod Topology Spread Constraints

1. Understanding Topology Spread

Distribute pods evenly across failure domains (zones, nodes) to maximize availability.

vs Pod Anti-AffinityDetail
GranularityQuantitative skew, not binary
PerformanceMore efficient at scale

2. Setting Maximum Skew

topologySpreadConstraints:
- maxSkew: 1
  topologyKey: topology.kubernetes.io/zone
  whenUnsatisfiable: DoNotSchedule
  labelSelector: { matchLabels: { app: web } }
maxSkewEffect
1Strictest balance (±1 pod)
NAllow up to N pod difference

3. Configuring When Unsatisfiable

ValueBehavior
DoNotSchedulePod stays Pending (hard)
ScheduleAnywaySoft preference, scores nodes

4. Using Topology Keys

KeyGoal
topology.kubernetes.io/zoneAZ spread (HA)
kubernetes.io/hostnameNode spread
topology.kubernetes.io/regionRegion spread

5. Configuring Label Selectors

labelSelector:
  matchLabels: { app: web }
  matchExpressions:
  - { key: tier, operator: In, values: [frontend] }

6. Setting Match Label Keys

matchLabelKeys: [pod-template-hash]   # 1.27+ stable
UseDetail
Per-revision spreadDistinct rollouts spread independently

7. Using Min Domains

minDomains: 3              # require ≥3 zones (1.27+ stable)
whenUnsatisfiable: DoNotSchedule

8. Combining Multiple Constraints

Example: Zone + node spread

topologySpreadConstraints:
- { maxSkew: 1, topologyKey: topology.kubernetes.io/zone, whenUnsatisfiable: DoNotSchedule, labelSelector: { matchLabels: { app: web } } }
- { maxSkew: 1, topologyKey: kubernetes.io/hostname,      whenUnsatisfiable: ScheduleAnyway, labelSelector: { matchLabels: { app: web } } }

9. Understanding Node Inclusion Policy

FieldEffect
nodeAffinityPolicyHonor / Ignore — filter nodes by pod's affinity before counting
nodeTaintsPolicyHonor / Ignore tainted nodes

10. Troubleshooting Spread Issues

SymptomCause
Pending FailedSchedulingmaxSkew=1 + DoNotSchedule too strict
Imbalanced placementInitial pods skew baseline; use cluster autoscaler
Wrong domains countedSet nodeAffinityPolicy: Honor