Sub topic 10
What spread constraints do not do
This chapter assumes you can write a topologySpreadConstraints block and calculate skew. Spread constraints are narrower than they look, and most surprises come from expecting more of them.
They act only when a pod is scheduled
The scheduler checks a constraint at one moment: when it places a new pod. After that, the constraint is not enforced again. Kubernetes has no controller that watches skew and moves pods to fix it. The behavior is the same as IgnoredDuringExecution in node affinity.
Three common events can leave a workload unbalanced:
- Pods are deleted or a node fails. Pods in one zone disappear, and the pods in other zones stay where they are.
- You scale down. The ReplicaSet controller picks which pods to remove, and it does not aim to keep zones balanced.
- Cluster capacity changes. A new zone or node appears, but running pods do not move onto it.
Suppose six web pods sit two per zone across zones a, b and c. Zone c loses its nodes, and its pods are recreated in a and b. Zone c comes back empty, so the distribution is now 3, 3, 0. Nothing moves the pods back. Only the next new pod is steered toward zone c.
Rebalancing needs another tool
To fix skew, delete pods so the scheduler places their replacements again. A rolling restart does this:
kubectl rollout restart deployment/web
kubectl get nodes -L topology.kubernetes.io/zone
kubectl get pods -l app=web -o custom-columns=NAME:.metadata.name,NODE:.spec.nodeNameCompare the node column with the zone labels to see the new distribution. For continuous correction, the separate Descheduler project has a RemovePodsViolatingTopologySpreadConstraint strategy. It evicts pods that break their constraints, so they are scheduled again.
Other limits
- Capacity is not considered. If a zone has no free CPU or memory, a hard constraint can leave a pod
Pending. The constraint does not make room. - Only matching pods count. The
labelSelectorselects pods in the pod's own namespace. Pods from other namespaces or with different labels do not count toward skew. - Domains come from eligible nodes. A zone with no eligible nodes is not a domain, so it is not counted. Use
minDomainsif you need to require a minimum number of domains. - Soft constraints can be ignored. With
ScheduleAnyway, the scheduler prefers a balanced placement but may still violate the constraint. - Evictions are not covered. A spread constraint says nothing about how many pods may be removed during a drain. A PodDisruptionBudget handles that.
Exercise
A Deployment has maxSkew: 1 with DoNotSchedule across three zones. You scale it from 6 pods to 3, and the remaining pods end up all in zone a. Will Kubernetes move them to balance the zones?
Show answer
No. Spread constraints apply only at scheduling time, and scale-down does not reschedule pods. The pods stay in zone a until they are deleted and recreated, for example by a rollout restart or by the Descheduler. Scaling back up places new pods in zones b and c first, because that reduces skew.