Kubernetes

How to Set the Kubernetes Tolerations

Today, we learn about toleration in Kubernetes with the help of simple and basic examples. This article is designed to learn the basic concept of toleration in Kubernetes and how it can be implemented in Kubernetes to schedule the pods. Since toleration and taints work together, we will have a brief discussion about taints as well to understand the whole concept of taint and toleration in Kubernetes. Let us begin with the basic definitions of taint and toleration.

What Are Tolerations and Taints in Kubernetes?

Toleration and taint in Kubernetes are used to ensure that the pods are placed in the correct node. Toleration is defined in the pod specification while the taints are defined in the node specification. When you apply the toleration on a pod, it allows the scheduler to schedule the pods on a specific node. However, the taints work opposite to the toleration. It allows the node to reject the pods to be scheduled on it. The pods are only allowed to be scheduled on the node if they have tolerations applied to them with matching taints.

Kubernetes works with pods, clusters, nodes, events, etc. In some cases, to manage these things, Kubernetes needs tolerations and taints. Toleration is the implementation of the scheduling process. The pods need to be scheduled so that they can work properly and they can have enough resources when needed to perform their operation. Tolerations are applied to the pods against taint so that they do not have any interruption or disruption while working.

The taints in Kubernetes enable a pod to reject the scheduling of a pod. It is applied to a node using the “NodeSpec” Node specification. The scheduler is unable to put a pod on a node that has a taint on it. However, if you need to schedule the pods on a node where a taint is already applied, you need to declare the toleration against it.

Toleration in Kubernetes allows a pod to be scheduled on a node where a taint is already applied. The toleration on a pod is applied using the “PodSpec” Pod specification. When you apply the toleration on a pod with matching taint, the schedular can easily schedule the pods on a specific node.

Now, let us present a scenario to help you understand how you can implement the toleration on a pod in Kubernetes. Before you proceed to the implementation section, make sure you have all the prerequisite requirements.

Prerequisite:

The following are the things that you need to implement the toleration on a node in Kubernetes:

  • Ubuntu 20.04 or any other latest version of any Linux system
  • Minikube (latest version)
  • Installed virtual machine in your Linux/Unix system
  • Kubectl command line tool

Assuming that your system meets all the needs of prerequisites, let us begin to set the Kubernetes toleration.

Step 1: Start the Minikube Terminal

The first thing that you need to do is to start the minikube terminal so that you can use the kubectl commands for the implementation of Kubernetes toleration on a node. To start the minikube, the following command is used:

> minikube start

Upon the execution of this command, you get the following output in your terminal:

Step 2: Get the List of Active Nodes

Now that we started the minikube, our system is ready to set the toleration on pods in Kubernetes. Before we set the toleration on the pods, let us check how many nodes and what kind of nodes we already have. To do that, we use the following subsequent command:

> kubectl get nodes -o=custom-columns=NodeName:.metadata.name,TaintKey:.spec.taints[*].key,TaintValue:.spec.taints[*].value,TaintEffect:.spec.taints[*].effect

This instruction lists all the nodes which are tainted by the Kubernetes default installation. Let us first see the output of this command. Then, we discuss the list of nodes:

Since there are no nodes that are created and tainted by the Kubernetes default installation and we also did not specifically create any node, the result is <none>. From the previous output, we can see that there is no node. Hence, we first create the node and then set the toleration. To set the toleration on a pod in Kubernetes, we first need to deploy an app on a cluster. The next few steps demonstrate how to deploy an app on a cluster.

Step 3: Create a Namespace

First, we create a namespace to deploy an app on a cluster. Here, we create an app with the “frontend” value with the help of the following command:

> kubectl create ns frontend

This command creates the namespace having the “frontend” value. See the following output:

Step 4: Deploy the Nginx Pod on the Namespace

Now, deploy the nginx pod on the namespace that we just created. We use the following command to deploy the nginx:

> kubectl run nginx –image=nginx –namespace frontend

This deploys an app on a cluster with no toleration configuration in the specification of the app deployment. Using the kubectl command, we deploy the nginx pod on the namespace frontend:

Text Description automatically generated

Step 5: Get the List of Pods

Now, let us check the created pods to see their statuses. The given command lists down all the pods and their statuses as well:

> kubectl get pods -n frontend

Since we only created the nginx, this command should list down that pod with its status. See the following output:

Step 6: Analyze the Kubernetes Events

Now, let us analyze the events in Kubernetes so that we can set the toleration on the pods accordingly. We use the following kubectl command to get the list of events in Kubernetes:

> kubectl get events -n frontend

This lists down all the events which are associated with the front-end value along with their properties like type, reason, object, and message. See the given list in the following output:

Text Description automatically generated

As you can see from the previous output, the nginx pod is scheduled with the specific toleration. The “message” property shows the list of actions that are performed along the process.

Step 7: Check the Status of Pods

The final step is to check again the status of the pod which we created previously to ensure that it is successfully scheduled on a specific and correct node. To do that, we use the following kubectl command:

> kubectl get pods -n frontend

As can be seen in the previous output, the pod is now allowed to run on the tainted node since the toleration is set against it.

Conclusion

In this guide, we explored about the taints and toleration. We learned about the basic working of taints and toleration. Then, we implemented the toleration on a pod. With the help of a simple example, we learned how to set the toleration on a node in Kubernetes.

About the author

Kalsoom Bibi

Hello, I am a freelance writer and usually write for Linux and other technology related content