How to Use a Kubernetes PodDisruptionBudget
Three replicas does not mean high availability if all three land on the same node. A PodDisruptionBudget sets a floor on how many pods must stay up during a drain or upgrade, and topologySpreadConstraints forces the scheduler to spread them across nodes in the first place.
What a PodDisruptionBudget actually does
A PodDisruptionBudget (PDB) is a policy object that limits voluntary disruptions - node
drains, cluster upgrades, or manual kubectl delete pod. It tells Kubernetes: "never bring
this Deployment below N running pods." Without one, a drain can evict every pod on a node,
including all three replicas if they happen to be co-located.
apiVersion: policy/v1
kind: PodDisruptionBudget
metadata:
name: web-pdb
spec:
minAvailable: 2
selector:
matchLabels:
app: web
With minAvailable: 2 and 3 replicas, a drain is only allowed to proceed if at least
2 pods remain schedulable elsewhere. The drain blocks until that constraint is satisfied.
The missing half: topologySpreadConstraints
A PDB protects during disruption, but the scheduler still has to place the pods first.
topologySpreadConstraints tells the scheduler to spread replicas across distinct nodes
(or zones) at scheduling time, so the disruption scenario never gets bad to begin with.
Add this inside the spec.template.spec of the Deployment:
topologySpreadConstraints:
- maxSkew: 1
topologyKey: kubernetes.io/hostname
whenUnsatisfiable: DoNotSchedule
labelSelector:
matchLabels:
app: web
maxSkew: 1 means no node can have more than one extra pod compared to the least-loaded
node. whenUnsatisfiable: DoNotSchedule makes the pod stay Pending rather than violate
the spread - a hard guarantee, not a hint.
Putting it together in one file
Both objects go in the same deployment.yaml, separated by ---:
apiVersion: policy/v1
kind: PodDisruptionBudget
metadata:
name: web-pdb
spec:
minAvailable: 2
selector:
matchLabels: {app: web}
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: web
spec:
replicas: 3
selector:
matchLabels: {app: web}
template:
metadata:
labels: {app: web}
spec:
topologySpreadConstraints:
- maxSkew: 1
topologyKey: kubernetes.io/hostname
whenUnsatisfiable: DoNotSchedule
labelSelector:
matchLabels: {app: web}
containers:
- name: web
image: nginx:stable
Apply and verify:
kubectl apply -f deployment.yaml
kubectl get pdb web-pdb # check ALLOWED DISRUPTIONS
kubectl get pods -o wide # confirm replicas are on different nodes
What to check in production
- PDB + HPA together - set
minAvailablelower than your HPA minimum so scale-down events are not permanently blocked. - Zone spread - change
topologyKeytotopology.kubernetes.io/zonefor multi-AZ clusters; combine withkubernetes.io/hostnamefor belt-and-suspenders. - Every workload, not just the important ones - PDB-less Deployments are the ones that disappear during a routine node upgrade and cause an on-call page.
Want to try it hands-on? HeyDevJob gives you this exact setup in a live cloud workspace in your browser - edit it, run it, and see it work. Free, nothing to install.
Try it in a workspace →What you'll practice
- Writing a PodDisruptionBudget with minAvailable and a label selector
- Adding topologySpreadConstraints to a Deployment to spread replicas across nodes
- Applying both objects from one YAML file and verifying with kubectl get pdb
FAQ
What does a PodDisruptionBudget do in Kubernetes?
A PDB limits voluntary disruptions - drains, upgrades, manual deletes - by setting a floor on how many pods must remain running. With minAvailable: 2 and 3 replicas, Kubernetes will block a node drain until at least 2 pods can still run elsewhere.
What is the difference between a PDB and topologySpreadConstraints?
A PDB acts at disruption time - it prevents eviction below a minimum count. topologySpreadConstraints act at scheduling time - they force the scheduler to place replicas on different nodes or zones. You need both: spread prevents the bad placement, PDB enforces the floor if a drain still happens.
What does maxSkew: 1 mean in topologySpreadConstraints?
maxSkew is the maximum allowed difference in replica count between the most-loaded and least-loaded topology domain (e.g. node). maxSkew: 1 means at most one extra pod per node compared to the node with the fewest pods.
What is the purpose of a PodDisruptionBudget?
A PodDisruptionBudget (PDB) sets a minimum number or percentage of pods that must stay available during voluntary disruptions like node drains and upgrades, so maintenance cannot take your service below a safe replica count.
Keep learning
Learn it by doing. Open this in a live cloud workspace, make the change yourself, and keep a record of the work you can share.
Open the workspace →