K8s Taint and Toleration

ยท

4 min read

Taints and Tolerations are mechanisms used to control pod scheduling and allow or prevent certain pods from running on specific nodes. They are typically used in scenarios where you have nodes with specific requirements or constraints.

  1. Taints: A taint is a label applied to a node that repels pods. It indicates that the node has some specific requirements or constraints, making it unsuitable for running certain types of pods. Taints prevent pods from being scheduled onto nodes unless the pods have corresponding tolerations.

  2. Tolerations: A toleration is a configuration setting that allows a pod to tolerate (or accept) nodes with specific taints. When a pod has a toleration for a particular taint, it can be scheduled on nodes with that taint. Tolerations are specified in the pod's specification, and each toleration contains the key and value that match the taint on the node

Here's an example to illustrate the usage of taints and tolerations:

  1. Taint a node:

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

    The <key>=<value> pair represents the label applied to the node, and <effect> specifies the taint effect, which can be one of three values: NoSchedule, PreferNoSchedule, or NoExecute.

Taints Effects:

the effect field is used to specify the behavior of a taint or toleration. It determines how the presence of a taint or toleration affects pod scheduling on a node. There are three possible values for the effect field:

  1. NoSchedule: If a node has a taint with the effect set to NoSchedule, pods without a matching toleration will not be scheduled on that node by the Kubernetes scheduler. Existing pods on the node are not affected.

  2. PreferNoSchedule: When a node has a taint with the effect set to PreferNoSchedule, the Kubernetes scheduler will try to avoid scheduling pods without a matching toleration on that node. However, it is not an absolute restriction like NoSchedule. If no other suitable nodes are available, pods without tolerations can still be scheduled on the node.

  3. NoExecute: If a node has a taint with the effect set to NoExecute, any pod that does not have a matching toleration will be evicted from the node. This can happen when a taint is added to a running node or when a toleration is removed from a pod. Existing pods on the node that were already scheduled with the toleration are not affected.

Create a pod with a toleration:

apiVersion: v1
kind: Pod
metadata:
  name: mypod
spec:
  containers:
    - name: mycontainer
      image: nginx
  tolerations:
    - key: <key>
      operator: <operator>
      value: <value>
      effect: <effect>

The <key>, <value>, and <effect> must match the taint on the node. The <operator> can be one of three values: Exists, Equal, or DoubleEqual. The Exists operator ignores the <value> field.

In Kubernetes, the operator field is used in the toleration specification to define how the value of the toleration should be compared with the value of the taint on a node. The operator field can take one of the following three values:

  1. Equal: This is the default operator. When the operator is set to Equal, the value of the toleration must be an exact match to the value of the taint on the node for the toleration to be considered a match. For example, if the taint has a value of "high-priority" and the toleration has a value of "high-priority", they would be considered a match.

  2. Exists: When the operator is set to Exists, the value of the toleration is ignored. This operator is used to indicate that any value of the taint is acceptable. It only checks if the key of the toleration matches the key of the taint. This allows pods to tolerate nodes with any value of the taint. For example, if the taint has a key of "gpu" with any value, a toleration with the key "gpu" and operator "Exists" would match.

  3. DoubleEqual: The DoubleEqual operator is used when you want to compare the value of the toleration with the value of the taint using a case-insensitive comparison. It checks for an exact match regardless of the case of the values. For example, if the taint has a value of "High-Priority" and the toleration has a value of "high-priority", they would be considered a match with the DoubleEqual operator.

Did you find this article valuable?

Support Naveen Elwaka by becoming a sponsor. Any amount is appreciated!

ย