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.
As a Kubernetes API object, pod disruption budget sets the minimum number of pods an application needs to function smoothly during disruptions. These disruptions might be voluntary, such as maintenance operations or node scaling, or involuntary, such as hardware failures or system crashes.
Regardless of the source, disruptions can significantly affect your application’s performance and availability if not well-managed. That’s where pod disruption budgets come in.
When defining a PDB, you establish a safety net for your applications, minimizing downtime and ensuring continuity of services. This post will explore how to create and use pod disruption budgets and offer best practices and examples of their application.
Introducing pod disruption budgets
Creating a pod disruption budget requires careful consideration of your application’s availability needs. First and foremost, it’s essential to understand the minimum number of instances to remain functional during disruptions to maintain the desired level of availability.
Selectors play a crucial role in determining the scope of the pod disruption budget. When you define appropriate selectors, you can specify the pods to which the PDB applies. This ensures that the budget only protects the relevant pods.
Three steps to creating a PDB:
1. Determine the minimum number of instances.
Assess your application’s needs and identify the minimum number of instances (minAvailable) that must be operational during disruptions.
2. Create a YAML file.
Use a text editor to create a YAML file that specifies the PDB object. Here’s an example YAML file:
apiVersion: policy/v1beta1 kind: PodDisruptionBudget metadata: name: my-pdb spec: minAvailable: 2 selector: matchLabels: app: my-app
In this example, the minAvailable field is set to 2, indicating that at least two instances of the specified pods should be available at any given time. The selector field identifies the target pods based on the label selector app: my-app.
3. Apply the YAML file.
Use the kubectl apply command to apply the YAML file and create the PDB within your Kubernetes cluster:
kubectl apply -f pdb.yaml
Replace pdb.yaml with the name of your YAML file.
Following these steps, you can create a pod disruption budget tailored to your application’s availability needs. It ensures that the required number of instances remains operational during disruptions, helping maintain the stability and continuity of your Kubernetes workload.
How to use pod disruption budgets
The effective use of a pod disruption budget involves knowing how to check its status, simulate disruptions, and make necessary updates or deletions.
Let’s explore these steps now:
1. Check the PDB status.
Use kubectl commands to check the status and details of your PDB. The following command displays the PDB information:
kubectl describe pdb my-pdb
This provides insights into the current state of the PDB and its associated pods.
2. Simulate disruptions.
It’s crucial to test your pod disruption budget to check if it functions as expected during disruptions. You can simulate disruptions by draining a node or deleting a pod.
For example, to drain a node and observe the impact on the PDB, use the following command:
kubectl drain <node-name> --ignore-daemonsets
This tests if the PDB safeguards the desired number of available pods during node maintenance.
3. Update or delete the PDB.
If modifications are necessary, you can update the pod disruption budget to adjust the minimum available instances or other parameters.
- Use the kubectl apply command with the updated YAML file to make changes:
kubectl apply -f updated-pdb.yaml
- To delete a PDB, run the following command:
kubectl delete pdb my-pdb
By removing the PDB, you can release the constraints and allow normal scaling operations.
Handling PDBs gets even easier with Kubernetes automation and cost management platforms like CAST AI, which provide user-friendly features streamlining your deployment’s operations.
Best practices for using pod disruption budgets
Effectively using PDBs is crucial for maintaining high availability and minimizing downtime in Kubernetes deployments. These best practices will help you maximize their benefits:
1. Understand your applications.
Gain a clear understanding of your applications’ availability requirements. Determine the number of pod replicas that can be lost without impacting functionality.
2. Use selectors wisely.
Ensure the selectors in your pod disruption budget accurately reflect the pods you want to protect. This ensures the PDB applies to the intended pods.
3. Opt for percentage-based disruption budgets.
Instead of using absolute numbers, consider using percentage-based values for minAvailable or maxUnavailable. This provides flexibility as your deployments scale.
4. Use PDBs with higher-level concepts.
Combine pod disruption budgets with Deployments, ReplicaSets, and StatefulSets. These higher-level concepts manage the desired state of your pods and work in harmony with PDBs to maintain high availability.
5. Account for involuntary disruptions.
Remember that PDBs only limit voluntary disruptions. Ensure that your cluster has enough capacity to handle involuntary disruptions, such as hardware failures, without violating the PDB constraints.
6. Monitor PDBs
Regularly check the status and details of your PDBs and related pods. Utilize Kubernetes commands or a dashboard to gain insights into the impact of disruptions and verify that PDBs are appropriately set.
7. Review and revise.
You should periodically review and revise PDBs based on changes to applications, the Kubernetes cluster, and other organizational requirements. Stay proactive in adjusting them to align with your evolving needs.
A few useful code snippets to illustrate PDB usage:
- To describe a PDB:
kubectl describe pdb my-pdb
- To drain a node and observe PDB behavior during maintenance:
kubectl drain <node-name> --ignore-daemonsets
Real-world examples of pod disruption budgets
Using a pod disruption budget can be beneficial in multiple real-world scenarios. They are particularly handy when you seek the balance between the application’s high availability and the need to carry out routine admin tasks potentially causing disruptions.
This quality makes pod disruption budgets useful across all industries By ensuring continuous availability during peak shopping periods, they help e-commerce shops build the right consumer experience. Other sectors like financial services, telecommunications, SaaS and healthcare rely on PDBs to maintain stability during maintenance operations.
Here are some more real-world examples of PDBs in action:
1. E-commerce platforms
High-traffic e-commerce websites often run on microservices architectures deployed on Kubernetes. During peak shopping periods like Black Friday or after-Christmas sales, it’s crucial to maintain high availability while also updating or scaling services to handle the load. In such cases, PDBs help ensure that a minimum number of pods remain available during updates, minimizing potential impacts on the customer experience.
2. Financial services
In the financial sector, downtime can result in substantial losses. Companies running trading platforms, banking services, or other critical financial applications on Kubernetes can use PDBs to maintain service availability even during necessary maintenance or updates.
3. Telecommunications industry
Telcos often run applications requiring high availability like VoIP services or streaming platforms. Pod disruption budgets can help maintain service quality while allowing routine system maintenance or upgrades.
4. SaaS platforms
Software as a Service (SaaS) platforms often need to balance system maintenance with user experience. For instance, a cloud-based tool might use PDBs to ensure that a given part of its services is always available, even when rolling out new features or patches.
5. Healthcare industry
In healthcare, any service disruption could impact patient care. For example, a cloud-based Electronic Health Record (EHR) system deployed on Kubernetes could use PDBs to ensure constant availability, even during system upgrades.
In all these cases, using PDBs can help balance required system maintenance or updates and the need for constant availability and minimal disruption. This enhances the resilience of your Kubernetes applications and adds to your overall business continuity.
Using pod disruption budgets is a great method to ensure the availability and reliability of applications in Kubernetes clusters. By adding PDBs, you can control how many pods can be simultaneously disrupted to prevent potential downtime and ensure that critical services continue to function properly.
Pod disruption budgets get even more impactful with CAST AI, which also helps you optimize Kubernetes resource limits, ultimately boosting your cluster’s performance and cost-efficiency.
Curious how this could work for you? Schedule a technical demo and discover how much you can save on your Kubernetes deployment.
CAST AI clients save an average of 63%
on their Kubernetes bills
Book a call to see if you too can get low & predictable K8s bills.