The Wayback Machine - https://web.archive.org/web/20240907034843/https://www.geeksforgeeks.org/kubernetes-taint-and-toleration/
Open In App

Kubernetes – Taint and Toleration

Last Updated : 30 Mar, 2023
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

A pod is a group of one or more containers and is the smallest deployable unit in Kubernetes.  A node is a representation of a single machine in a cluster (we can simply view these machines as a set of CPU and RAM). A node can be a virtual machine, a physical machine in a data center hosted on a cloud provider like Azure. 

When a user runs the below-given pod creation command then the request is sent to the API server. The scheduler is always watching the API server for new events, it identified the unassigned pods and decides which node it should choose to deploy this pod-based on various factors like Node selector, Taints/Tolerations, Node Affinity, CPU and memory requirements, etc. Once Node is decided and sent to API server then kubelet make sure that pod is running on the assigned node.

kubectl (client ): kubectl create -f <pod-yaml-file-path> 

Need Of Taint and Toleration:

Nodes with different Hardware: If you have a node that has different hardware (example: GPU ) and you want to schedule only the pods on it which need GPU. Example: Consider there are 2 applications APP 1: A simple dashboard application and APP 2: A data-intensive application both has different CPU and memory requirements. APP1 does not require much memory and CPU whereas APP 2 needs high memory and  CPU (GPU machine ). Now with help of taints and tolerations + Node affinity, we can make sure that APP 2 is deployed on a node that has high CPU and memory, while APP1 can be scheduled on any Node with low CPU and Memory.

Limit the number of pods in a node: If you want a node to schedule a certain number of pods to reduce the load on that node then Taints/Tolerations + Node Affinity can help us achieve it. Example: Consider there is a pod that consists of a database application that needs to be fast in queries the data and highly available.  So, we will dedicate a node with high memory and CPU for this pod. Now the node will have only one pod in it, which makes it faster and more efficient to use node resources. 

  • Node affinity makes sure that pods are scheduled in particular nodes. 
  • Taints are the opposite of node affinity; they allow a node to repel a set of pods. 
  • Toleration is applied to pods and allows (but does not require) the pods to schedule onto nodes with matching taints.

Let’s understand this with an example:  Consider there is a Person N1 and Mosquito P1. Taint Example: Person N1 applied a repellent (taint) so now Mosquito P1 won’t be able to attack Person N1. Now Let’s suppose there is Wasp P2 which tries to attack Person N1

Toleration Example: Wasp P2 is tolerant to repellent, hence has no effect and a Person N1 would be attacked. Here 2 things decide whether a mosquito or wasp can land on a person: Taint (Repellent ) of the Mosquito P1  and Tolerance of the Wasp P2

In the Kubernetes world, Persons correspond to  Nodes, and the Mosquito, and Wasp correspond to  Pods. 

Example  

Case 1: Taint Node 1 (Blue)

Since Pods are not tolerated so none of them would be scheduled on node 1

Node 1 is a taint to blue

Case 2: We add tolerance to pod D. Now only Pod D will be able to schedule on Node 1

Pod D tolerant to taint  blue 

Taints and Tolerations

Taints are a property of nodes that push pods away if they are not tolerate to node taint.  Like Labels, multiple taints can be applied to a node. 

How can we enable certain pods to be scheduled on tainted nodes ? 

 By specifying which pods are tolerant to specific taint; we add tolerations to certain pods.

Tolerations are set to pods, and allow the pods to schedule onto nodes with matching taints. Taints and tolerations have nothing to do with security.

Syntax: 

kubectl taint nodes node-name key=value:taint-effect 

Taint-effect:

  • NoSchedule: Pods will not be scheduled on the node unless they are tolerant. Pods won’t be scheduled, but if it is already running, it won’t kill it. No more new pods are scheduled on this node if it doesn’t match all the taints of this node.
  • PreferNoSchedule: Scheduler will prefer not to schedule a pod on taint node but no guarantee. Means Scheduler will try not to place a Pod that does not tolerate the taint on the node, but it is not required.
  • NoExecute: As soon as, NoExecute taint is applied to a node all the existing pods will be evicted without matching the toleration from the node.

