Hello, readers! In this article, we will be focusing on Kubernetes nodeSelector Label, in detail.
So, let us begin!! 🙂
Table of Contents
Assigning Kubernetes Pods to Nodes
Kubernetes provides us with various resources to have the application up and running plus management by the Kubernetes engine. A Kubernetes pod is the smallest deployable unit of Kubernetes which holds containers with the underlying app configurations.
Usually, the kube-scheduler automatically does all the reasonable placement of Pods onto the nodes. But, in certain scenarios, we may feel the need to assign and bind the pods to certain specific nodes. For example, we would want the pods to bind on a Node with SSD attached to it.
In the above situation, Kubernetes provides us with the concepts of Affinity and Anti-affinity, nodeSelector to assign/prevent a pod to bind to a Node.
In order to bind a pod to a specific node, the below three techniques can be used–
- The nodeSelector label
- Node affinity
- Pod affinity and anti-affinity
Let us now have a look at each one of them in the upcoming section.
The nodeSelector label
Kubernetes nodeSelector label is the simplest form of technique to assign a pod to a specific node. In this technique, we first label a node with a specific key-value pair. Further, we include the nodeSelector in the Pod Specification and include the labels that are part of the Node. By this, the Pod finds and matches the labels on the node and is bind to it.
Applying a label on the node:
kubectl label nodes gcp-2313-dev-01 disk=persistent-SSD
Having labelled the Node, let us now include the label details of the Node in the Pod specification-
apiVersion: v1 kind: Pod metadata: name: nginx-pod labels: app: demo spec: containers: - name: nginx-container image: nginx nodeSelector: disk: persistent-SSD
In the above Pod YAML schema, we have included nodeSelector along with the label applied to the Node under the spec section. By this, the Pod is bind only on the Node whose label matches the label under the nodeSelector section within the Pod Specification.
Limitations of Kubernetes nodeSelector label
- With nodeSelector label, the language is not much expressive. Rather is it confined to certain syntax. That is, we cannot apply multiple key-value labels and cannot have a condition wherein if either of the labels is present still the pod would be assigned to the Node.
- The nodeSelector label is a hard-code rule, that is, if the Pod fails to satisfy or carry the label, it will not get scheduled to the mentioned nodes and may prohibit an undefined behavior.
- With nodeSelector label, we can just define labels against nodes. No labels can be defined against Pods to choose which Pods can and cannot be scheduled on a node.
To overcome the above limitations, Kubernetes did introduce us with Node and Pod Affinity & Anti-Affinity. We will be covering this concept in a separate article. So, stay tuned with us for further updates.
By this, we have come to the end of this topic. Feel free to comment below, in case you come across any question.
For more such posts related to Docker and Kubernetes, Stay tuned with us.
Till then, Happy Learning!! 🙂