feat: add section on pdb

This commit is contained in:
Rémy-Christophe Schermesser 2019-05-09 15:41:50 +02:00
parent 2b815b4007
commit d3be3d4957
3 changed files with 117 additions and 0 deletions

40
15-pdb/01-pdb.yml Normal file
View File

@ -0,0 +1,40 @@
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx-pdb
spec:
replicas: 3
selector:
matchLabels:
run: nginx-pdb
template:
metadata:
labels:
run: nginx-pdb
spec:
containers:
- name: nginx
image: nginx
affinity:
podAntiAffinity:
preferredDuringSchedulingIgnoredDuringExecution:
- weight: 1
podAffinityTerm:
labelSelector:
matchExpressions:
- key: run
operator: In
values:
- nginx-pdb
topologyKey: kubernetes.io/hostname
---
apiVersion: policy/v1beta1
kind: PodDisruptionBudget
metadata:
name: nginx-pdb
spec:
minAvailable: 2
selector:
matchLabels:
run: nginx-pdb

70
15-pdb/README.md Normal file
View File

@ -0,0 +1,70 @@
# Pod distruption budget (PDB)
## Introduction
In kubernetes pods are mortal and can be terminated at any time. When a pod is terminated it is called a [“disruption”](https://kubernetes.io/docs/concepts/workloads/pods/disruptions/).
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
## PDB
A `PDB` is configured as this:
```yaml
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` or `maxUnavailable`. It can either be a fixed number like `2` in the example, or a percentage like `20%`
If you want to see the effect of a PDB, you will need a multi-node k8s. As those lines are written `minikube` is a single node cluster. To have locally a multi-node cluster you can install [kind](https://github.com/kubernetes-sigs/kind).
Use the [configuration file](./kind.yml) provided to create your cluster:
```bash
kind create cluster --config kind.yml
```
Review and apply the manifests in [01-pdb.yml](./01-pdb.yml). Why did we specify a soft anti-affinity?
In a terminal run the command:
```bash
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:
```bash
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?
## Exercices
Nothing to see here.
## Clean up
```bash
kubectl delete service,deployment,pod --all
```
## Links

7
15-pdb/kind.yml Normal file
View File

@ -0,0 +1,7 @@
# three node (two workers) cluster config
kind: Cluster
apiVersion: kind.sigs.k8s.io/v1alpha3
nodes:
- role: control-plane
- role: worker
- role: worker