
Ever wondered how to keep your Kubernetes applications running smoothly during upgrades or maintenance ?
Pod Disruption Budgets (PDBs) — your secret weapon for ensuring high availability and reliability.
In this guide, we’ll break down what PDBs are, why they matter, and how to use them like a pro!
When managing Kubernetes workloads, it’s crucial to understand and effectively use pod disruption budgets (PDBs). This often underutilized Kubernetes feature helps ensure the high availability of applications during voluntary and involuntary disruptions.
but wait wait wait, what’s voluntary disruptions and involuntary disruptions ?
well first voluntary disruptions are actions initiated by the application owner and those initiated by a Cluster Administrator.
- Deleting the deployment or other controller that manages the pod
- Updating a deployment’s pod template causing a restart
- Directly deleting a pod (e.g. by accident)
- Draining a node for repair or upgrade.
- Draining a node from a cluster to scale the cluster down
- Removing a pod from a node to permit something else to fit on that node.
in the other hand involuntary disruptions means unavoidable cases like
- A hardware failure of the physical machine backing the node
- Cluster administrator deletes VM (instance) by mistake
- Cloud provider or hypervisor failure makes VM disappear
- A kernel panic
- The node disappears from the cluster due to cluster network partition
- Eviction of a pod due to the node being out-of-resources.
How To implement PDBs in K8s ?
first thing make sur you have already a deployement in the caluster, i will use this one from my side.
kubectl create deploy nginx - image=nginx - replicas=2 - dry-run=client -o yaml | tee nginx-pdbs.yml | kubectl apply -f -
This command will create nginx deployement and save the output to a file.
apiVersion: apps/v1
kind: Deployment
metadata:
creationTimestamp: null
labels:
app: nginx
name: nginx
spec:
replicas: 2
selector:
matchLabels:
app: nginx
strategy: {}
template:
metadata:
creationTimestamp: null
labels:
app: nginx
spec:
containers:
- image: nginx
name: nginx
resources: {}
status: {}
now let’s verify the results
➜ ~ kubectl get pods
NAME READY STATUS RESTARTS AGE
nginx-bf5d5cf98-9g6b8 1/1 Running 0 2m58s
nginx-bf5d5cf98-rh228 1/1 Running 0 2m26s
Next, we’ll create the Pod Disruption Budget (PDB) YAML file to ensure that at least one pod remains available even during disruptions.
apiVersion: policy/v1
kind: PodDisruptionBudget
metadata:
creationTimestamp: null
name: nginx-pdb
spec:
minAvailable: 1
selector:
matchLabels:
app: nginx
Here’s what each section means:
apiVersion: policy/v1
: Specifies the API version for PDBs.kind: PodDisruptionBudget
: Declares that this YAML is a PDB.metadata
: Includes the name of the PDB (nginx-pdb
).spec
:minAvailable: 1
: This ensures that at least one pod will always remain available.selector
: Defines which pods the PDB applies to, in this case, any pod with the labelapp: nginx
Step 3: Apply the PDB
To apply the Pod Disruption Budget to your Kubernetes cluster, run the following command:
kubectl apply -f nginx-pdbs.yml
What Happens During a Disruption?
Now that you have your PDB in place, let’s see how it works in practice:
- Voluntary Disruptions: For example, if a node is drained for maintenance, the Kubernetes scheduler will not allow more than one pod of the Nginx deployment to be disrupted at once, as the PDB specifies that at least one pod must remain available.
- Involuntary Disruptions: If a node fails unexpectedly, Kubernetes will ensure that it replaces the disrupted pod to meet the PDB requirements. If the pod cannot be replaced immediately (due to resource constraints), the disruption will be prevented until enough resources are available to launch a replacement pod.
Resources
If you found this article valuable and want to keep up with the latest tips and best practices in web security, DevOps, and performance optimization, consider subscribing to our newsletter!