First, let’s check what exactly taints and toleration in Kubernetes.
Taint
Taints are a property of nodes that push pods away if they don't tolerate this taint. Like Labels, one or more Taints can be applied to a node. This means that the node must not accept any pod that does not tolerate all of these taints.
Toleration
Tolerations are applied to pods, and allow the pods to schedule onto nodes with matching taints.
Taint and toleration
Taints and tolerations work together to ensure that pods are not scheduled onto inappropriate nodes. You can add one or multiple taints and tolerations on your nodes and pods. A taint applied to a node marks that the node should not accept any pods that do not tolerate the taints.
Taint is like key=value:Effect. Taint effect defines the behavior of the pod in regards of the tainted node.
You can assign 3 different values to “effect:
The default value for the operator is “Equal. But it can also be “Exist.
This means that if you change the operator by “exist you don’t have to give a value. If the taint isn’t present on the node, “effect is applied to the pod.
Let's illustrate with schemas. First, when there is no taint, pod can be affected to any node(here Pod1 is attached to Node1, but it could also be on Node2).
<schema-without-taint>
Now, add a taint on the node:taint=test:NoSchedule
This means if the pod doesn't match this taint, it can’t be scheduled on this node. You can see on the schema below, “the new pod doesn’t have toleration, so it has been affected on Node2. But Pod2, which has toleration that matches the taint, can be affected on Node1.
<schema-with-noschedule-taint>
Now, if we add a taint on the node:taint=test:NoExecute
It will exclude all pods which have toleration that doesn’t match the taint.
<schema-with-noexecute-taint>
To add a taint to an existing node, you can run the following command:
$ kubectl taint nodes node-name key=value:effect
Example: $ kubectl taint nodes node-main taint=test:PrefereNoSchedule
To show taint of your node you can run this command:
$ kubectl describe node
Name: node-main
Roles: <none>
Labels: [...]
Annotations: [...]
CreationTimestamp: Sat, 17 Apr 2021 04:38:19 +0200
Taints: taint=test:PreferNoSchedule
[...]
Now that your nodes are tainted, here is an example to add tolerations to your pod:
tolerations:
key: "key1"
operator: "Equal"
value: "value1"
Example :
Taint and toleration are useful if you want to work with dedicated nodes. With dedicated nodes, you can create a node pool with very specific parameters(for example high CPU) and use these nodes only for specific applications. You also can separate your applicative pods from those of your secondary applications.
Pay attention to the rules defined in the applied taints. Indeed, if a strong taint is applied to all the nodes and a pod does not have any tolerance, it will not be able to be executed anywhere. Another example: if several taints are applied to a node, the strongest win