Node Selectors in Kubernetes
In a newly created Kubernetes cluster, as per default setup a pod can be scheduled on any of the worker node in the cluster. But there are some circumstances, where we may need to control which node the pod deploys to.
For ex: Let’s say we have a different kinds of workloads running in our cluster and we would like to dedicate, the data processing workloads pods that require higher horsepower to the nodes with an SSD attached to it.
To achieve this goal, Kubernetes provides 2 methods:
- NodeSelector
- Node Affinity
Let’s Discuss NodeSelector:
nodeSelector is the simplest form of node selection. It is a field PodSpec and specifies a map of key-value pairs. For the Pod to be eligible to run on a node, the node must have the key-value pairs as labels attached to them.
To work with nodeSelector, we first need to attach a label to the node with below command:
# kubectl label nodes <node-name> <label-key>=<label-value>
# kubectl label nodes node-01 disktype=ssd# kubectl get nodes node-01 --show-labels (to verify the attached labels)
In 2nd step we need to add a nodeSelector term to the pod configuration:
apiVersion: v1
kind: Pod
metadata:
name: nginx
labels:
env: test
spec:
containers:
- name: nginx
image: nginx
nodeSelector:
disktype: ssd
Once the nodeSelector term is added in the Pod configuration file, we can run the below command to create the pod:
# kubectl apply -f nginx-pod.yaml
Once the Pod is created, the scheduler identifies the right node to place the pod as per the nodeSelector term in the Pod configuration file.
That’s it for nodeSelector, Refer : Node Affinity to schedule the pods with more specific configuration.
Thanks for reading!!!
Refer following articles for more insights on Kubernetes:-
How kubectl apply command works?
Kubernetes Services for Absolute Beginners — NodePort
Kubernetes Services for Absolute Beginners — ClusterIP
Kubernetes Services for Absolute Beginners — LoadBalancer