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.
- 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
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
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
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
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
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
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"
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
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
kubectl apply -f my-deployment-with-node-selector.yaml #Apply changes to the deployment
Here, you can see that all pods are created and used only on the “master” node.
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.