In the last article we read about taints and toleration and that is just away to tell a node to allow pods to sit on it only if it has toleration for the taint.But it does not tell pod , not to go on any other node.Moving further here we will discuss about Node Selectors.
Now let us discuss a scenario where we have different types of workloads running on the cluster. One of the workload is really heavy and requires node with big configuration like it should have high amount of RAM and CPU and that workload (pod) can only run if it is placed on the bigger node.
We can achieve this by Node Selector , we can mention this property in the pod definition file.Let us understand this in a better way by taking an example.Assume that we want to run an nginx pod and we want that it should always run on node3 so we will do the below
Define a pod definition file like below
[root@node1 kubernetes]# cat node-selector.yaml apiVersion: v1 kind: Pod metadata: name: nginx-pod-node-selector labels: app: myapp spec: containers: - name: nginx-container image: nginx nodeSelector: size: large
Now next point to think about is that from where will pod come to know about the nodeSelector criteria i.e size: large , this is something that will come when we will put a label on our node. Here we want pod to come on node3 so we will label node3 like below.
[root@node1 kubernetes]# kubectl label nodes node3.example.com size=Large node/node3.example.com labeled [root@node1 kubernetes]# kubectl create -f node-selector.yaml [root@node1 kubernetes]# kubectl describe pod nginx-pod-node-selector | tail -5 Normal Scheduled 2m37s default-scheduler Successfully assigned default/nginx-pod-node-selector to node3.example.com Normal Pulling 2m36s kubelet, node3.example.com Pulling image "nginx" Normal Pulled 2m30s kubelet, node3.example.com Successfully pulled image "nginx" Normal Created 2m30s kubelet, node3.example.com Created container nginx-container Normal Started 2m30s kubelet, node3.example.com Started container nginx-container
So we see that how node selector works. Though it server a purpose but here we cannot specify multiple conditions like "Place the pods on the node which are NOT Large" or "Place the pods on the nodes which are Large OR Medium"
For this the concept of node affinity was introduced which we will talk about in next article.