In Kubernetes, Pods are scheduled across different nodes. Taints and tolerations help in the scheduling of these pods. We can use taints and tolerations to ensure which Pods are placed onto which nodes.
We add taints to the nodes, whereas the tolerations for the pods are defined in the specification of the Pods.
Taints and tolerations are especially useful when we have different nodes containing certain specifications, For example, limited resources and certain hardware configurations. We want to ensure that the correct pods are placed on the correct nodes.
Let's learn how we can use taints and tolerations using an example. Execute the following command by running the SPA widget provided below:
kubectl apply -f pod.yaml
Let's run the widget below:
apiVersion: v1 kind: Pod metadata: name: taints-tolerations-example spec: containers: - name: pod-example image: nginx
On running the widget, a single node kind
cluster will be created.
Note: The cluster will take some time to initialize, approximately 2 to 3 minutes.
The pod.yaml
file creates a single Pod. A detailed explanation of the Pod definitions is provided below:
Line 1: We define the version of Kubernetes API that we will use to create the Pod object, i.e., v1
.
Line 2: We define the object type to be created, i.e., a Pod
.
Line 4: We give a name to the object that will be created, i.e., taints-tolerations-example
.
Line 6: We specify the container that will be created in this Pod
.
Line 7: We give a name to the container that will be created in this Pod
, i.e., pod-example
.
Line 8: We specify the Docker image that will be used to create this container. We use the nginx
image for this example.
Run the following command to check the status of the Pod. The Pod should be in the "Running" state. If the Pod is showing a "Container Creating" status, wait for a few seconds for the Pod to initialize and then run to check the status again.
kubectl get pods
Now use the following command to delete the Pod and then again run the above-mentioned command to make sure that the Pod is deleted:
kubectl delete pod taints-tolerations-example
Let's list all the nodes by running the following command:
kubectl get nodes
Let's apply a taint to the node and then try to recreate the pod. Use the following commands to perform these operations:
kubectl taint node kind-control-plane key=value:NoSchedule
The kind-control-plane
command is the node that'll be tainted. We'll add a taint with key
and value
to the node and set the effect
to NoSchedule
. No new Pods will be scheduled on this node unless they have a matching toleration.
Now execute the following command to describe the node:
kubectl describe node
Scroll up the output, and you'll be able to see the taint we just applied to the node. Let's now try creating a Pod; you'll see that this pod is not scheduled on any node. Run the following command and wait for approximately 1 minute:
kubectl apply -f pod.yamlwatch kubectl get pods
Note: Press “Ctrl + C” to terminate the session.
You'll notice that even after a minute or two, the Pod is in a "Pending" state. It's because there's no node available where this Pod could be scheduled. The available node has a taint, and our Pod doesn't contain a matching toleration for this node. Let's now have a look at the taint-and-tolerations.yaml
file:
apiVersion: v1kind: Podmetadata:name: taints-tolerations-examplespec:containers:- name: pod-exampleimage: nginxtolerations:- key: "key"operator: "Equal"value: "value"effect: "NoSchedule"
This file is an extended version of pod.yaml
file. The only change this file has is that the pod is configured to have a toleration whose key is key
, value is value
, and it contains the NoSchedule
effect, the same as the taint that we applied to the node. Let's now configure the Pod to use these specifications, and let's see what happens:
kubectl apply -f taints-and-tolerations.yamlwatch kubectl get pods
You'll notice that the Pod is configured, and its state changes from "Pending" to "Container Creating" and then eventually "Running. "
Unlock your potential: Kubernetes Essentials series, all in one place!
To deepen your understanding of Kubernetes, explore our series of Answers below:
What is Kubernetes?
Get an introduction to Kubernetes, the powerful container orchestration platform that automates deployment, scaling, and management of containerized applications.
What is Kubernetes Event-Driven Autoscaling (KEDA)?
Learn how KEDA enables event-driven scaling, allowing Kubernetes workloads to automatically scale based on external metrics such as message queues, databases, and cloud events.
Why do we use Kubernetes?
Understand the core benefits of Kubernetes, including automated deployment, scaling, and management of containerized applications across distributed environments.
What are Kubernetes namespaces?
Discover how Kubernetes namespaces help organize and isolate workloads within a cluster, enhancing security and resource allocation.
What are the different types of services in Kubernetes?
Explore the various Kubernetes service types—ClusterIP, NodePort, LoadBalancer, and ExternalName—and their roles in facilitating communication between applications.
ReplicationController in Kubernetes
Learn about the ReplicationController, its role in maintaining pod availability, and how it ensures that a specified number of pod replicas are always running.
ExternalDNS in Kubernetes
Understand how ExternalDNS simplifies service discovery by dynamically managing DNS records for Kubernetes services, making external access seamless.
What are taints and tolerations in Kubernetes?
Gain insights into taints and tolerations and how they control pod scheduling by preventing or allowing specific workloads to run on designated nodes.
Introduction to Node Affinity in Kubernetes
Discover how Node Affinity works in Kubernetes to influence pod scheduling by specifying node selection preferences and ensuring efficient workload distribution.
Free Resources