Introduction
Are you looking to master Kubernetes taints and tolerations but feeling overwhelmed by the jargon? Look no further! In this short guide, I’ll walk through a simple demo to help you grasp the basics.
Whether you’re a Kubernetes beginner or a seasoned DevOps engineer, this tutorial will demystify taints and tolerations so you can apply them effectively in your own clusters.
For more depth, be sure to check out the video tutorial accompanying this blog post!
What Are Taints and Tolerations?
In Kubernetes, taints and tolerations are key mechanisms that ensure workloads are placed on the appropriate nodes. Here’s a quick overview:
- Taints: Add a condition to nodes that prevents workloads from being scheduled there unless explicitly allowed.
- Tolerations: Specify which taints a pod can tolerate, allowing them to be scheduled on nodes with specific taints.
Together, they prevent unwanted pods from occupying unsuitable nodes and ensure critical workloads run on the right hardware.
Setting Up the Demo Environment
requirements
Install Docker and Kind on your system if you haven’t done so yet. Check out these guides for assistance:
- How to Install Docker on Ubuntu
- How to install Docker on Windows
- How to install Docker on MacOS
- How to install Kind on Ubuntu
- How to install Kind on MacOs
- How to install Kind on Windows
1. CLONE THE REPOSITORY
First things first, let’s get started by cloning the repository that contains our project files.
git clone https://github.com/thiagousa/youtube.git
cd youtube/16
Creating a Cluster with Kind
We’ll use kind
to create a local cluster. The following Makefile commands will set up a cluster with one control plane and two worker nodes:
# Create a Kind cluster with 1 control plane and 2 worker nodes
create-cluster:
kind create cluster --name taint --config kind-config.yaml
- Run
make create-cluster
to create the cluster.
make create-cluster
Adding Taints
Add taints to your worker nodes using the Makefile:
make taint
: Taints the worker nodegpu="yes":NoSchedule
, which prevents non-tolerant pods from being scheduled on it.
make taint
Deploying Pods
We will deploy two example pods with different toleration configurations.
- Pod A: Doesn’t tolerate taints.
Use the following command to deploy the pod a without tolerations:
make deploy-a
- File:
pod-a.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
labels:
app: pod-a
name: pod-a
spec:
replicas: 1
selector:
matchLabels:
app: pod-a
strategy: {}
template:
metadata:
labels:
app: pod-a
spec:
containers:
- image: thiagousa/right:latest
name: right
resources: {}
Pod B: Tolerates specific taints.
Use the following command to deploy the pod a with tolerations:
make deploy-b
- File:
pod-b.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
creationTimestamp: null
labels:
app: pod-b
name: pod-b
spec:
replicas: 1
selector:
matchLabels:
app: pod-b
strategy: {}
template:
metadata:
creationTimestamp: null
labels:
app: pod-b
spec:
### Add the Toleration here
tolerations:
- key: "gpu"
operator: "Equal"
value: "yes"
effect: "NoSchedule"
containers:
- image: thiagousa/right:latest
name: right
Testing Tolerations
To see how taints and tolerations affect pod scheduling:
- Check pod distribution using
make check
.
make check
- Run
make describe
to inspect nodetaint-worker
and verify its taints.
make describe
Removing Taints
To remove the taints, use:
make untaint
Monitoring Applications
To ensure your apps are healthy and responding, explore this Application Monitoring with Docker guide.
Conclusion
By following this demo, you’ve gained hands-on experience in controlling pod scheduling with Kubernetes taints and tolerations. This skill is crucial for optimizing your workloads across diverse hardware. Let me know in the comments or video if you have questions or insights to share.
