Sub topic 12
Protecting workloads with PodDisruptionBudgets
This chapter assumes you can read Deployment and Pod manifests, use kubectl drain, and know that a node upgrade moves Pods off one node at a time. It covers how a PodDisruptionBudget (PDB) limits that movement.
What a PodDisruptionBudget does
A PDB is an object that sets how many Pods of a workload may be down at once during a voluntary disruption. Voluntary disruptions are actions an operator or controller starts on purpose, such as draining a node for an upgrade. Crashes, node hardware failures, and out-of-memory kills are involuntary. A PDB cannot prevent those.
Draining uses the Eviction API. Before the API evicts a Pod, it checks every PDB that selects that Pod. If the eviction would break a budget, the API refuses it. kubectl drain then retries until the eviction is allowed or the command times out.
Writing a PDB
apiVersion: policy/v1
kind: PodDisruptionBudget
metadata:
name: web-pdb
spec:
minAvailable: 2
selector:
matchLabels:
app: webThis budget keeps at least two Pods labeled app: web available. You can set one of two fields, but not both:
minAvailable: the fewest Pods that must stay available.maxUnavailable: the most Pods that may be unavailable.
Each accepts an integer or a percentage. Use policy/v1. The older policy/v1beta1 was removed in Kubernetes 1.25, so this is also an API migration item from earlier chapters.
How a drain interacts with the budget
Suppose web has three replicas and minAvailable: 2. One Pod can be evicted. The drain of the next node waits until the replacement Pod is Ready, because only then are three Pods available again. This is what makes rolling node upgrades safe for the service.
Common errors
- A drain hangs forever. Set
minAvailableequal to the replica count, ormaxUnavailable: 0, and no eviction is ever allowed. Check withkubectl get pdb. IfALLOWED DISRUPTIONSis0, the drain cannot proceed. - A single replica with a PDB. One replica and
minAvailable: 1blocks every drain of its node. Run at least two replicas, or accept downtime for that workload. - The selector matches nothing, or too much. A PDB whose selector matches no Pods protects nothing. Overlapping PDBs on the same Pod can also cause refusals that are hard to trace.
- Unready Pods count against you. Only Ready Pods count as available. If a rollout is stuck, the budget has no spare capacity.
kubectl get pdb
kubectl describe pdb web-pdbThe describe output shows current and desired healthy Pods and the number of allowed disruptions.
Exercise
A Deployment has 3 replicas and a PDB with minAvailable: 3. You run kubectl drain on a node that hosts one of the Pods. What happens, and how do you fix it?
Show answer
The drain hangs because the API refuses the eviction. Allowed disruptions is 0, since evicting any Pod would leave fewer than 3 available. Fix it by lowering minAvailable to 2 (or using maxUnavailable: 1), or by adding a replica and keeping the budget below the new replica count.