Sub topic 1
Why nodes need a way to repel pods
Every pod in a cluster has to run on some node. The scheduler picks that node. This chapter explains how the scheduler decides, which controls pods have for choosing nodes, and which problem those controls leave unsolved. Taints and tolerations solve that problem in the later chapters.
Prerequisites
This chapter assumes you know the following:
- Basic Kubernetes objects: pods, nodes, and namespaces.
- How to read and write simple YAML manifests.
- How to use
kubectlto get, describe, and apply resources. - A general idea of what the Kubernetes scheduler does.
How the scheduler picks a node
When you create a pod, it has no node assigned and its status is Pending. The scheduler watches for such pods and handles each one in two steps:
- Filter. It removes every node that cannot run the pod. Reasons include too little CPU or memory, a label mismatch, or a constraint the pod does not satisfy.
- Score. It ranks the remaining nodes and picks the best one.
If filtering removes every node, the pod stays Pending, and the scheduler records the reasons as events on the pod.
Constraints that start from the pod
The scheduling controls most people learn first all live in the pod spec. The pod says what it needs, and the scheduler removes nodes that do not match.
- Resource requests make the scheduler skip nodes without enough free CPU or memory.
nodeSelectorlimits a pod to nodes that carry specific labels.- Node affinity is a more expressive form of
nodeSelector. It supports operators such asInandNotIn, and it can be a preference instead of a requirement.
Here is a pod that asks for nodes labeled disktype=ssd:
apiVersion: v1
kind: Pod
metadata:
name: fast-disk-app
spec:
nodeSelector:
disktype: ssd
containers:
- name: app
image: nginxYou can check which labels your nodes have with this command:
kubectl get nodes --show-labelsAll of these controls work in one direction. The pod chooses which nodes it is willing to run on.
The gap: nodes have no say
Now consider the cluster operator's point of view. Some nodes are special:
- A few nodes have expensive GPUs that only machine-learning jobs should use.
- A node is reserved for a team that paid for it.
- A node has a hardware problem and should stop receiving new work.
- The control plane nodes should run cluster components, not ordinary applications.
Labels and nodeSelector do not solve this. A node label only helps pods that ask for it. A pod without a nodeSelector can still land on the GPU node, because nothing filters that node out for it. To keep ordinary pods off the node, you would have to edit every other workload and add a rule that avoids it. That is slow, easy to forget, and it breaks as soon as someone deploys a new manifest that lacks the rule.
The fix has to come from the node's side. A node needs a way to say "keep pods away unless they have been explicitly approved." That is what a taint does. The approval a pod carries is called a toleration.
| Control | Set on | Direction |
|---|---|---|
| `nodeSelector`, node affinity | Pod | Pod chooses nodes |
| Taint | Node | Node repels pods |
| Toleration | Pod | Pod accepts a node's repulsion |
What comes next
The next chapters build the mechanism in order:
- Taints, the marks placed on nodes, and their three effects.
- How to add and remove taints with
kubectl. - Tolerations, and how they match taints.
- How to combine taints with node affinity to dedicate nodes to a workload.
- Eviction of running pods, the taints Kubernetes adds on its own, and debugging.
Exercise
Exercise
A cluster has one GPU node, labeled gpu=true. You add the same nodeSelector (gpu=true) to your machine-learning pods. A teammate then deploys a web app with no nodeSelector, and one of its pods lands on the GPU node. Why did the label and nodeSelector not prevent this, and what kind of control is missing?
Show answer
A nodeSelector is a pod-side constraint, and it only applies to pods that include it. The web app's pods have no such rule, so the GPU node passes the scheduler's filter for them like any other node. The missing control works from the node's side. The node needs to repel pods by default and admit only approved ones. That is what a taint on the node, combined with a toleration on the approved pods, provides.