Sub topic 12
Creating a PodDisruptionBudget
This chapter assumes you can write Deployments with labels and selectors and apply manifests with kubectl. It also assumes you know the difference between voluntary and involuntary disruptions from the previous chapter.
What a budget does
A PodDisruptionBudget (PDB) limits how many pods of a workload can be down at the same time because of voluntary disruptions. Examples are kubectl drain and node upgrades. A PDB cannot prevent involuntary disruptions such as a node crash. Pods lost that way still count against the budget, so a crash can leave less room for later evictions.
Writing a budget
This budget allows at most one pod matching app=web to be voluntarily evicted at a time:
apiVersion: policy/v1
kind: PodDisruptionBudget
metadata:
name: web-pdb
spec:
maxUnavailable: 1
selector:
matchLabels:
app: webThe selector picks the pods the budget protects. It should match the labels in the Deployment's pod template. A selector that matches nothing protects nothing, and Kubernetes gives no error.
minAvailable or maxUnavailable
Set exactly one of these fields. The API rejects a PDB that sets both.
| Field | Meaning | Example with 6 replicas |
|---|---|---|
| `minAvailable` | Pods that must stay available | `4` allows 2 evictions |
| `maxUnavailable` | Pods that may be unavailable | `1` allows 1 eviction |
Both fields accept an integer or a percentage string such as "50%". Kubernetes rounds percentages up to a whole pod, so "50%" of 6 pods is 3. For maxUnavailable, rounding up means a small percentage on a small workload still allows one eviction.
maxUnavailable is usually the safer choice for a Deployment. It keeps working when you scale the replica count. A fixed minAvailable does not. If you set minAvailable: 6 on a 6-replica Deployment, no pod can ever be evicted.
Applying and checking it
kubectl apply -f web-pdb.yaml
kubectl get pdb web-pdb
kubectl get pdb web-pdb -o jsonpath='{.status.disruptionsAllowed}{"\n"}'The first command creates the budget. The second shows the budget's fields and its current number of allowed disruptions. The third prints only that number. With 6 healthy replicas and maxUnavailable: 1, the number is 1. The budget counts only pods that are Ready. If one pod is already failing, the allowed number drops to 0 and no further voluntary eviction goes through.
Placement versus eviction
The PDB acts at eviction time. Topology spread constraints act only when the scheduler places a pod. A PDB cannot change where pods run, and it cannot repair an uneven spread. The next chapters show how drains use the budget and how to read its status in more detail.
Exercise
A Deployment has 6 replicas, all Ready. You create a PDB with minAvailable: 4. One pod then crashes and is not yet Ready. How many more pods can a drain evict right now?
Show answer
One. Five pods are Ready, and the budget requires at least 4 to stay available, so disruptionsAllowed is 1. Once the crashed pod recovers, the allowed number goes back to 2.