If you are running with a multi-mode cluster and when you want to place your pods on specific nodes then node affinity is the best option. to achieve the node affinity you have to mention in the manifest specification.
There are two types of affinity: hard and soft.
- requiredDuringSchedulingIgnoredDuringExecution ( Hard)
- preferredDuringSchedulingIgnoredDuringExecution (soft)
when we use required that means hard affinity and preferred means soft affinity. In simple way when you want to place pod on specific node then you can use the required option and when you have two nodes then you can mention the weight to place the pods.
Request: you need place a nginx pod to k8s-n2 with 2 replicas.
Hard Affinity -
I have 3 node cluster as follows;
my manifest file as follows.
In my manifest file if you see closely the match expression has set to key and value pairs.
the key is "servers" and value is "qrst-app", this means if the label will match then the pod will deploy on the node.
The label is already set to my node2 for "qrst-app" application snip.
$ k8s-ms$ kubectl get no k8s-n2 --show-labels
NAME STATUS ROLES AGE VERSION LABELS
k8s-n2 Ready <none> 11d v1.18.3 beta.kubernetes.io/arch=amd64, servers=qrst-app
Now lets create the pod:
kubectl create -f hardaffinitypods.yaml
pod/ha-pods created
query the details of the newly created pod.
We will see the soft node affinity in the next section: How to achieve Node Affinity (Soft) in K8S 2/2
Comments
Post a Comment