Running Pods on Only Some Nodes

This page demonstrates how can you run Pods on only some Nodes as part of a DaemonSet

Before you begin

You need to have a Kubernetes cluster, and the kubectl command-line tool must be configured to communicate with your cluster. It is recommended to run this tutorial on a cluster with at least two nodes that are not acting as control plane hosts. If you do not already have a cluster, you can create one by using minikube or you can use one of these Kubernetes playgrounds:

Running Pods on only some Nodes

Imagine that you want to run a DaemonSet, but you only need to run those daemon pods on nodes that have local solid state (SSD) storage. For example, the Pod might provide cache service to the node, and the cache is only useful when low-latency local storage is available.

Step 1: Add labels to your nodes

Add the label ssd=true to the nodes which have SSDs.

  1. kubectl label nodes example-node-1 example-node-2 ssd=true

Step 2: Create the manifest

Let’s create a DaemonSet which will provision the daemon pods on the SSD labeled nodes only.

Next, use a nodeSelector to ensure that the DaemonSet only runs Pods on nodes with the ssd label set to "true".

controllers/daemonset-label-selector.yaml Running Pods on Only Some Nodes - 图1

  1. apiVersion: apps/v1
  2. kind: DaemonSet
  3. metadata:
  4. name: ssd-driver
  5. labels:
  6. app: nginx
  7. spec:
  8. selector:
  9. matchLabels:
  10. app: ssd-driver-pod
  11. template:
  12. metadata:
  13. labels:
  14. app: ssd-driver-pod
  15. spec:
  16. nodeSelector:
  17. ssd: "true"
  18. containers:
  19. - name: example-container
  20. image: example-image

Step 3: Create the DaemonSet

Create the DaemonSet from the manifest by using kubectl create or kubectl apply

Let’s label another node as ssd=true.

  1. kubectl label nodes example-node-3 ssd=true

Labelling the node automatically triggers the control plane (specifically, the DaemonSet controller) to run a new daemon pod on that node.

  1. kubectl get pods -o wide

The output is similar to:

  1. NAME READY STATUS RESTARTS AGE IP NODE
  2. <daemonset-name><some-hash-01> 1/1 Running 0 13s ..... example-node-1
  3. <daemonset-name><some-hash-02> 1/1 Running 0 13s ..... example-node-2
  4. <daemonset-name><some-hash-03> 1/1 Running 0 5s ..... example-node-3