Sub topic 8
Forcing spread across a minimum number of domains
This chapter assumes you can write a topologySpreadConstraints block and calculate skew by hand. It also assumes you know the difference between DoNotSchedule and ScheduleAnyway.
The problem minDomains solves
Skew compares a domain's pod count with the global minimum across the domains that currently exist. Suppose a cluster has nodes in only two zones, and you want pods spread over three. With maxSkew: 1, the scheduler is happy with 3 pods in zone a and 3 in zone b, because the skew is 0. The constraint never notices that a third zone should exist.
minDomains fixes this. It sets the minimum number of eligible domains the scheduler expects to see.
How it changes the calculation
If the number of eligible domains is lower than minDomains, the scheduler treats the global minimum as 0. If the number is equal or higher, minDomains has no effect. Without the field, the scheduler behaves as if minDomains were 1.
With a global minimum of 0, a domain can only hold maxSkew pods. With maxSkew: 1, each existing zone accepts one pod and the rest wait.
| Eligible zones | minDomains | Treated minimum | Result with maxSkew 1 |
|---|---|---|---|
| 2 | 3 | 0 | 1 pod per zone, others Pending |
| 3 | 3 | Real minimum | Normal spread |
| 4 | 3 | Real minimum | Normal spread |
Example
apiVersion: apps/v1
kind: Deployment
metadata:
name: web
spec:
replicas: 5
selector:
matchLabels:
app: web
template:
metadata:
labels:
app: web
spec:
topologySpreadConstraints:
- maxSkew: 1
minDomains: 3
topologyKey: topology.kubernetes.io/zone
whenUnsatisfiable: DoNotSchedule
labelSelector:
matchLabels:
app: web
containers:
- name: web
image: nginx:1.27Say the cluster has nodes in only two zones. The scheduler places the first pod in zone a. It places the second in zone b. The third pod would raise a zone to 2 pods against a treated minimum of 0, so the skew would be 2. That exceeds maxSkew, and the pod stays Pending. The same applies to pods four and five.
A cluster autoscaler can read the pending pods and add a node in a third zone. Once three zones exist, the real minimum applies and the pending pods schedule.
Rules and gotchas
minDomainsmust be greater than 0.- You can set it only when
whenUnsatisfiableisDoNotSchedule. The API server rejects it withScheduleAnyway. - A pod that can never be placed stays
Pendinguntil another domain appears. UseminDomainsonly if you can add capacity in the missing domains. - Setting
minDomainshigher than the number of zones your cloud region offers leaves pods pending permanently. - It does not rebalance running pods. It only affects placement of new pods.
To see why a pod waits, run kubectl describe pod <name>. The scheduler event reports that the pod didn't match the topology spread constraints.
Exercise
A Deployment with 4 replicas uses maxSkew 1, minDomains 3 and DoNotSchedule on the zone key. The cluster has nodes in 2 zones, and no pods are running yet. How many pods get scheduled, and what would let the rest run?
Show answer
Two pods get scheduled, one in each zone. With fewer than 3 eligible zones, the global minimum is treated as 0, so each zone accepts only maxSkew (1) pod. The other two pods stay Pending. They schedule once nodes appear in a third zone, for example through the cluster autoscaler. They would also schedule if you lowered minDomains to 2 or less.