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/TroubleshootingandDebuggingCommandsGroup -.-> kubernetes/describe("`Describe`") kubernetes/BasicCommandsGroup -.-> kubernetes/get("`Get`") kubernetes/BasicCommandsGroup -.-> kubernetes/taint("`Taint`") kubernetes/AdvancedCommandsGroup -.-> kubernetes/apply("`Apply`") kubernetes/ConfigurationandVersioningGroup -.-> kubernetes/label("`Label`") subgraph Lab Skills kubernetes/describe -.-> lab-34029{{"`Taints and Tolerations`"}} kubernetes/get -.-> 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`"}} end

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