Introduction
When managing Kubernetes clusters, rolling updates, node drains, and scaling events can cause temporary downtime for applications. In a production environment, even a brief outage can impact users.
This is where Pod Disruption Budgets (PDBs) come in!
A Pod Disruption Budget ensures that a minimum number of pods remain available during voluntary disruptions like:
Node upgrades
Cluster maintenance
Manual pod evictions
By implementing PDBs, we can prevent downtime while still allowing controlled disruptions for cluster maintenance. Let’s see how to build a highly available application setup using PDBs in Kubernetes.
Step 1: Deploying a Sample Application
Let’s start with a simple Nginx deployment with three replicas.
apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx-deployment
namespace: default
labels:
app: nginx
spec:
replicas: 3
selector:
matchLabels:
app: nginx
template:
metadata:
labels:
app: nginx
spec:
containers:
- name: nginx
image: nginx:latest
ports:
- containerPort: 80
Apply the deployment:
kubectl apply -f nginx-deployment.yaml
Check if all pods are running:
kubectl get pods -l app=nginx
Step 2: Creating a Pod Disruption Budget (PDB)
Now, let’s create a PDB to ensure that at least one pod is always running during disruptions.
apiVersion: policy/v1
kind: PodDisruptionBudget
metadata:
name: nginx-pdb
namespace: default
spec:
minAvailable: 1
selector:
matchLabels:
app: nginx
Apply the PDB:
kubectl apply -f nginx-pdb.yaml
Verify the PDB:
kubectl get poddisruptionbudget
Expected output:
NAME MIN AVAILABLE MAX UNAVAILABLE ALLOWED DISRUPTIONS AGE
nginx-pdb 1 N/A 2 10s
This means at least 1 pod must always be running, and up to 2 pods can be disrupted at a time.
Step 3: Testing the Pod Disruption Budget
Let’s try to evict a pod and see how the PDB enforces availability:
kubectl drain <node-name> --ignore-daemonsets --force
If this eviction violates the PDB, Kubernetes will block the eviction to maintain the availability constraint.
To manually evict a pod:
kubectl delete pod <pod-name> --grace-period=0 --force
If this violates the PDB rules, Kubernetes will prevent the pod deletion.
Conclusion
Kubernetes Pod Disruption Budgets help maintain application availability during voluntary disruptions.
They ensure that a minimum number of pods always remain available.
Useful for high-availability applications and stateful workloads like databases.
With PDBs, you can perform cluster upgrades and maintenance without worrying about breaking your application’s availability!
Would you use PDBs in your setup? Let me know your thoughts in the comments! 