Sub topic 6
Spreading across zones and nodes together
This chapter assumes you can write a basic topologySpreadConstraints entry and calculate skew by hand. It also assumes you know the difference between DoNotSchedule and ScheduleAnyway. Here you will combine two constraints on one workload.
Why use more than one constraint
A single constraint spreads pods over one topology key. Spreading over zones alone can still put three pods of a zone on one node. Spreading over nodes alone can still put every pod in one zone if that zone holds most of the nodes.
You can list several constraints in one pod spec. Each constraint has its own topologyKey, maxSkew and whenUnsatisfiable.
How the scheduler combines them
The scheduler treats the constraints as a logical AND. A node is a candidate only if placing the pod there satisfies every DoNotSchedule constraint. Constraints with ScheduleAnyway do not filter nodes. They only lower the score of nodes that increase skew.
A common pattern
The usual choice is a hard constraint on zones and a soft constraint on nodes. This is the Deployment from the chapter plan. It has six replicas.
apiVersion: apps/v1
kind: Deployment
metadata:
name: web
spec:
replicas: 6
selector:
matchLabels:
app: web
template:
metadata:
labels:
app: web
spec:
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
containers:
- name: web
image: nginx:1.27With three zones, the zone constraint forces a 2/2/2 split, because a 3/2/1 split has skew 2. Within each zone, the scheduler prefers nodes that hold fewer web pods. If a zone has only one node, the soft constraint cannot be met there, and the pod still schedules.
Choosing the strictness of each constraint
| Key | Typical setting | Reason |
|---|---|---|
| Zone | `DoNotSchedule` | A zone outage is the failure you most need to survive. |
| Hostname | `ScheduleAnyway` | A hard node constraint can leave pods Pending when nodes are full or few. |
Pitfalls
- Every constraint must be satisfiable together. A hard zone constraint plus a hard hostname constraint can leave a pod Pending when a zone has too few nodes.
- Each constraint calculates skew independently. Satisfying one does not help with the other.
- Use the same
labelSelectorin all constraints, or the counts will cover different sets of pods.
Exercise
A Deployment has a hard zone constraint (maxSkew 1) and a hard hostname constraint (maxSkew 1). Zone A has one node, and it already holds one pod. Zone B has three nodes with one pod each. A new pod arrives. Where can it go?
Show answer
Only a node in zone B that holds no pod, or a node in zone A is ruled out. Zone counts are A=1 and B=3. Placing in B gives skew 3 against A's 1... after placement B=4 versus A=1, which is skew 3, so zone B fails. Placing in A gives A=2 versus B=3, skew 1, which passes the zone constraint. But zone A's only node already has a pod, so hostname counts become 2 on that node against 0 on empty nodes in B, skew 2, which fails. No node passes, so the pod stays Pending. Making the hostname constraint ScheduleAnyway would let it land in zone A.