In Kubernetes pods are mortal and can be terminated at any time. When a pod is terminated it is called a “disruption”.
Disruptions can either be voluntary or involuntary. Involuntary means that it was not something anyone could expect (hardware failure for example). Voluntary means it was initiated by someone or something, like the upgrade of a node, a new deployment, etc.
Defining a “Pod Disruption Budget” helps Kubernetes manage your pods when a voluntary disruption happens. Kubernetes will try to ensure that not too many pods, matching a given selector, are unavailable at the same time
A PDB
is configured as this:
apiVersion: policy/v1beta1
kind: PodDisruptionBudget
metadata:
name: nginx-pdb
spec:
minAvailable: 2 # or maxUnavailable
selector:
matchLabels:
app: nginx
A PDB is composed of two configurations:
- the
selector
to know on which pods to apply this PDB - a number either
minAvailable
ormaxUnavailable
. It can either be a fixed number like2
in the example, or a percentage like20%
If you want to see the effect of a PDB, you will need a multi-node Kubernetes. As those lines are written minikube
is a single node cluster. To have locally a multi-node cluster you can install kind.
Use the configuration file provided to create your cluster:
kind create cluster --config 14-pdb/kind.yml
Review and apply the manifests in 01-pdb.yml. Why did we specify a soft anti-affinity?
In a terminal run the command:
kubectl get pods -owide -w
It will display all the pods with the node where it's deployed. The -w
is to watch the changes of those resources.
In another termimal run the command:
kubectl drain kind-worker2 --ignore-daemonsets
This command will remove, drain, the node kind-worker2
from the cluster. Watch the output of this command and the changes in the kubectl get pods -owide -w
.
What do you see? How can you explain this?
kubectl delete service,deployment,pod --all