How to use nodeSelector with DaemonSets?

QuestionsQuestions8 SkillsProDec, 13 2025
0170

Okay, excellent question! You're ready to explore how to be more selective with your DaemonSets.

While a DaemonSet's default behavior is to run on every qualifying node, you can absolutely use nodeSelector to tell it to only run on a subset of nodes that have specific labels. This is very useful when you have heterogeneous clusters (e.g., some nodes with GPUs, some with high memory, some with specific operating systems).

Here's how you would modify your myapp-daemonset.yaml to use a nodeSelector:

1. Identify your target node(s):
First, you need to know which nodes you want to target and what labels they have (or you can add a label to them).

Let's imagine you have a node that you've labeled for "production" type workloads using a command like this:

kubectl label node <your-node-name> node-type=production

(You can find your node names by running kubectl get nodes)

2. Modify the DaemonSet's template.spec:
You'll add the nodeSelector directly under spec within the template section of your DaemonSet.

apiVersion: apps/v1
kind: DaemonSet
metadata:
  name: myapp-daemonset-prod
spec:
  selector:
    matchLabels:
      app: myapp-prod # Updated label for clarity
  template:
    metadata:
      labels:
        app: myapp-prod # Updated label for clarity
    spec:
      # Add the nodeSelector here
      nodeSelector:
        node-type: production # The key-value pair of the label you want to match
      containers:
        - name: myapp-container
          image: nginx
          ports:
            - containerPort: 80

Explanation:

  • nodeSelector is placed inside the spec of the pod template (template.spec).
  • It takes key-value pairs. The DaemonSet will only schedule pods onto nodes that have all of the specified labels. In this example, it will only schedule myapp-prod pods on nodes that have the label node-type=production.

Steps to try it out (optional, but good for understanding):

  1. Get Node Names:
    kubectl get nodes
  2. Choose a Node and Label It (if you haven't already):
    Let's say one of your node names is minikube (which is often the case in a single-node Minikube setup).
    kubectl label node minikube node-type=production
    (You can verify the label with kubectl get node minikube -o yaml | grep node-type)
  3. Save the modified DaemonSet:
    Save the YAML above (with nodeSelector) as myapp-daemonset-prod.yaml.
  4. Apply the DaemonSet:
    kubectl apply -f myapp-daemonset-prod.yaml
  5. Verify:
    kubectl get ds myapp-daemonset-prod
    kubectl get pods -l app=myapp-prod -o wide
    You should see the pod scheduled on the minikube node (if it's the only one with that label). If you had other nodes, and they were not labeled node-type=production, the DaemonSet would not create pods on them.

By using nodeSelector, you gain fine-grained control over where your DaemonSet managed pods are deployed! Let me know if you'd like to try an example or have more questions about this!

0 Comments

no data
Be the first to share your comment!