⎈ k8s knowledge compiler

Taints and Tolerations [page]deterministic

concepts

[_Node affinity_](/docs/concepts/scheduling-eviction/assign-pod-node/#affinity-and-anti-affinity) is a property of [Pods](#gloss:pod) that *attracts* them to a set of [nodes](#gloss:node) (either as a preference or a hard requirement). _Taints_ are the opposite -- they allow a node to repel a set of pods.

_Tolerations_ are applied to pods. Tolerations allow the scheduler to schedule pods with matching taints. Tolerations allow scheduling but don't guarantee scheduling: the scheduler also [evaluates other parameters](/docs/concepts/scheduling-eviction/pod-priority-preemption/) as part of its function.

Taints and tolerations work together to ensure that pods are not scheduled onto inappropriate nodes. One or more taints are applied to a node; this marks that the node should not accept any pods that do not tolerate the taints.

## Concepts

You add a taint to a node using [kubectl taint](/docs/reference/generated/kubectl/kubectl-commands#taint). For example,

```shell kubectl taint nodes node1 key1=value1:NoSchedule ```

places a taint on node `node1`. The taint has key `key1`, value `value1`, and taint effect `NoSchedule`. This means that no pod will be able to schedule onto `node1` unless it has a matching toleration.

To remove the taint added by the command above, you can run:

```shell kubectl taint nodes node1 key1=value1:NoSchedule- ```

You specify a toleration for a pod in the PodSpec. Both of the following tolerations "match" the taint created by the `kubectl taint` line above, and thus a pod with either toleration would be able to schedule onto `node1`:

```yaml tolerations: - key: "key1" operator: "Equal" value: "value1" effect: "NoSchedule" ```

```yaml tolerations: - key: "key1" operator: "Exists" effect: "NoSchedule" ```

The default Kubernetes scheduler takes taints and tolerations into account when selecting a node to run a particular Pod. However, if you manually specify the `.spec.nodeName` for a Pod, that action bypasses the scheduler; the Pod is then bound onto the node where you assigned it, even if there are `NoSchedule` taints on that node that you selected. If this happens and the node also has a `NoExecute` taint set, the kubelet will eject the Pod unless there is an appropriate tolerance set.

Here's an example of a pod that has some tolerations defined:

The default value for `operator` is `Equal`.

A toleration "matches" a taint if the keys are the same and the effects are the same, and:

* the `operator` is `Exists` (in which case no `value` should be specified), or * the `operator` is `Equal` and the values should be equal.

> Note: There are two special cases:

If the `key` is empty, then the `operator` must be `Exists`, which matches all keys and values. Note that the `effect` still needs to be matched at the same time.

An empty `effect` matches all effects with key `key1`.

The above example used the `effect` of `NoSchedule`. Alternatively, you can use the `effect` of `PreferNoSchedule`.

The allowed values for the `effect` field are:

`NoExecute` : This affects pods that are already running on the node as follows:

* Pods that do not tolerate the taint are evicted immediately * Pods that tolerate the taint without specifying `tolerationSeconds` in their toleration specification remain bound forever * Pods that tolerate the taint with a specified `tolerationSeconds` remain bound for the specified amount of time. After that time elapses, the node lifecycle controller evicts the Pods from the node.

`NoSchedule` : No new Pods will be scheduled on the tainted node unless they have a matching toleration. Pods currently running on the node are not evicted.

`PreferNoSchedule` : `PreferNoSchedule` is a "preference" or "soft" version of `NoSchedule`. The control plane will *try* to avoid placing a Pod that does not tolerate the taint on the node, but it is not guaranteed.

You can put multiple taints on the same node and multiple tolerations on the sam …(trimmed)

Sources

concepts/scheduling-eviction/taint-and-toleration.md · docTaints and Tolerations

Related (12)

references PodPods conf=1
references Nodenodes conf=1
references Controllercontroller conf=1
references QoS ClassQoS class conf=1
part_of Conceptsdescribes conf=1
part_of Example Use Casesdescribes conf=1
part_of Taint based Evictionsdescribes conf=1
part_of Taint Nodes by Conditiondescribes conf=1
part_of Device taints and tolerationsdescribes conf=1
part_of {{% heading "whatsnext" %}}describes conf=1
api_for Taintdocuments API object conf=1

← all Docs