0
0
KubernetesHow-ToBeginner · 3 min read

How to Taint a Node in Kubernetes: Simple Guide

Use the kubectl taint nodes command to add a taint to a node in Kubernetes. The syntax is kubectl taint nodes <node-name> <key>=<value>:<effect>, where effect can be NoSchedule, PreferNoSchedule, or NoExecute.
📐

Syntax

The command to taint a node in Kubernetes is:

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

Explanation of parts:

  • node-name: The name of the node you want to taint.
  • key: The identifier for the taint.
  • value: The value assigned to the taint key.
  • effect: The behavior enforced by the taint. It can be:
    • NoSchedule: Pods that do not tolerate this taint will not be scheduled on the node.
    • PreferNoSchedule: Kubernetes will try to avoid scheduling pods that do not tolerate this taint on the node.
    • NoExecute: Pods that do not tolerate this taint will be evicted if already running on the node.
bash
kubectl taint nodes <node-name> key=value:effect
💻

Example

This example taints a node named worker-node-1 with a key special, value true, and effect NoSchedule. This means pods without the matching toleration will not be scheduled on this node.

bash
kubectl taint nodes worker-node-1 special=true:NoSchedule
Output
node/worker-node-1 tainted
⚠️

Common Pitfalls

Common mistakes when tainting nodes include:

  • Using incorrect node names. Check node names with kubectl get nodes.
  • Forgetting to specify the effect (NoSchedule, PreferNoSchedule, or NoExecute).
  • Trying to taint a node with an existing taint key without removing it first.
  • Not adding matching tolerations to pods, causing them to be unschedulable.

To remove a taint, use the same command with a minus sign (-) at the end of the taint:

bash
kubectl taint nodes worker-node-1 special=true:NoSchedule-
Output
node/worker-node-1 untainted
📊

Quick Reference

CommandDescription
kubectl taint nodes key=value:NoSchedulePods without toleration won't schedule on node
kubectl taint nodes key=value:PreferNoScheduleKubernetes tries to avoid scheduling pods without toleration
kubectl taint nodes key=value:NoExecutePods without toleration are evicted from node
kubectl taint nodes key=value:effect-Remove the taint from the node

Key Takeaways

Use kubectl taint nodes with key, value, and effect to control pod scheduling on nodes.
Effects NoSchedule, PreferNoSchedule, and NoExecute define how pods are affected by the taint.
Always verify node names and specify the effect when tainting nodes.
Remove taints by appending a minus sign (-) to the taint specification.
Pods must have matching tolerations to be scheduled on tainted nodes.