Pod disruption budgets
- Version
- 1.37
- Since
- 1.21
- Review
- Draft
Summary#
A PodDisruptionBudget (PDB) limits how many healthy Pods can be evicted voluntarily through the Eviction API. During node maintenance, kubectl drain uses this API by default. The budget can refuse an eviction until enough matching Pods are Ready.
Syntax#
apiVersion: policy/v1
kind: PodDisruptionBudget
metadata:
name: ledger
spec:
selector:
matchLabels:
app: ledger
minAvailable: 2
Choose one threshold. minAvailable sets how many matching Pods must stay healthy. maxUnavailable sets how many of a controller's desired replicas may be unavailable and requires the selected Pods to share one controller. Either field takes an integer or a percentage string such as "25%". Kubernetes rounds percentages up, so maxUnavailable: "25%" can allow eviction of the only replica.
Example#
Set KUBECONFIG to a disposable cluster's kubeconfig. The example uses two replicas and sends two eviction requests for the same Pod, changing the budget between requests. The cluster pulls busybox:1.37 if it is missing locally.
(
set -eu
: "${KUBECONFIG:?Set KUBECONFIG to the kubeconfig for a disposable cluster}"
ns="ref-pdb-$$"
kubectl create namespace "$ns" >/dev/null
trap 'kubectl delete namespace "$ns" >/dev/null' EXIT
trap 'exit 1' HUP INT TERM
kubectl create deployment ledger -n "$ns" --replicas=2 \
--image=busybox:1.37 -- sleep 3600 >/dev/null
kubectl rollout status -n "$ns" deployment/ledger --timeout=120s >/dev/null
kubectl create -n "$ns" -f - >/dev/null <<'EOF'
apiVersion: policy/v1
kind: PodDisruptionBudget
metadata:
name: ledger
spec:
selector:
matchLabels:
app: ledger
minAvailable: 2
EOF
kubectl wait -n "$ns" --for=jsonpath='{.status.currentHealthy}'=2 \
pdb/ledger --timeout=60s >/dev/null
pod=$(kubectl get pods -n "$ns" -l app=ledger -o jsonpath='{.items[0].metadata.name}')
evict() {
kubectl create --raw "/api/v1/namespaces/$ns/pods/$pod/eviction" -f - <<EOF
{"apiVersion":"policy/v1","kind":"Eviction","metadata":{"name":"$pod","namespace":"$ns"}}
EOF
}
if result=$(evict 2>&1); then
printf '%s\n' 'Expected the budget to refuse the eviction' >&2
exit 1
fi
case "$result" in
*TooManyRequests*'disruption budget'*) printf '%s\n' 'minAvailable=2: eviction refused by budget (HTTP 429)' ;;
*) printf '%s\n' "$result" >&2; exit 1 ;;
esac
kubectl patch -n "$ns" pdb ledger --type=merge \
-p '{"spec":{"minAvailable":null,"maxUnavailable":1}}' >/dev/null
kubectl wait -n "$ns" --for=jsonpath='{.status.disruptionsAllowed}'=1 \
pdb/ledger --timeout=60s >/dev/null
evict >/dev/null
kubectl wait -n "$ns" --for=delete "pod/$pod" --timeout=90s >/dev/null
printf '%s\n' 'maxUnavailable=1: eviction allowed, original Pod removed'
)
Expected output#
minAvailable=2: eviction refused by budget (HTTP 429)
maxUnavailable=1: eviction allowed, original Pod removed
The Pod name and replacement timing can vary. The example reports the budget decision and checks that the allowed eviction removed the selected Pod.
Common mistakes#
Cannot evict pod as it would violate the pod's disruption budget. is the answer, with HTTP status 429, to an eviction that the budget does not allow at that moment.
Check the selector and kubectl get pdb before draining a node. A selector of {} in policy/v1 covers every Pod in the namespace. With zero disruptions allowed, check how many selected Pods are Ready and whether the budget leaves room for maintenance.
Use the Eviction API to exercise the budget. Direct Pod deletion and a workload controller's rolling update bypass the budget. A PDB also cannot prevent involuntary losses such as a node failure.
Version applicability#
policy/v1 PodDisruptionBudget is available since Kubernetes 1.21. The complete example also needs policy/v1 Eviction, available since 1.22, and a kubectl client with JSONPath waits, introduced in 1.23. It targets a Kubernetes 1.37 server.
Sources#
- Attribution
- DevLookup contributors
- License
- CC-BY-4.0