{"id":40497,"date":"2023-09-28T02:25:55","date_gmt":"2023-09-28T02:25:55","guid":{"rendered":"https:\/\/www.devopsschool.com\/blog\/?p=40497"},"modified":"2025-01-23T12:45:14","modified_gmt":"2025-01-23T12:45:14","slug":"what-is-kubernetes-pod-disruption-budget-and-explained-with-example","status":"publish","type":"post","link":"https:\/\/www.devopsschool.com\/blog\/what-is-kubernetes-pod-disruption-budget-and-explained-with-example\/","title":{"rendered":"What is Kubernetes Pod disruption budget and Explained with example"},"content":{"rendered":"\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"747\" height=\"276\" src=\"https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2023\/09\/image-719.png\" alt=\"\" class=\"wp-image-40498\" srcset=\"https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2023\/09\/image-719.png 747w, https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2023\/09\/image-719-300x111.png 300w\" sizes=\"auto, (max-width: 747px) 100vw, 747px\" \/><\/figure>\n\n\n\n<p><strong>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.<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"892\" height=\"601\" src=\"https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2023\/09\/image-720.png\" alt=\"\" class=\"wp-image-40499\" srcset=\"https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2023\/09\/image-720.png 892w, https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2023\/09\/image-720-300x202.png 300w, https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2023\/09\/image-720-768x517.png 768w\" sizes=\"auto, (max-width: 892px) 100vw, 892px\" \/><\/figure>\n\n\n\n<p>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.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Example Scnario<\/h2>\n\n\n\n<p>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&#8217;s availability. To achieve this, you create a Pod Disruption Budget for the application.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Which K8 resources can be used with PodDisruptionBudget<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Deployment<\/li>\n\n\n\n<li>ReplicationController<\/li>\n\n\n\n<li>ReplicaSet<\/li>\n\n\n\n<li>StatefulSet<\/li>\n<\/ul>\n\n\n\n<p>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.<\/p>\n\n\n\n<p>PDBs can be used to protect a wide variety of workloads, including:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Critical production applications<\/li>\n\n\n\n<li>State machines<\/li>\n\n\n\n<li>Databases<\/li>\n\n\n\n<li>Queues<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">How to create PodDisruptionBudget?<\/h2>\n\n\n\n<p>A&nbsp;<code>PodDisruptionBudget<\/code>&nbsp;has three fields:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>A label selector&nbsp;<code>.spec.selector<\/code>&nbsp;to specify the set of pods to which it applies. This field is required.<\/li>\n\n\n\n<li><code>.spec.minAvailable<\/code>&nbsp;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.&nbsp;<code>minAvailable<\/code>&nbsp;can be either an absolute number or a percentage.<\/li>\n\n\n\n<li><code>.spec.maxUnavailable<\/code>\u00a0(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.<\/li>\n<\/ul>\n\n\n\n<p><strong>minAvailable<\/strong><\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n<pre class=\"wp-block-code\"><span><code class=\"hljs\">apiVersion: policy\/v1\r\nkind: PodDisruptionBudget\r\nmetadata:\r\n  name: zk-pdb\r\nspec:\r\n  minAvailable: 2\r\n  selector:\r\n    matchLabels:\r\n      app: zookeeper\r\n<\/code><\/span><\/pre>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p><strong>maxUnavailable<\/strong><\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n<pre class=\"wp-block-code\"><span><code class=\"hljs\">apiVersion: policy\/v1\r\nkind: PodDisruptionBudget\r\nmetadata:\r\n  name: zk-pdb\r\nspec:\r\n  maxUnavailable: 1\r\n  selector:\r\n    matchLabels:\r\n      app: zookeeper\r\n<\/code><\/span><\/pre>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">Example of PodDisruptionBudget<\/h2>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n<pre class=\"wp-block-code\"><span><code class=\"hljs\">apiVersion: apps\/v1\r\nkind: Deployment\r\nmetadata:\r\n  name: my-app\r\nspec:\r\n  replicas: 3\r\n  template:\r\n    metadata:\r\n      labels:\r\n        app: my-app\r\n    spec:\r\n      containers:\r\n      - name: my-container\r\n        image: my-app-image\r\n<\/code><\/span><\/pre>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n<pre class=\"wp-block-code\"><span><code class=\"hljs\">apiVersion: policy\/v1\r\nkind: PodDisruptionBudget\r\nmetadata:\r\n  name: my-app-pdb\r\nspec:\r\n  minAvailable: 2\r\n  selector:\r\n    matchLabels:\r\n      app: my-app\r\n<\/code><\/span><\/pre>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p><strong>How It Works:<\/strong><\/p>\n\n\n\n<p>Now, let&#8217;s say a node in your cluster needs maintenance, and Kubernetes needs to evict some Pods. The PDB you&#8217;ve created ensures that, even during the eviction process, there will always be at least two Pods of your critical application running.<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>If there are three Pods running on a node, the eviction will proceed because it maintains the <code>minAvailable<\/code> constraint (two Pods must remain).<\/li>\n\n\n\n<li>However, if there are only two Pods running on a node, Kubernetes will block the eviction since it cannot maintain the minimum availability requirement.<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">List of all kubectl commands to work with poddisruptionbudgets<\/h2>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-1\" data-shcb-language-name=\"JavaScript\" data-shcb-language-slug=\"javascript\"><span><code class=\"hljs language-javascript\">kubectl create poddisruptionbudget - Creates a <span class=\"hljs-keyword\">new<\/span> PDB.\r\nkubectl <span class=\"hljs-keyword\">get<\/span> poddisruptionbudgets - Lists all PDBs in the current namespace.\r\nkubectl describe poddisruptionbudget - Displays detailed information about a PDB.\r\nkubectl update poddisruptionbudget - Updates an existing PDB.\r\nkubectl delete poddisruptionbudget - Deletes a PDB.<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-1\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">JavaScript<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">javascript<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-2\" data-shcb-language-name=\"JavaScript\" data-shcb-language-slug=\"javascript\"><span><code class=\"hljs language-javascript\">Create a PodDisruptionBudget:\nTo create a <span class=\"hljs-keyword\">new<\/span> PodDisruptionBudget, you can use the kubectl create command <span class=\"hljs-keyword\">with<\/span> a YAML file containing the PDB definition:\n\n$ kubectl create -f my-pdb.yaml\nReplace my-pdb.yaml <span class=\"hljs-keyword\">with<\/span> the filename <span class=\"hljs-keyword\">of<\/span> your PDB definition YAML.\n\nList PodDisruptionBudgets:\nTo list all the PodDisruptionBudgets <span class=\"hljs-keyword\">in<\/span> your cluster, you can use the kubectl <span class=\"hljs-keyword\">get<\/span> command with the pdb resource type:\n\n$ kubectl <span class=\"hljs-keyword\">get<\/span> pdb\n\nDescribe a PodDisruptionBudget:\nTo view the details of a specific PodDisruptionBudget, you can use the kubectl describe command with the PDB's name:\n\n$ kubectl describe pdb my-pdb\n\nReplace my-pdb with the name of the PDB you want to describe.\n\nEdit a PodDisruptionBudget:\nTo modify the configuration of an existing PodDisruptionBudget, you can use the kubectl edit command:\n\n$ kubectl edit pdb my-pdb\n\nThis will open the PDB definition in your default text editor for you to make changes.\n\nDelete a PodDisruptionBudget:\nTo delete a PodDisruptionBudget, you can use the kubectl delete command with the PDB's name:\n\n$ kubectl delete pdb my-pdb\n\nReplace my-pdb with the name of the PDB you want to delete.\n\nCheck PDB Status:\nYou can check the status of a PodDisruptionBudget to see how many disruptions are allowed and how many are currently allowed by running:\n\n$ kubectl describe pdb my-pdb | grep Current\nReplace my-pdb with the name of the PDB you want to check.<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-2\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">JavaScript<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">javascript<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>","protected":false},"excerpt":{"rendered":"<p>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&#8230; <\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_joinchat":[],"footnotes":""},"categories":[4859],"tags":[],"class_list":["post-40497","post","type-post","status-publish","format-standard","hentry","category-kubernetes"],"_links":{"self":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts\/40497","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/comments?post=40497"}],"version-history":[{"count":1,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts\/40497\/revisions"}],"predecessor-version":[{"id":40500,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts\/40497\/revisions\/40500"}],"wp:attachment":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/media?parent=40497"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/categories?post=40497"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/tags?post=40497"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}