Linux

How to use Node Selectors in Kubernetes

How to use Node Selectors in Kubernetes

We can restrict Pods to only run on certain Nodes. There might be some situations where we might want more control at the node where the pod is landing.

node selector is one form of node selection constraints. nodeSelector is the PodSpec field. This is a simple Pod scheduling feature that allows scheduling a Pod to a node whose label matches the Selector node label specified by the user.

To find out more about Node Selects, click here to open the official Kubernetes page.

In this article, we won’t have stains on the master node so pods can be used on the master node as well. Then we will create a pod to see they can be used on workers as well as the master node. We will then attach labels to the master node and pod points to be placed on the master node using only the Selector node.

Precondition

  • Kubernetes Cluster with at least 1 worker node.
    If you want to learn how to make the Kubernetes Cluster, click here. This guide will help you create a Kubernetes cluster with 1 Master and 2 Nodes on AWS Instance Ubuntu 18.04 EC2.

What we will do

  • Configure Node-Selectors

Configure Node-Selectors

First, let’s extract the node details in the cluster using the following command.

kubectl get nodes #Get nodes available in the cluster
kubectl describe nodes node01 | grep Taint #Describe node1 node to extract details regarding Taints
kubectl describe nodes master | grep Taint #Describe master node to extract details regarding Taints

1

The above shows that the node has no stain, this means Pods can be placed on one node, either on master or node01.

Now, let’s create a deployment that doesn’t have a node selector in it.

vim my-deployment-without-node-selector.yaml #Create a deployment definition
apiVersion: apps/v1
kind: Deployment
metadata:
  labels:
    app: frontend
  name: frontend
spec:
  replicas: 3
  selector:
    matchLabels:
      app: frontend
  template:
    metadata:
      labels:
        app: frontend
    spec:
      containers:
      - image: httpd:2.4-alpine
        name: frontend

2

Now, we are ready to create a deployment using the following command.

kubectl get pods #Get pods available in the cluster
kubectl create -f my-deployment-without-node-selector.yaml #Create a deployment
kubectl get pods ##Get nodes available in the cluster

3

Now, change the number of replicas in the spread by editing the file and applying changes.

kubectl apply -f my-deployment-without-node-selector.yaml #Apply changes made in the deployment definition file
kubectl get pods -o wide # Get more details regarding pods using -o wide option

4

In the screenshot above, it can be seen that a new pod is being created and is also scheduled on the master node.

The reason for this is, both node, master and node01, do not have stains.

SO, to limit scheduling and to make sure the pod gets a place in the master node, let’s create a label on the master node.

kubectl label nodes master on-master=true #Create a label on the master node
kubectl describe node master #Get more details regarding the master node

5

In the screenshot above, it can be seen that the master node has the label as “on-master = true”

Now, let’s create a new deployment with the Selector node: on-master = true in it to ensure that Pods are deployed only to the master node.

vim my-deployment-with-node-selector.yaml #Create a deployment definition file 
apiVersion: apps/v1
kind: Deployment
metadata:
  name: streamer-v4-deployment
  labels:
    app: streamer-v4
spec:
  replicas: 2
  selector:
    matchLabels:
      app: streamer-v4
  template:
    metadata:
      labels:
        app: streamer-v4
    spec:
      containers:
      - name: streamer-v4
        image: nginx
        ports:
        - containerPort: 8880
      nodeSelector:
        on-master: "true"

6

New placements with the Selector node can now be created with the following command.

kubectl create -f my-deployment-with-node-selector.yaml #Create a deployment
kubectl get pods -o wide | grep streamer-v4-deployment #Get more details of the pods

7

In the screenshot above, it can be seen that Pods are deployed only to the master node.

Now, let’s change “replica = 50” to see where the pod will be placed.

vim my-deployment-with-node-selector.yaml #Change a deployment definition

8
Apply the latest changes with the commands mentioned below.

kubectl apply -f my-deployment-with-node-selector.yaml #Apply changes to the deployment

9

Here, you can see that all pods are created and used only on the “master” node.

Conclusion

In this article, we see how pods can be restricted to be placed on certain nodes using only the label and node selector.

We also see that if the master node does not have Taints then the pod can be used.

Related posts

How to Add SWAP Space on Ubuntu 20.04

Linux

How to Install TensorFlow on CentOS

Linux

How to install Linux Kernel 5.7 on Ubuntu

Linux

How to Grep Way for Various Strings and Patterns

Linux

How To Remove Snap From Ubuntu

Linux

How to Install Node.js and npm on Ubuntu 20.04

Linux

How to Install Sublime Text 3 on Ubuntu 20.04

Linux

How to Change the Time Zone in Ubuntu 20.04

Linux

How to Activate Snap Package Manager on Linux Mint 20

Linux