What is Taint?
The combination of pods, nodes, and clusters works together. Pods are attracted towards the nodes by the Node affinity property while taints restrict the pods to be scheduled on the nodes. Toleration and taints work together to make sure that no pod gets scheduled on the wrong node. These properties ensure that the pods are scheduled on the correct node and also manage the workload of a node by distributing an equal workload on all the nodes in the cluster. Taints are defined for the node using the node specification while pods use the toleration from the pod specification.
Taints are the restrictions that enable a node to repel the pods which are being scheduled on the node. On the other hand, toleration works against the taints and allows the pods to be scheduled on the node even if they have taints defined. There can be more than one taint defined for the pods to ensure that the pods do not get scheduled unless they can tolerate the defined taint. Here, we are demonstrating how to use the taints on Kubernetes pods with the help of a simple, short, and easy example.
Make sure you have the following tools installed on your system before continuing with this guide. With these basic tools, you will not be able to use taints on Kubernetes nodes.
- Ubuntu 20.02 or any other latest version
- Kubectl command line tool
- Kubernetes cluster
- Minikube cluster
Make sure these tools are properly installed and configured in your system so that you do not have a problem using taints on Kubernetes nodes. Assuming that you have your system ready with all these tools installed, we are moving to the next section.
How to Use Taints on Kubernetes Nodes?
Here, we will present a simple scenario to help you learn how to use a taint on a Kubernetes node. Let us start!
Step # 1: Start the Minikube Cluster
First, start the minikube cluster so that you can use the kubectl commands and run your application. The minikube cluster allows you to deploy your nodes, pods, and even cluster in the Kubernetes environment. Hence, it is essential to keep the minikube in active mode using the following command:
This will activate the minikube cluster and make the Kubernetes environment ready to use the taints on the node.
Step # 2: Get the List of Nodes
As we discussed above, the Kubernetes environment is based on nodes, pods, and clusters. The pods are scheduled on the nodes and we must define the taint for that node to prevent the pods from being scheduled on that node. So, here we are displaying the list of nodes to check which node already has a taint defined for it by the Kubernetes default installation. In addition, we will use the following command:
From the output above, we can notice that no pod is tainted by the default Kubernetes installation so we can taint this node.
Step # 3: Taint the Node
The node can be tainted by using the following command:
As you can see in the output, the node ‘minikube’ has been tainted with the ‘NoSchedule’ effect having the key name ‘app’ with a ‘frontend’ value. This has restricted the pods to be scheduled on the node. So, now, no pod can be placed on this node until a toleration has been defined against the taint.
Step # 4: Schedule the Pods on the Node
In this step, we will try to schedule the pod on the node which we have tainted in the previous step. We are deploying the pods on the cluster which does not have any toleration defined in the app deployment specification. See the process given below:
Step # 4a: Create a Namespace
First, use the given command here to create a namespace:
Step # 4b: Run nginx File on the Namespace
Now, run the nginx file on the namespace which we have just created by using the following command:
Step # 4c: Check Pods Status
Now, check the status of the pods to see if they are scheduled on the nodes or not. Since there is no toleration defined for the cluster, the pod should not be scheduled on any node in the cluster. Let us check the status of the pods with the command given below:
The status of the pod is ‘pending’ which means it has not been scheduled on any node. We can confirm whether it is scheduled on any node in the cluster or not by checking the events with the following command:
From the output, you can see that no node is available for the pods to be scheduled on because there is no toleration defined for the nodes.
We learned how to use the taints on the Kubernetes nodes. We explore what a taint is and what function it performs in a Kubernetes environment. We learned from this article that toleration and taints go hand in hand. Taints are defined for the nodes in the node specification while toleration is defined for the pods in the pod specification. Taints restrict the pods to be scheduled on the node while toleration works against the taint and allows the pods to get scheduled on nodes.