Kubernetes Day 14: Mastering Taints and Tolerations

Kubernetes is a powerful container orchestration platform that automates deployment, scaling, and management of containerized applications. One of the critical features of Kubernetes is its scheduling mechanism, which ensures that pods are efficiently placed on nodes. Taints and Tolerations are mechanisms within Kubernetes that influence this scheduling process by allowing certain nodes to repel specific pods.

What Are Taints and Tolerations?

Taints are applied to nodes and allow a node to repel a set of pods. Think of taints as a way to mark a node with specific characteristics that make it unsuitable for certain pods.

Tolerations are applied to pods and allow the pods to tolerate (i.e., be scheduled on) nodes with specific taints. Tolerations enable exceptions to the rules set by taints.

How Taints and Tolerations Work

Taints and Tolerations work together to ensure that pods are only scheduled on appropriate nodes. Here’s how they interact:

  1. Applying Taints: A taint is added to a node to mark it with a key-value pair and an effect. This indicates that only pods with matching tolerations should be scheduled on that node.

  2. Applying Tolerations: Tolerations are added to pods, allowing them to be scheduled on nodes with matching taints.

  3. Scheduling Decision: The Kubernetes scheduler checks the taints on each node and the tolerations on each pod. If a pod tolerates a node’s taint, it can be scheduled on that node.

Taints and Tolerations in Action: A Practical Example

Let's look at a practical example of how to use taints and tolerations in a Kubernetes cluster.

Step 1: Apply a Taint to a Node

To taint a node, you can use the kubectl taint command. Suppose we have a node named node1 and we want to add a taint that marks it as a special node for high-memory applications.

kubectl taint nodes node1 key=value:NoSchedule

This command adds a taint with the key key, the value value, and the effect NoSchedule. The effect NoSchedule means that no pod will be scheduled on this node unless it has a matching toleration.

Step 2: Apply a Toleration to a Pod

Now, let's create a pod that can tolerate the taint on node1. This is done by adding a toleration in the pod specification.

apiVersion: v1
kind: Pod
metadata:
  name: mypod
spec:
  containers:
  - name: mycontainer
    image: nginx
  tolerations:
  - key: "key"
    operator: "Equal"
    value: "value"
    effect: "NoSchedule"

In this YAML file, the pod mypod has a toleration that matches the taint on node1, allowing it to be scheduled on that node.

Effects of Taints and Tolerations

There are three possible effects when a taint is applied to a node:

  1. NoSchedule: Pods that do not tolerate the taint will not be scheduled on the node.

  2. PreferNoSchedule: Kubernetes will try to avoid scheduling pods that do not tolerate the taint on the node but will not guarantee it.

  3. NoExecute: Pods that do not tolerate the taint will be evicted from the node if they are already running and new pods will not be scheduled on the node.

Scheduling a Pod with Taints and Tolerations

NoSchedule Effect

apiVersion: v1
kind: Pod
metadata:
  name: noschedule-pod
spec:
  containers:
  - name: mycontainer
    image: nginx
  tolerations:
  - key: "key"
    operator: "Equal"
    value: "value"
    effect: "NoSchedule"

This pod will only be scheduled on nodes with a matching toleration.

PreferNoSchedule Effect

apiVersion: v1
kind: Pod
metadata:
  name: prefernoschedule-pod
spec:
  containers:
  - name: mycontainer
    image: nginx
  tolerations:
  - key: "key"
    operator: "Equal"
    value: "value"
    effect: "PreferNoSchedule"

Kubernetes will try to avoid scheduling this pod on nodes with the taint, but if no other nodes are available, it will be scheduled on the tainted node.

NoExecute Effect

apiVersion: v1
kind: Pod
metadata:
  name: noexecute-pod
spec:
  containers:
  - name: mycontainer
    image: nginx
  tolerations:
  - key: "key"
    operator: "Equal"
    value: "value"
    effect: "NoExecute"
    tolerationSeconds: 3600

This pod will tolerate the taint for 3600 seconds (1 hour) if it is already running on the node. After that, it will be evicted.

Differences Between NoSchedule, PreferNoSchedule, and NoExecute

  • NoSchedule: Ensures that pods without the toleration will never be scheduled on the tainted node.

  • PreferNoSchedule: Indicates a preference to avoid scheduling pods without the toleration on the tainted node, but it is not enforced.

  • NoExecute: Applies to both new and already running pods. Pods without the toleration will be evicted if they are running and will not be scheduled if they are new.

Conclusion

Taints and Tolerations are powerful tools in Kubernetes for managing pod placement on nodes. By understanding and using these features, you can ensure that your workloads are efficiently and appropriately scheduled, improving the performance and reliability of your applications. Whether you're isolating specific workloads, managing node resources, or ensuring high availability, Taints and Tolerations provide the flexibility needed to tailor your Kubernetes cluster to your specific needs.

Reference

Video

Documentation

0
Subscribe to my newsletter

Read articles from Rahul Vadakkiniyil directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Rahul Vadakkiniyil
Rahul Vadakkiniyil