Pod Disruption allows you to moderate a pod replica in your cluster without having access to the controller template. In this example, we have a Nginx deployment with 8 replica pods and we want to make sure that at least 7 are in production regardless of quantity of voluntary disruption requests. The next question is, what are involuntary and voluntary disruption request?
Involuntary Disruption are changes in our cluster like node failure, node out of resources, cloud provider disruption, VM shutdown that have effects on the number of pods we have in our cluster while voluntary disruption are requests that are made to our kubernetes api tthat affects pod distribution like node draining, pod eviction etc. This is our Deployment Template;
apiVersion: apps/v1 kind: Deployment metadata: name: nginx-redis-deployment labels: app: webserver spec: replicas: 8 selector: matchLabels: app: webserver template: metadata: labels: app: webserver spec: containers: - name: nginx image: nginx:1.9.1 - name: redis imagePullPolicy: IfNotPresent image: redis
As you can see from above, our deployment has 8 replica pods and we want to use PDB to make sure there is at least 7 running at any given time and any request that puts the number of pods below 7 gets denied.
apiVersion: policy/v1beta1 kind: PodDisruptionBudget metadata: name: pdb-demo spec: minAvailable: 7 selector: matchLabels: app: webserver
Let’s go ahead and run kubectl apply to create the deployment along with PodDisruptionBudget.
$ kubectl get deployment/nginx-redis-deployment NAME DESIRED CURRENT UP-TO-DATE AVAILABLE AGE nginx-redis-deployment 8 8 8 8 1h $ kubectl get pdb NAME MIN-AVAILABLE MAX-UNAVAILABLE ALLOWED-DISRUPTIONS AGE pdb-demo 7 N/A 1 7h
Now, lets go ahead and drain the minikube node. You will notice that drain was only able to evict one pod from the minikube node since further eviction will violate our PDB.
$ kubectl get deployment/nginx-redis-deployment NAME DESIRED CURRENT UP-TO-DATE AVAILABLE AGE nginx-redis-deployment 8 8 8 7 2h Friendwithagoat:kubernetes tunde.oladipupo$ kubectl get pods NAME READY STATUS RESTARTS AGE nginx-redis-deployment-6b95986b48-86gg5 2/2 Running 0 2h nginx-redis-deployment-6b95986b48-gkkdl 0/2 Pending 0 6m nginx-redis-deployment-6b95986b48-h8xhk 2/2 Running 0 2h nginx-redis-deployment-6b95986b48-hkntf 2/2 Running 0 2h nginx-redis-deployment-6b95986b48-jq6dz 2/2 Running 0 2h nginx-redis-deployment-6b95986b48-nk8h6 2/2 Running 0 2h nginx-redis-deployment-6b95986b48-qwlm5 2/2 Running 0 2h nginx-redis-deployment-6b95986b48-w9vkl 2/2 Running 0 2h<span id="mce_SELREST_start" style="overflow:hidden;line-height:0;">﻿</span>