Sub topic 5
Hard and soft constraints
This chapter assumes you can write a Deployment, read kubectl get pods -o wide output, and know that nodes carry zone and hostname labels. It also assumes you know how skew is calculated: the pod count in a domain minus the lowest pod count across all domains.
What `whenUnsatisfiable` controls
Every topology spread constraint must say what the scheduler does when placing a pod would push skew above maxSkew. The whenUnsatisfiable field has two values.
| Value | Type | Behavior when skew would exceed `maxSkew` |
|---|---|---|
| `DoNotSchedule` | Hard | The scheduler treats the node as unfit. If no node qualifies, the pod stays `Pending`. |
| `ScheduleAnyway` | Soft | The scheduler still places the pod. It gives higher scores to nodes that reduce skew. |
A hard constraint is a filter. A soft constraint is only a scoring preference, so other scoring rules, such as image locality or resource balance, can outweigh it.
A worked example
Take three zones and a maxSkew of 1. Zone a has 2 pods, zone b has 2 pods, and zone c has 1 pod. A new pod arrives.
- Placing it in
amakes the counts 3, 2, 1. Skew is 3 - 1 = 2, which is above 1. - Placing it in
bgives the same result. - Placing it in
cmakes the counts 2, 2, 2. Skew is 0.
With DoNotSchedule, only zone c nodes are candidates. If zone c has no free capacity, the pod stays Pending even though zones a and b have room. With ScheduleAnyway, the pod prefers zone c but may land in a or b.
Choosing between them
Use DoNotSchedule when an uneven placement is worse than a missing pod. A typical case is a quorum-based service that must survive the loss of one zone. Pair it with enough capacity in every zone, or pods will wait.
Use ScheduleAnyway when availability of the pod matters more than perfect balance. This suits per-node spreading in a large Deployment, or any workload that must scale out quickly during a traffic spike.
The Deployment from the topic's first example uses both. Zone spread is hard, so the workload never concentrates in one zone. Node spread is soft, so a full node never blocks scaling:
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: webSeeing the effect
When a hard constraint blocks a pod, the pod's events say so. Run this:
kubectl describe pod <pod-name>Look for a FailedScheduling event that mentions didn't match pod topology spread constraints. A soft constraint never produces this event, because it never rejects a node.
Exercise
Zones a, b and c hold 3, 3 and 2 pods. maxSkew is 1 and all zones have free capacity. Where can the next pod go under DoNotSchedule, and where under ScheduleAnyway?
Show answer
Under DoNotSchedule, only zone c. Placing the pod in a or b gives counts such as 4, 3, 2, a skew of 2. Under ScheduleAnyway, any zone is allowed, but zone c scores highest because it brings the counts to 3, 3, 3.