Sub topic 17
Using both features on one Deployment
This chapter assumes you can write a Deployment, read a topology spread constraint, and create a PodDisruptionBudget (PDB). It also assumes you know that spread constraints act when pods are scheduled, while PDBs act when pods are evicted.
Why you need both
The two features act at different times, so neither can replace the other.
- A topology spread constraint decides where a new pod lands. It does nothing once the pod is running.
- A PDB limits how many pods voluntary evictions, such as
kubectl drain, can remove at once. It does not influence placement.
Spread alone leaves you exposed during maintenance, because a drain can evict several replicas at the same time. A PDB alone leaves you exposed to a zone failure, because all replicas may sit in one zone.
A combined manifest
This Deployment runs six replicas. The zone constraint is hard, the node constraint is soft, and the PDB allows one voluntary eviction at a time.
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
matchLabelKeys:
- pod-template-hash
- maxSkew: 1
topologyKey: kubernetes.io/hostname
whenUnsatisfiable: ScheduleAnyway
labelSelector:
matchLabels:
app: web
containers:
- name: web
image: nginx:1.27
---
apiVersion: policy/v1
kind: PodDisruptionBudget
metadata:
name: web-pdb
spec:
maxUnavailable: 1
selector:
matchLabels:
app: webBoth objects select pods with app: web. If the labels differ, the PDB protects nothing and the spread constraint counts the wrong pods.
How they interact during a drain
The PDB gates each eviction. The scheduler then applies the spread constraints to the replacement. With maxUnavailable: 1, the drain waits until the replacement is Ready before it evicts the next pod.
Choosing values that work together
- With six replicas across three zones, each zone holds two pods. Draining one node removes at most two, but the PDB lets only one go at a time, so the drain proceeds in steps.
- A hard zone constraint can leave a replacement Pending. If a zone has no spare capacity, skew would exceed
maxSkew. Keep spare capacity in every zone, or useScheduleAnywaywhere availability matters more than balance. - Do not set
maxUnavailable: 0, orminAvailableequal to the replica count. Every drain then blocks forever.
Exercise
A Deployment has 3 replicas, a PDB with minAvailable: 3, and a hard zone spread constraint. Why will kubectl drain hang, and what is the smallest PDB change that fixes it?
Show answer
With minAvailable equal to the replica count, disruptionsAllowed is 0, so the Eviction API rejects every eviction and the drain keeps retrying. Change it to minAvailable: 2 or maxUnavailable: 1, which allows one eviction at a time.