Node Selectors
In this tutorial we will discuss about Node Selectors in Kubernetes. Node Selector is one of the forms of node selection constraint. Hope I kept it simple as possible to understand.
This is a simple POD scheduling feature that allows scheduling a POD onto a node whose labels match the Node Selector labels specified by the user. Let us start with a simple example.
We have a 3 node cluster of which two are smaller nodes with lower hardware resources and one of them is a larger node configured with higher resources. We have different kinds of workloads running in our cluster.
You would like to dedicate the data processing workloads that require higher horsepower to the larger node as that is the only node that will not run out of resources in case the job demand extra resources.
However, in the current default setup, any PODs can go to any nodes.
So POD C in this case may very well end up on nodes 2 or 3 which is not desired. To solve this we can set a limitation on the PODs. So that they only run on particular nodes. There are 2 ways to do this.
The first is using Node Selectors which is the simple and easier method. For this we look at the POD definition file we created in earlier tutorials. This file has a simple definition to create a POD with a data processing image.
To run on the larger nodes, we add a new property called nodeSelector to the spec section and specify the size as large.
apiVersion: v1 kind: Pod metadata: name: myapp-pod spec: containers: - name: data-processor image: data-processor nodeSelector: size: Large
In the about configuration file, where did the size Large come from and how does Kubernetes know which is the large node?
The key value pair of size and Large are in fact labels assigned to the nodes the scheduler uses these labels to match and identify the right node to place the PODs on.
To use labels in a nodeSelector like this, we must have first labelled our nodes prior to creating this POD. So let us see how we can label the nodes.
Syntax
kubectl label nodes <node-name> <label-key>:<label-value>
kubectl label nodes node-1 size=Large
Now we have labeled the node. Now when the POD is created it is placed on Node 1 as desired.
Node Selectors served our purpose but it has limitations. We used a single label and selector to achieve our goal here. But what if our requirement is much more complex.
For example, we would like to say something like place the POD on a large or medium node or something like place the POD on any nodes that are not small. You cannot achieve this using Node Selectors.
For this node affinity and anti affinity features were introduced and we will look at that next tutorial.