PodDisruptionBudgets: draining Kubernetes nodes without taking your app down
A PodDisruptionBudget tells kubectl drain and cluster autoscalers how many replicas must stay up. How to write one, check it, and avoid budgets that block drains.
Contents
The problem with maintenance
Sooner or later every Kubernetes node needs to go away for a while: a kernel update, a node pool upgrade, or the cluster autoscaler deciding the node is no longer needed. The standard tool for this is kubectl drain, which cordons the node and then evicts every pod on it.
If your Deployment has three replicas and two of them happen to sit on the same node, a drain can take two of the three down at once. If you drain two nodes in parallel, you might lose all of them. Kubernetes will reschedule the pods elsewhere, but between eviction and the new pods becoming ready, your service runs short or not at all.
A PodDisruptionBudget (PDB) is how you tell Kubernetes what “too many at once” means for your workload.
Voluntary vs. involuntary disruptions
It helps to be clear about what a PDB covers. Kubernetes separates two kinds of disruption:
- Voluntary: someone or something asks to remove a pod through the Eviction API.
kubectl drain, node upgrades in managed clusters and the cluster autoscaler all work this way. - Involuntary: a node crashes, the kernel kills a container for memory, a VM disappears. Nobody asks permission.
A PDB only applies to the first kind. It cannot stop hardware from failing, which is why you still need enough replicas spread across nodes. It also does not apply to a plain kubectl delete pod, and it does not control Deployment rolling updates; those use the maxUnavailable and maxSurge settings of the Deployment strategy.
Writing a PDB
A PDB selects pods by label and sets either minAvailable or maxUnavailable (not both). For a three-replica API:
apiVersion: policy/v1
kind: PodDisruptionBudget
metadata:
name: api-pdb
namespace: shop
spec:
maxUnavailable: 1
selector:
matchLabels:
app: api
This says: at most one app: api pod may be voluntarily disrupted at a time. When an eviction would push the count of unavailable pods above one, the API server refuses it, and kubectl drain keeps retrying until a replacement pod is ready somewhere else.
Both fields also accept percentages, such as maxUnavailable: "25%". I usually prefer maxUnavailable over minAvailable, because it keeps working when you scale the Deployment up or down. A minAvailable: 3 on a Deployment you later scale to three replicas means zero disruptions are allowed.
Make sure the selector matches the labels in the pod template, not just the Deployment’s own labels. A PDB that selects nothing protects nothing, and Kubernetes will not warn you about it.
Checking that it works
Apply it and look at the status:
kubectl apply -f api-pdb.yaml
kubectl get pdb -n shop
NAME MIN AVAILABLE MAX UNAVAILABLE ALLOWED DISRUPTIONS AGE
api-pdb N/A 1 1 12s
The column to watch is ALLOWED DISRUPTIONS. If it shows 0 while all your pods are healthy, a drain will hang on this workload. For more detail, including how many pods the PDB matched:
kubectl describe pdb api-pdb -n shop
Then try a real drain on a test node:
kubectl drain node-2 --ignore-daemonsets --delete-emptydir-data --timeout=5m
While it runs, you will see messages that an eviction was blocked because it would violate the disruption budget, followed by retries. That is the PDB doing its job. When the drain finishes, bring the node back with kubectl uncordon node-2.
Budgets that block every drain
The most common mistake is a PDB that can never be satisfied. Typical cases:
- A single replica with
minAvailable: 1(ormaxUnavailable: 0). No eviction is ever allowed, so the node cannot be drained until someone intervenes. Managed node pool upgrades may wait for a timeout and then proceed anyway, or stall, depending on the provider. - Pods that are not ready. If one replica is stuck in CrashLoopBackOff, it already counts as unavailable and uses up the budget, so healthy pods cannot be evicted either.
- Overlapping PDBs. If two PDBs select the same pod, eviction of that pod fails. Keep one PDB per workload.
For the stuck-pod case, recent Kubernetes versions support unhealthyPodEvictionPolicy:
spec:
maxUnavailable: 1
unhealthyPodEvictionPolicy: AlwaysAllow
selector:
matchLabels:
app: api
With AlwaysAllow, pods that are running but not ready can be evicted regardless of the budget, so a broken replica does not hold the whole node hostage. Check that your cluster version supports the field before relying on it.
If a workload really must run as a single replica, be honest about it: either accept that drains will interrupt it and skip the PDB, or plan the maintenance window by hand.
A short checklist
- Run at least two replicas of anything that should survive a drain, and spread them with pod anti-affinity or topology spread constraints.
- Add a PDB with
maxUnavailable: 1(or a percentage for larger workloads). - Make sure readiness probes are accurate, because the budget counts ready pods.
- Check
kubectl get pdb -Afor entries with 0 allowed disruptions before a node upgrade. - Test a drain on a non-critical node once, instead of finding out during an upgrade.
A PDB is a few lines of YAML, but it turns node maintenance from something you schedule late at night into something that can happen during the working day.