Keep apps up during maintenance with disruption budgets
In this task you'll stop maintenance and upgrades from taking too many replicas down at once.
A PodDisruptionBudget (PDB) tells Kubernetes how many of an app's pods must stay up during voluntary disruptions: drains, upgrades and the cluster autoscaler removing a node. You set either minAvailable (for example 2) or maxUnavailable (for example 1), and a label selector for the pods.
It doesn't protect against involuntary problems such as a node crashing; that's what replicas spread across nodes and zones are for. And a PDB that allows zero disruptions (for example maxUnavailable: 0, or minAvailable equal to the replica count) blocks every drain, so node upgrades get stuck.
Example. Fernhill Cloud's AKS upgrade stalled for an hour. A team's PDB said minAvailable: 3 for a deployment with exactly 3 replicas, so no pod could ever be evicted. They changed it to maxUnavailable: 1 and the upgrade finished.
Before you start
You need: Docker Desktop and kind on your computer (both free). Time: 25 minutes.
Where to look
- Terminal
- kubectl
Steps
- Create a deployment with 3 replicas, and a PDB with
maxUnavailable: 1that selects it. - Check it:
kubectl get pdbshould show ALLOWED DISRUPTIONS 1. - Drain a worker and watch pods move one at a time.
- Change the PDB to
minAvailable: 3, try the drain again, and read the 'Cannot evict pod' message. Then put it back.
Commands
kubectl create deployment web --image=nginx --replicas=3
kubectl create pdb web-pdb --selector=app=web --max-unavailable=1
kubectl get pdb web-pdb
kubectl drain <node> --ignore-daemonsets --delete-emptydir-data
Watch out for
- Cost
- Free on kind.
- Security
- Nothing to secure here; just review PDBs as part of platform standards.
- Common mistakes
- A PDB with zero allowed disruptions, and a PDB on a single-replica app (it can never be drained safely).
Clean up
Delete the PDB and the deployment, and uncordon any drained node.
Words to know
- PodDisruptionBudget
- A rule for how many of an app's pods may be taken down at once during voluntary disruptions.
- Voluntary disruption
- A planned removal of pods, such as a drain, upgrade or scale-in.
- Eviction API
- The polite way to remove a pod, which checks disruption budgets first.
Tick off each step and track your progress in the guide.
Open this task in DevOps, Hands On