Example of NoExecute Taint effect:

Currently, none of the pods is tolerant to blue.

No taint on nodes

 

Pod D YAML file :

  • Line 1-2 : v1 of Kubernetes Pod API. By kind, Kubernetes knows which component to create.                                                                                    
  • Line 3-4: metadata provides info that does not influence how the pod behaves, it is used to define the name of the pod and few labels(which will be used later by controllers )                                                                                                                                                                                                       
  • Line 5:  spec we define containers here. The pod can have multiple containers                                                                                                                       
  • Line 6-8: container name here is redis-container, image Redis will be pull from the container registry.                                                                           
  • Line 7:  operator default value is Equal. A toleration “matches” a taint if the keys are the same and the effects are the same, and: operator = Exists (in which case no value should be specified). operator =  Equal and the values are equal. If keys are empty and the operator exists then it matches all the keys and values (i.e  will tolerate everything effect is the type of taint-effect, here we chose NoExecute effect 

Now taint node 1 to blue 

kubectl taint nodes node1 app=blue:NoExecute

Here, node1 –>  name of the node on which taint will be applied. The app=blue:NoExecute –> key-value pair : Type of taint effect. This means that all the existing pods that do not tolerate the taint will be evicted.

Pod C will be evicted from Node 1, as it is not tolerant to taint blue. 

Pod C evicted 

In the above example, Node D was scheduled on Node 1. What if it was scheduled on another node? Is it possible ?

Yes

Taints/Tolerations + Node Affinity = Assures that a specific pod can only schedule on a specific node only and No other pods can be scheduled in tainted nodes.Example o

Note: The master node does not have any pods in it. Because when cluster is created the Kubernetes taints its master node so no pods are scheduled on master node.

kubectl describe node kubemaster | grep Taints

Output:



Previous Article
Next Article

Similar Reads

Kubernetes - Monolithic Architecture of Kubernetes
There is a new way of developing software apps using a microservices architecture. That's when all the buzz around containers and container orchestration has increased but we have been developing and using these large software apps even before most of us were born. So in this article, we will be discussing what is that old software architecture we
7 min read
Why Kubernetes? Benefits of using Kubernetes
The popularity of container orchestration technologies specially Kubernetes comes from its use cases and the problems that it solves. Kubernetes is the most popular container orchestration and is widely used by Cloud Native Computing Foundation (CNCF), the foundation to which Kubernetes as a project was donated by Google, estimates that about 92% b
8 min read
Kubernetes Controller VS Kubernetes Operator
Kubernetes Controllers are ideal for managing stateless apps and maintaining the correct number of copies, but Kubernetes Operators are more appropriate for complicated, stateful applications that require human-like decision-making abilities. Kubernetes ControllerKubernetes Controllers are ideal for managing stateless apps and maintaining the corre
4 min read
How Raspberry Pi and Kubernetes Work Together?
Pre-requisite: Kubernetes and Raspberry Pi Kubernetes and Raspberry Pi are two popular technologies that can be used together to create powerful and scalable applications. In this article, we will look at what each technology is, how they work together, and the benefits and challenges of using the two technologies together. KubernetesKubernetes is
3 min read
Kubernetes Resource Model (KRM) and How to Make Use of YAML?
Here we will explain how YAML can simplify system management and automation of most processes so that Kubernetes is a convenient working system. Basic Kubernetes Models: KRM and Everything-as-CodeAccording to Kubernetes co-founder Brian Grant, Kubernetes is very convenient thanks to the Kubernetes Resource Model (KRM) resource model. This is a way
6 min read
How To Use Kubernetes Taints and Tolerations?
Kubernetes is an open-source Container Management tool that automates container deployment, container scaling, descaling, and container load balancing (also called a container orchestration tool). Suppose there is a scenario, you have a single cluster where you run all of your production, staging, and testing workloads. Our Productions nodes are of
5 min read
Benefits and Characteristics of Kubernetes Namespaces
In Kubernetes, Namespaces are used to organize resources. You can have multiple Namespaces in a Cluster And these Namespaces are kind of virtual Clusters of their own. The official definition of Namespace says "In Kubernetes, namespaces provide a mechanism for isolating groups of resources within a single cluster". Within a Kubernetes Namespace, re
7 min read
Kubernetes - Kubectl Create and Kubectl Apply
Kubernetes is an open-source Container Orchestrating software platform that is widely used in modern application deployment and management. It comes up with many internal resources to manage the other working nodes Organizing as a Cluster facilitates seamless automatic scaling, and deployment updates making applications highly available and fault t
8 min read
Difference Between Kubernetes And Elastic Beanstalk
Kubernetes is an open-source container orchestration technology that automates container deployment options have benefits deriving from its structure. Elastic Beanstalk is a Platform as a Service (PaaS) that simplifies application deployment and administration by delivering a fully managed environment.AWS Elastic Beanstalk and Google Kubernetes Eng
5 min read
Difference Between Docker Compose And Kubernetes
Docker Compose and Kubernetes are two prominent container orchestration solutions with distinct features and capabilities. It simplifies application stack management by allowing you to handle services, networks, and volumes in a single, understandable YAML configuration file. Kubernetes integrates nicely with other popular CI/CD technologies like J
5 min read
Difference Between Kubernetes Ingress And Loadbalancer
Kubernetes is an enterprise-grade container orchestration technology. In many non-container contexts, load balancing is simple—for example, balancing across servers. However, load balancing across containers necessitates specialized management.The most fundamental kind of load balancing in Kubernetes is called load distribution. Implementing load d
5 min read
Difference Between kubernetes Ingress And Egress
Kubernetes is an open-source container orchestration system designed to automate software deployment, scalability, and administration. Kubernetes, ingress and egress are two concepts that refer to how traffic enters and departs a cluster. Ingress in Kubernetes at its most basic. Ingress is the process of directing external traffic into the Kubernet
4 min read
Difference Between Kubernetes And Docker Swarm
Containers are brilliant at packaging and holding all application codes, dependencies, libraries, and necessary configurations in a way that you can run them anywhere easily. But the problem arises from the fact that containers themselves cannot do things like load balancing, provisioning hosts, distributing containers across multiple servers, scal
8 min read
Kubernetes Ingress Controllers: Routing And Load Balancing For Services
In the dynamic landscape of containerized applications, efficiently orchestrating and managing services is critical. As an effective platform for container orchestration, Kubernetes offers a stable foundation for launching, growing, and overseeing containerized applications. The Ingress controller is a crucial part of Kubernetes that helps with eff
6 min read
Fundamental Kubernetes Components and their role in Container Orchestration
Kubernetes or K8s is an open-sourced container orchestration technology that is used for automating the manual processes of deploying, managing and scaling applications by the help of containers. Kubernetes was originally developed by engineers at Google and In 2015, it was donated to CNCF (Cloud Native Computing Foundation) To understand Kubernete
12 min read
Difference Between Kubernetes and OpenShift
Containerization is the process of packaging software code along with all of its necessary dependencies, including libraries, frameworks, and other dependencies, into a single, isolated "container." The purpose of this is to enable consistent movement and operation of the program or application within the container in any environment and on any inf
12 min read
How To Use Kubernetes Liveness and Readiness Probes?
Kubernetes (also known as K8s) is an open-source Container Management tool. It helps us automate all the processes like deployment, load balancing, rolling updates, etc. We can deploy, scale, and manage containerized applications. Table of Content What are Kubernetes Liveness and Readiness Probes?What are the Three Types of Kubernetes Probes?How Do
15 min read
How to Opimize kubernetes Pod CPU And Memory Utilization?
Before we dive into boosting your Kubernetes pod performance, let's take a moment to understand the key players in this orchestration symphony. In the world of Kubernetes, pods are used to run containers, which are the smallest units of computing in Kubernetes. Table of Content Why to optimize Kubernetes Pod CPU and Memory Utilization?How to check
12 min read
Kubernetes Pods: How to Create and Manage Them
Kubernetes is an open-source container orchestration system mainly used for automated software deployment, management, and scaling. Kubernetes is also known as K8s. Kubernetes was originally developed by Google, but it is now being maintained by the Cloud Native Computing Foundation. It was originally designed to be interfaced with only the Docker
13 min read
How to Install and Run a Kubernetes Cluster on Ubuntu 22.04 (Step-by-Step)
You can install and configure Kubernetes in different ways on your personal laptops, physical servers, Virtual machines, and as a cloud service. Before moving ahead with this article, we need to have a basic understanding of Kubernetes and its architecture and containers. In this article, we will get a high-level overview of ways you can install Ku
6 min read
Kubernetes Interview Questions and Answers
Kubernetes is a powerful open-source platform for automating deployment, scaling, and operations of application containers across clusters of hosts. It provides a container-centric infrastructure that is efficient, making it a leading choice for managing complex applications. Derived from the Greek word meaning "captain," "helmsman," or "governor,"
15+ min read
How to Configure NCache to Run in a Kubernetes Cluster and Testing
In this article, let us discuss how to deploy and run NCache in a Kubernetes Cluster and how we can integrate this cache cluster with our application. We’ll follow it up with testing stress and then expose performance counters with SNMP. What is NCache?NCache is one of the most popular caching providers available in the market, along with other pla
8 min read
Using Commands and Arguments in a Kubernetes Pod
Kubernetes is open-source software that is used for the deployment and automation of software applications. It is a container orchestration platform. It means that we can manage microservices using containers. What is a Kubernetes pod?Kubernetes Pod is the smallest unit in the structure of Kubernetes. It basically consists of one or more containers
7 min read
Monitoring Kubernetes Clusters with Prometheus and Grafana
In modern era of containers, Kubernetes has emerged as a leading band. like any robust system, effective assessment is critical to ensuring it works, reliably and is scalable. Prometheus and Grafana, two powerful tools, combine to provide robust solution for managing a Kubernetes clusters. This article will guide you through setting up and using Pr
8 min read
Managing CPU and Memory Resources in Kubernetes
Kubernetes is an open-source platform developed for the automation of application containers' Deployment, scaling, and operations across private, public, and hybrid cloud environments. Organizations can also use Kubernetes to manage microservice designs. Most cloud providers quite easily realize the benefits of containerization and Kubernetes deplo
5 min read
Kubernetes Node Affinity and Anti-Affinity: Advanced Scheduling
Node Affinity and Anti-Affinity are strong methods to assist you in managing the pod placement on nodes, offering sophisticated scheduling features to satisfy particular needs. Pods are scheduled on nodes with the right qualities when rules are set for pod placement on nodes based on node labels using Node Affinity. To improve fault tolerance and p
5 min read
Kubernetes - Kubectl Commands
Pre-requisites: Kubernetes The Kubernetes command-line tool, kubectl, allows you to run commands against Kubernetes clusters. You can use kubectl to deploy applications, inspect and manage cluster resources, and view logs. Some basic Kubectl commands in a Kubernetes cluster are as follows: kubectl Commands The most popular kubectl commands and flag
5 min read
Kubernetes - Introduction to Container Orchestration
In this article, we will look into Container Orchestration in Kubernetes. But first, let's explore the trends that gave rise to containers, the need for container orchestration, and how that it has created the space for Kubernetes to rise to dominance and growth. The growth of technology into every aspect of our lives and days has created immense d
4 min read
Kubernetes - Autoscaling
Pre-requisite: Kubernetes Life before Kubernetes is like writing our code and pushing the code into physical servers in a data center and managing the resources needed by that server to run our application smoothly and another type is deploying our code in virtual machines(VM). With VMs also have problems with hardware and software components requi
8 min read
Kubernetes - Physical Servers vs Virtual Machines vs Containers
Kubernetes is an open-source framework for managing containerized workloads and services that allows declarative configuration as well as automation. It has a huge and fast-expanding ecosystem. Services, support, and tools for Kubernetes are widely available. Kubernetes is a Greek word that means "helmsman" or "pilot." The acronym K8s comes from co
3 min read