Taints and Tolerations

Taints and Tolerations

  • It’s a way to control which pods are allowed to be scheduled on a given node.
  • Taints are key-value pairs. Once we apply a taint to a node, only those pods that are tolerant to that taint can be scheduled on that node. This prevents the intolerant pods from being scheduled on this node. This however, does not mean that the tolerant pod can only be scheduled on the tainted node. It can very well be scheduled on another node that is not tainted. If we want to restrict some pods to certain nodes, we need to use
    Node Affinity
    Node Affinity
    .
💡
When a K8s cluster is setup. A taint is automatically applied to the master node to prevent any pod from being scheduled on the master node. This is to prevent other processes from starving the master processes required to run the K8s cluster.
notion image
  • Taint effect defines what happens to pods that don’t tolerate this taint. There are 3 possible taint effects:
    • NoSchedule - do not schedule new intolerant pods on this node
    • PreferNoSchedule - try not to schedule intolerant pods on this node
    • NoExecute - do not schedule intolerant pods on this node; if there are existing intolerant pods on this node, they will be evicted from the node

Commands

  • Taint a node - kubectl taint node <node-name> <key>=<value>:<taint-effect>
  • Un-taint a node - kubectl taint node <node-name> <key>=<value>:<taint-effect>-
  • List taints on a node - k describe node <node-name> | grep Taints

Apply toleration to a pod

The below pod definition file tolerates the taint applied to the node as: kubectl taint node node1 compute=high:NoSchedule
apiVersion: v1 kind: Pod metadata: name: web-pod spec: tolerations: - key: compute operator: Equal value: high effect: NoSchedule containers: - name: nginx image: nginx