concept DaemonSet in category kubernetes

appears as: DaemonSet, DaemonSet, A DaemonSet, The DaemonSet
Core Kubernetes

This is an excerpt from Manning's book Core Kubernetes.

If you understand the concept of Pods, Deployments, and the basic Kubernetes API patterns, you probably can skip over this chapter, but it is not a long read, and you may glean something new. Pods are the atomic unit of execution in Kubernetes, and we’ve mentioned before that they are a significant differentiator between Kubernetes and its predecessor runtimes, like Mesos, Docker Swarm, and cloud container execution environments such as Amazon’s Elastic Container Service (ECS). As abstractions such as Helm charts and operators become more commonplace, it is easy to overlook the fact that Pods can manifest through many different pathways in the Kubernetes API. If you’ve ever wondered whether a Deployment, StatefulSet, or DaemonSet was the ideal solution for your distributed app, this is the chapter for you.

Kubernetes in Action

This is an excerpt from Manning's book Kubernetes in Action.

4.4.1. Using a DaemonSet to run a pod on every node

To run a pod on all cluster nodes, you create a DaemonSet object, which is much like a ReplicationController or a ReplicaSet, except that pods created by a DaemonSet already have a target node specified and skip the Kubernetes Scheduler. They aren’t scattered around the cluster randomly.

A DaemonSet makes sure it creates as many pods as there are nodes and deploys each one on its own node, as shown in figure 4.8.

Whereas a ReplicaSet (or ReplicationController) makes sure that a desired number of pod replicas exist in the cluster, a DaemonSet doesn’t have any notion of a desired replica count. It doesn’t need it because its job is to ensure that a pod matching its pod selector is running on each node.

If a node goes down, the DaemonSet doesn’t cause the pod to be created elsewhere. But when a new node is added to the cluster, the DaemonSet immediately deploys a new pod instance to it. It also does the same if someone inadvertently deletes one of the pods, leaving the node without the DaemonSet’s pod. Like a ReplicaSet, a DaemonSet creates the pod from the pod template configured in it.

4.4.2. Using a DaemonSet to run pods only on certain nodes

A DaemonSet deploys pods to all nodes in the cluster, unless you specify that the pods should only run on a subset of all the nodes. This is done by specifying the node-Selector property in the pod template, which is part of the DaemonSet definition (similar to the pod template in a ReplicaSet or ReplicationController).

You’ve already used node selectors to deploy a pod onto specific nodes in chapter 3. A node selector in a DaemonSet is similar—it defines the nodes the DaemonSet must deploy its pods to.

Let’s imagine having a daemon called ssd-monitor that needs to run on all nodes that contain a solid-state drive (SSD). You’ll create a DaemonSet that runs this daemon on all nodes that are marked as having an SSD. The cluster administrators have added the disk=ssd label to all such nodes, so you’ll create the DaemonSet with a node selector that only selects nodes with that label, as shown in figure 4.9.

Figure 4.9. Using a DaemonSet with a node selector to deploy system pods only on certain nodes

You’ll create a DaemonSet that runs a mock ssd-monitor process, which prints “SSD OK” to the standard output every five seconds. I’ve already prepared the mock container image and pushed it to Docker Hub, so you can use it instead of building your own. Create the YAML for the DaemonSet, as shown in the following listing.

Listing 4.10. A YAML for a DaemonSet: ssd-monitor-daemonset.yaml
apiVersion: apps/v1beta2           #1
kind: DaemonSet                    #1
metadata:
  name: ssd-monitor
spec:
  selector:
    matchLabels:
      app: ssd-monitor
  template:
    metadata:
      labels:
        app: ssd-monitor
    spec:
      nodeSelector:                #2
        disk: ssd                  #2
      containers:
      - name: main
        image: luksa/ssd-monitor

You’re defining a DaemonSet that will run a pod with a single container based on the luksa/ssd-monitor container image. An instance of this pod will be created for each node that has the disk=ssd label.