What is Kubernetes Pod disruption budget and Explained with example

A Kubernetes Pod disruption budget (PDB) is a policy that defines the minimum number of pods that must be available at any given time for a particular workload. This helps to ensure that the workload is always available, even during disruptions such as node failures or upgrades.

A Kubernetes Pod Disruption Budget (PDB) is a resource policy that allows you to control the disruption or eviction of Pods during actions like node maintenance, voluntary scaling down of nodes, or other events that may cause Pods to be rescheduled or terminated. PDBs are used to ensure the high availability and stability of your applications by specifying how many Pods of a particular application can be simultaneously disrupted.

Example Scnario

Imagine you have a critical application running in your Kubernetes cluster with multiple replicas to ensure redundancy. You want to ensure that, during maintenance or scaling activities, at least a certain number of Pods remain running to maintain the application’s availability. To achieve this, you create a Pod Disruption Budget for the application.

Which K8 resources can be used with PodDisruptionBudget

  • Deployment
  • ReplicationController
  • ReplicaSet
  • StatefulSet

If the scheduler needs to evict a pod that is covered by a PDB, it will first check to make sure that the minAvailable requirement is still met. If it is not, the scheduler will not evict the pod.

PDBs can be used to protect a wide variety of workloads, including:

  • Critical production applications
  • State machines
  • Databases
  • Queues

How to create PodDisruptionBudget?

PodDisruptionBudget has three fields:

  • A label selector .spec.selector to specify the set of pods to which it applies. This field is required.
  • .spec.minAvailable which is a description of the number of pods from that set that must still be available after the eviction, even in the absence of the evicted pod. minAvailable can be either an absolute number or a percentage.
  • .spec.maxUnavailable (available in Kubernetes 1.7 and higher) which is a description of the number of pods from that set that can be unavailable after the eviction. It can be either an absolute number or a percentage.

minAvailable


apiVersion: policy/v1
kind: PodDisruptionBudget
metadata:
  name: zk-pdb
spec:
  minAvailable: 2
  selector:
    matchLabels:
      app: zookeeper

maxUnavailable


apiVersion: policy/v1
kind: PodDisruptionBudget
metadata:
  name: zk-pdb
spec:
  maxUnavailable: 1
  selector:
    matchLabels:
      app: zookeeper

Example of PodDisruptionBudget


apiVersion: apps/v1
kind: Deployment
metadata:
  name: my-app
spec:
  replicas: 3
  template:
    metadata:
      labels:
        app: my-app
    spec:
      containers:
      - name: my-container
        image: my-app-image

apiVersion: policy/v1
kind: PodDisruptionBudget
metadata:
  name: my-app-pdb
spec:
  minAvailable: 2
  selector:
    matchLabels:
      app: my-app

How It Works:

Now, let’s say a node in your cluster needs maintenance, and Kubernetes needs to evict some Pods. The PDB you’ve created ensures that, even during the eviction process, there will always be at least two Pods of your critical application running.

  • If there are three Pods running on a node, the eviction will proceed because it maintains the minAvailable constraint (two Pods must remain).
  • However, if there are only two Pods running on a node, Kubernetes will block the eviction since it cannot maintain the minimum availability requirement.

List of all kubectl commands to work with poddisruptionbudgets


kubectl create poddisruptionbudget - Creates a new PDB.
kubectl get poddisruptionbudgets - Lists all PDBs in the current namespace.
kubectl describe poddisruptionbudget - Displays detailed information about a PDB.
kubectl update poddisruptionbudget - Updates an existing PDB.
kubectl delete poddisruptionbudget - Deletes a PDB.

Create a PodDisruptionBudget:
To create a new PodDisruptionBudget, you can use the kubectl create command with a YAML file containing the PDB definition:

$ kubectl create -f my-pdb.yaml
Replace my-pdb.yaml with the filename of your PDB definition YAML.

List PodDisruptionBudgets:
To list all the PodDisruptionBudgets in your cluster, you can use the kubectl get command with the pdb resource type:

$ kubectl get pdb

Describe a PodDisruptionBudget:
To view the details of a specific PodDisruptionBudget, you can use the kubectl describe command with the PDB's name:

$ kubectl describe pdb my-pdb

Replace my-pdb with the name of the PDB you want to describe.

Edit a PodDisruptionBudget:
To modify the configuration of an existing PodDisruptionBudget, you can use the kubectl edit command:

$ kubectl edit pdb my-pdb

This will open the PDB definition in your default text editor for you to make changes.

Delete a PodDisruptionBudget:
To delete a PodDisruptionBudget, you can use the kubectl delete command with the PDB's name:

$ kubectl delete pdb my-pdb

Replace my-pdb with the name of the PDB you want to delete.

Check PDB Status:
You can check the status of a PodDisruptionBudget to see how many disruptions are allowed and how many are currently allowed by running:

$ kubectl describe pdb my-pdb | grep Current
Replace my-pdb with the name of the PDB you want to check.
Rajesh Kumar
Follow me
Latest posts by Rajesh Kumar (see all)
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x