This guide explains how to manage taints and tolerations in Kubernetes for controlling pod scheduling.
In this guide, you will learn how to work with taints and tolerations in Kubernetes. We start by inspecting the cluster nodes, then proceed to apply a taint to a node and create pods with and without the appropriate tolerations.
Now, add a taint to node01 by specifying a key-value pair and an effect. The command below applies a taint with the key “spray”, a value of “mortein”, and an effect of “NoSchedule”:
Step 4: Create the “mosquito” Pod Without a Toleration
Create a pod named “mosquito” using the nginx image without specifying any toleration:
Copy
Ask AI
root@controlplane:~# kubectl run mosquito --image=nginx
After creating the pod, check its status:
Copy
Ask AI
root@controlplane:~# kubectl get podsNAME READY STATUS RESTARTS AGEmosquito 0/1 Pending 0 3m37sroot@controlplane:~#
Since the pod lacks a toleration for the taint applied on node01, it remains in a pending state. To investigate further, describe the pod:
Copy
Ask AI
root@controlplane:~# kubectl describe pod mosquitoName: mosquitoNamespace: defaultPriority: 0Node: <none>Labels: run=mosquitoStatus: PendingContainers: mosquito: Image: nginx Port: <none> Host Port: <none>...Tolerations: node.kubernetes.io/not-ready:NoExecute op=Exists for 300s node.kubernetes.io/unreachable:NoExecute op=Exists for 300sEvents: Warning FailedScheduling 45s (x2 over 45s) default-scheduler 0/2 nodes are available: 1 node(s) had taint {node-role.kubernetes.io/master: }, that the pod didn't tolerate, 1 node(s) had taint {spray: mortein:NoSchedule}, that the pod didn't tolerate....root@controlplane:~#
The event message clarifies that “mosquito” cannot be scheduled due to the untolerated “spray” taint on node01.
Ensure that you add the proper tolerations when you need a pod to be scheduled on a tainted node.
This walkthrough demonstrates the use of taints and tolerations to control pod placement in a Kubernetes cluster:
Initially, node01 had a taint (spray=mortein:NoSchedule) and the control plane had the default master taint, which prevented the “mosquito” pod from scheduling.
Creating the “bee” pod with the appropriate toleration allowed it to be scheduled on node01.
Removing the taint from the control plane enabled the “mosquito” pod to be scheduled there.
Using taints and tolerations effectively can help you control where pods are deployed and maintain a balanced and secure Kubernetes environment.