Topology spread
Spread replicas instead of guessing placement
Suppose three catalog replicas are distributed across two fictional zones: east has two and west has one. A fourth replica should not make the distribution more uneven. A topology spread constraint uses labels on nodes to define domains and a Pod label selector to identify the workloads to count. It is a scheduling policy, not a promise of service uptime.
Calculate a hard skew
For a single DoNotSchedule constraint, assume the incoming Pod matches the selector, all counted Pods are in its namespace, both zones are eligible, and other filters pass. With counts east=2, west=1, global minimum is 1. Adding to east gives 3−1=2, which exceeds maxSkew 1. Adding to west gives 2−1=1, so west passes. The formula evaluates the prospective target count against the existing global minimum, not a newly recomputed minimum after placement.
Describe the constraint in YAML
This static Pod manifest uses a selector matching its own label. A Deployment would place the constraint under spec.template.spec for all new replicas. topologyKey uses the standard zone label, and minDomains specifies the minimum number of eligible domains for the calculation. Honor policies explicitly consider affinity and taints when determining domains; the pinned patch and schema are not verified. Do not deploy the placeholder image.
apiVersion: v1kind: Podmetadata: name: catalog-spread namespace: schedule-study labels: app: catalog-workerspec: automountServiceAccountToken: false restartPolicy: Never activeDeadlineSeconds: 120 securityContext: runAsNonRoot: true runAsUser: 10001 seccompProfile: type: RuntimeDefault containers: - name: worker image: example.invalid/bytemind/worker:offline resources: requests: cpu: 400m memory: 256Mi limits: cpu: 500m memory: 384Mi securityContext: allowPrivilegeEscalation: false readOnlyRootFilesystem: true capabilities: drop: - ALL topologySpreadConstraints: - maxSkew: 1 topologyKey: topology.kubernetes.io/zone whenUnsatisfiable: DoNotSchedule labelSelector: matchLabels: app: catalog-worker minDomains: 2 nodeAffinityPolicy: Honor nodeTaintsPolicy: HonorReason about missing domains and soft spread
If only east is eligible and minDomains is 2, the comparison baseline is zero. With one matching Pod already in east, another would give skew 2 and fail; with zero it can pass. minDomains is not a request to create more zones. ScheduleAnyway instead contributes a preference toward balanced placement and does not reject solely for this spread constraint; minDomains is used with DoNotSchedule only. Multiple hard spread rules must all be satisfied, and other scheduling filters still apply.
Avoid availability assumptions
A Pod label selector that does not match the incoming Pod can lead to surprising counts; review template labels and rollout overlap. Nodes missing the topology label cannot be used normally for that hard rule. A domain can count for spread purposes even if some other scheduling constraint prevents actual placement. Existing Pods are not automatically redistributed after node loss or a label change merely because spread is configured. Check ready endpoints, capacity, disruption policy and application dependencies separately.
Practice
Run the five offline spread scenarios in spread-cases.json. The script assumes the eligible domain list has already been derived and does not reproduce the scheduler. Explain when hard constraints are worth leaving a replica Pending and when a soft preference better fits the availability goal.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Topology spread — knowledge check
Apply scheduling concepts using synthetic offline scenarios.
Counts east=2 and west=1, maxSkew=1, both eligible. Where can a new matching Pod pass the hard spread rule?
Only one domain is eligible, minDomains=2, with one matching Pod already there. What is the global minimum?
Which mode expresses a soft spread preference?
What defines a topology domain in this example?
Does adding a spread constraint guarantee all existing replicas move into balanced positions?