All guides

PodDisruptionBudgets: keep pods running through node drains

A node drain tries to evict all the pods on the node. A PodDisruptionBudget limits how many pods of one application are down at the same time from voluntary disruptions.

Over the shoulder of someone working in a code editor on a laptop

Voluntary and involuntary disruptions

The Kubernetes documentation sorts disruptions into two groups.

  • Involuntary: a hardware failure, a kernel panic, a virtual machine that disappears, a network partition, or the eviction of a pod because the node is out of resources. A budget cannot prevent these.
  • Voluntary: actions started by the application owner or the cluster administrator, such as draining a node for repair or upgrade, or draining a node to scale the cluster down.

A PodDisruptionBudget (PDB) limits how many pods of a replicated application are down at once from voluntary disruptions. Tools respect a budget by calling the Eviction API in place of deleting pods directly. Not every voluntary disruption is covered: deleting a pod or a Deployment bypasses the budget. A rolling update is not limited by a PDB either, and its failure handling is configured in the workload's own spec.

Involuntary disruptions do count against the budget.

A budget for a Deployment

The number in maxUnavailable is yours to choose:

YAML

apiVersion: policy/v1
kind: PodDisruptionBudget
metadata:
  name: my-app
spec:
  maxUnavailable: 1
  selector:
    matchLabels:
      app: my-app
  unhealthyPodEvictionPolicy: AlwaysAllow

Use the same selector as the workload's controller. Write the budget in policy/v1, because policy/v1beta1 is no longer served as of Kubernetes v1.25. After you apply it, check its status:

Shell

kubectl get poddisruptionbudgets

In the documentation's example, a budget with no matching pods shows 0 under ALLOWED DISRUPTIONS. A non-zero value means the disruption controller has seen and counted the matching pods.

minAvailable or maxUnavailable

A budget takes only one of the two fields. Each accepts a number or a percentage.

  • minAvailable is the number of pods that must still be available after an eviction.
  • maxUnavailable is the number of pods that can be unavailable after an eviction.

The documentation recommends maxUnavailable, because it responds automatically to changes in the number of replicas. It can only be used when all the selected pods have the same controller managing them. For a quorum-based application such as etcd, the documentation gives two options: maxUnavailable: 1, or minAvailable set to the quorum size, such as 3 when the scale is 5.

Percentages round up. With seven pods, minAvailable: "50%" means four pods must be available. A pod counts as healthy when its Ready condition is true.

Settings that block every drain

Each of these requires zero voluntary evictions:

  • maxUnavailable set to 0 or 0%
  • minAvailable set to 100%
  • minAvailable set to the number of replicas

The semantics of a PodDisruptionBudget permit all three. If you then drain a node that runs one of those pods, the drain never completes.

For a single-instance stateful application, the documentation lists two possible solutions. Run it with no budget and tolerate occasional downtime. Or set maxUnavailable to 0 and agree, outside Kubernetes, that the cluster operator consults you before termination. You then prepare for downtime and delete the budget.

The unhealthyPodEvictionPolicy field

This field, stable since Kubernetes v1.31, defines when unhealthy pods are considered for eviction.

  • IfHealthyBudget is the default. A pod that is running and not yet healthy can be evicted only if the application has at least its desired number of healthy pods. The documentation notes the cost: an application with pods in CrashLoopBackOff can block a node drain.
  • AlwaysAllow lets running pods that are not yet healthy be evicted regardless of the budget.

The documentation recommends AlwaysAllow, to support the eviction of misbehaving applications during a node drain.

How node upgrades use budgets

Shell

kubectl drain <node-name> --ignore-daemonsets

kubectl drain marks the node unschedulable and evicts its pods through the Eviction API. When a budget does not allow an eviction, the API answers 429 Too Many Requests. kubectl retries the failed requests until all the pods are terminated or a configurable timeout is reached. The --timeout flag of kubectl drain sets the length of time to wait before giving up, and zero means infinite. An evicted pod is terminated gracefully and its terminationGracePeriodSeconds is honored. You can run drains for different nodes in parallel, and they still respect the budget.

Managed services document their own behavior. Amazon EKS states the rule for a managed node group update: pods get 15 minutes to leave a node, and when they are still there and no force flag is set, the upgrade phase fails with a PodEvictionFailure error. It lists an aggressive PDB among the known reasons.

The --disable-eviction flag makes kubectl drain delete pods and bypass the check of PodDisruptionBudgets. The reference says to use it with caution.

Mistakes to avoid

  1. More than one budget for the same pod. The Eviction API answers with a 500 Internal Server Error.
  2. No room for the replacement. In the documentation's example, a drain blocks because the cluster lacks the resources to schedule the new pod, and the administrator has to add a node.
  3. Treating a budget as a guarantee. It protects against voluntary evictions only, so a node failure can still take the application below the budget.
  4. A stuck eviction left to retry. The documentation's advice is to pause the automated operation and investigate the application before you restart it.

Sources

Want a second pair of eyes on yours? We do this work for a living.

Book a call

More guides