Taints and Tolerations

KubernetesKubernetesBeginner
Practice Now

Introduction

Taints and tolerations are used in Kubernetes to specify which nodes are suitable for scheduling certain pods. Taints are applied to nodes to repel pods, while tolerations are applied to pods to attract them to specific nodes. In this lab, we will learn how to use taints and tolerations to schedule pods on specific nodes.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL kubernetes(("`Kubernetes`")) -.-> kubernetes/TroubleshootingandDebuggingCommandsGroup(["`Troubleshooting and Debugging Commands`"]) kubernetes(("`Kubernetes`")) -.-> kubernetes/BasicCommandsGroup(["`Basic Commands`"]) kubernetes(("`Kubernetes`")) -.-> kubernetes/AdvancedCommandsGroup(["`Advanced Commands`"]) kubernetes(("`Kubernetes`")) -.-> kubernetes/ConfigurationandVersioningGroup(["`Configuration and Versioning`"]) kubernetes(("`Kubernetes`")) -.-> kubernetes/BasicsGroup(["`Basics`"]) kubernetes/TroubleshootingandDebuggingCommandsGroup -.-> kubernetes/describe("`Describe`") kubernetes/BasicCommandsGroup -.-> kubernetes/get("`Get`") kubernetes/BasicCommandsGroup -.-> kubernetes/cordon("`Cordon`") kubernetes/BasicCommandsGroup -.-> kubernetes/taint("`Taint`") kubernetes/AdvancedCommandsGroup -.-> kubernetes/apply("`Apply`") kubernetes/ConfigurationandVersioningGroup -.-> kubernetes/label("`Label`") kubernetes/BasicsGroup -.-> kubernetes/initialization("`Initialization`") subgraph Lab Skills kubernetes/describe -.-> lab-34029{{"`Taints and Tolerations`"}} kubernetes/get -.-> lab-34029{{"`Taints and Tolerations`"}} kubernetes/cordon -.-> lab-34029{{"`Taints and Tolerations`"}} kubernetes/taint -.-> lab-34029{{"`Taints and Tolerations`"}} kubernetes/apply -.-> lab-34029{{"`Taints and Tolerations`"}} kubernetes/label -.-> lab-34029{{"`Taints and Tolerations`"}} kubernetes/initialization -.-> lab-34029{{"`Taints and Tolerations`"}} end

Start the Minikube Cluster

Before creating resources, you need a running Kubernetes cluster. Minikube is a lightweight Kubernetes environment that runs on your local machine.

  1. Navigate to your working directory:

    Open the terminal and navigate to the default project folder:

    cd /home/labex/project
  2. Start Minikube:

    Start Minikube to initialize a Kubernetes cluster:

    minikube start
    • This command sets up a single-node Kubernetes cluster on your local machine.
    • Minikube may take a few minutes to start depending on your system's performance.
  3. Verify Minikube is running:

    Check the status of the Minikube cluster:

    minikube status
    • Look for components like kubelet and apiserver listed as Running.
    • If the cluster is not running, rerun minikube start.

If you encounter issues starting Minikube. Use minikube delete to reset the environment if needed.

Creating a Tainted Node

In this step, we will create a node with a taint that will repel certain pods.

  1. Label a node with a custom label:
kubectl label nodes minikube disk-type=ssd
  1. Taint the node with the following command:
kubectl taint nodes minikube disk-type=ssd:NoSchedule

The NoSchedule effect will prevent pods without tolerations from being scheduled on this node.

Creating a Pod without Tolerations

In this step, we will create a pod without tolerations and verify that it cannot be scheduled on the tainted node.

  1. Create a file named pod-without-toleration.yaml with the following contents:
apiVersion: v1
kind: Pod
metadata:
  name: pod-without-toleration
spec:
  containers:
    - name: nginx
      image: nginx:latest
  1. Apply the changes:
kubectl apply -f pod-without-toleration.yaml
  1. Verify that the pod is not scheduled on the tainted node:
kubectl describe pod pod-without-toleration | grep -i taint

The output should show that the pod is not scheduled on the node with the taint.

Creating a Pod with Tolerations

In this step, we will create a pod with tolerations that will allow it to be scheduled on the tainted node.

  1. Create a file named pod-with-toleration.yaml with the following contents:
apiVersion: v1
kind: Pod
metadata:
  name: pod-with-toleration
spec:
  containers:
    - name: nginx
      image: nginx:latest
  tolerations:
    - key: "disk-type"
      operator: "Equal"
      value: "ssd"
      effect: "NoSchedule"
  1. Apply the changes:
kubectl apply -f pod-with-toleration.yaml
  1. Verify that the pod is scheduled on the tainted node:
kubectl get pod pod-with-toleration -o wide

Tolerating Multiple Taints

In this step, we will create a pod with multiple tolerations that will allow it to be scheduled on nodes with multiple taints.

  1. Create a file named pod-with-multiple-tolerations.yaml with the following contents:
apiVersion: v1
kind: Pod
metadata:
  name: pod-with-multiple-tolerations
spec:
  containers:
    - name: nginx
      image: nginx:latest
  tolerations:
    - key: "disk-type"
      operator: "Equal"
      value: "ssd"
      effect: "NoSchedule"
    - key: "gpu"
      operator: "Equal"
      value: "true"
      effect: "NoSchedule"
  1. Apply the changes:
kubectl apply -f pod-with-multiple-tolerations.yaml
  1. Verify that the pod is scheduled on a node with both taints:
kubectl get pod pod-with-multiple-tolerations -o wide

Summary

In this lab, we learned how to use taints and tolerations to schedule pods on specific nodes. We started by creating a tainted node and verifying that pods without tolerations could not be scheduled on it. We then created a pod with tolerations and verified that it could be scheduled on the tainted node. Finally, we created a pod with multiple tolerations and verified that it could be scheduled on nodes with multiple taints.

Taints and tolerations are a powerful feature of Kubernetes that can be used to ensure that certain workloads are only scheduled on specific nodes.

Other Kubernetes Tutorials you may like