CodeBlanks Logo Icon CodeBlanks

Node selector

Learn to use nodeSelector in a DaemonSet.

Node selector

To run a DaemonSet only on some nodes, nodeSelector is written in the template; only nodes carrying those labels count as eligible:

kubectl label node worker-2 disk=ssd

    spec:
      nodeSelector:
        disk: ssd
      containers:
        - name: ssd-izleyici
          image: ssd-izleyici:1.0

For more flexible conditions (e.g. "gpu=a100 or gpu=h100"), affinity.nodeAffinity is used.

Taints and tolerations

Some nodes are marked with a taint and repel pods without a toleration (e.g. the control-plane node). DaemonSets that must be on every node, like a log collector, add tolerations for these taints:

$ kubectl describe node control-plane | grep Taints
Taints: node-role.kubernetes.io/control-plane:NoSchedule

    spec:
      tolerations:
        - key: node-role.kubernetes.io/control-plane
          operator: Exists
          effect: NoSchedule

Practice Run on selected nodes

Two steps: write the command adding the disk=ssd label to the worker-2 node; then write the DaemonSet template's spec section (starting with spec:): disk: ssd under nodeSelector; container named ssd-izleyici, image ssd-izleyici:1.0.

Starter code

terminal
# buraya yaz

Sample solution

terminal
kubectl label node worker-2 disk=ssd

spec:
  nodeSelector:
    disk: ssd
  containers:
    - name: ssd-izleyici
      image: ssd-izleyici:1.0

Expected output

node/worker-2 labeled
daemonset.apps/ssd-izleyici created
NAME           DESIRED   CURRENT   READY   NODE SELECTOR
ssd-izleyici   1         1         1       disk=ssd

Questions and answers

Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.

1 If the disk=ssd label is removed from a node, what happens to the DaemonSet pod on it?

  • It restarts
  • The pod is removed Correct answer
  • It keeps running
  • It moves to another node

Explanation: The DaemonSet continuously watches the set of eligible nodes.

2 Place the pod only on labelled nodes.

Question
    spec:
      ____:
        disk: ssd
Answer
    spec:
      nodeSelector:
        disk: ssd

Explanation: nodeSelector matches node labels.

3 Match.

Term Meaning
nodeSelector Labelled nodes
nodeAffinity Flexible node condition
taint The node repels pods
toleration Allowance for a taint

All lessons in Advanced