Kubernetes DaemonSets

Hello there! In this blog post, we are going to discuss Kubernetes Daemon Sets. We will explore what they are, when they are used and how we can deploy our very own Daemon Set in a Kubernetes Cluster.

What is a DaemonSet

A DaemonSet ensures that all (or some) Nodes run a copy of a Pod. As nodes are added to the cluster, Pods are added to them. As nodes are removed from the cluster, those Pods are garbage collected. Deleting a DaemonSet will clean up the Pods it created.

Some typical uses of a DaemonSet are:

  • running a cluster storage daemon on every node
  • running a logs collection daemon on every node
  • running a node monitoring daemon on every node

Did you know?
DaemonSets are similar to Deployments in that they both create Pods, and those Pods have processes which are not expected to terminate (e.g. web servers, storage servers).
Use a Deployment for stateless services, like frontends, where scaling up and down the number of replicas and rolling out updates are more important than controlling exactly which host the Pod runs on. Use a DaemonSet when it is important that a copy of a Pod always run on all or certain hosts, if the DaemonSet provides node-level functionality that allows other Pods to run correctly on that particular node.

How to create a DaemonSet?

You can create a DaemonSet using a YAML file. A sample DaemonSet file is provided below.

apiVersion: apps/v1
kind: DaemonSet
metadata:
  name: nginx-daemonset
  labels:
    app: nginx
spec:
  selector:
    matchLabels:
      name: nginx-daemonset
  template:
    metadata:
      labels:
        name: nginx-daemonset
    spec:
      containers:
      - name: nginx-daemonset
        image: nginx:1.14.2
        ports:
        - containerPort: 80

Note:
As with all other Kubernetes config, a DaemonSet needs apiVersion, kind, and metadata fields.
Pod Template
The .spec.template is one of the required fields in .spec. The .spec.template is a pod template. It has exactly the same schema as a Pod, except it is nested and does not have an apiVersion or kind.

In addition to required fields for a Pod, a Pod template in a DaemonSet has to specify appropriate labels (see pod selector).

A Pod Template in a DaemonSet must have a [RestartPolicy][4] equal to Always, or be unspecified, which defaults to Always.

Pod Selector
The .spec.selector field is a pod selector. It works the same as the .spec.selector of a Job.

As of Kubernetes 1.8, you must specify a pod selector that matches the labels of the .spec.template. The pod selector will no longer be defaulted when left empty. Selector defaulting was not compatible with kubectl apply. Also, once a DaemonSet is created, its .spec.selector can not be mutated. Mutating the pod selector can lead to the unintentional orphaning of Pods, and it was found to be confusing to users.

The .spec.selector is an object consisting of two fields:

  • matchLabels - works the same as the .spec.selector of a [ReplicationController][6].
  • matchExpressions - allows to build more sophisticated selectors by specifying key, list of values and an operator that relates the key and values.

When the two are specified the result is ANDed.

If the .spec.selector is specified, it must match the .spec.template.metadata.labels. Config with these not matching will be rejected by the API.

Running Pods on select Nodes
If you specify a .spec.template.spec.nodeSelector, then the DaemonSet controller will create Pods on nodes which match that node selector. Likewise if you specify a .spec.template.spec.affinity, then DaemonSet controller will create Pods on nodes which match that node affinity. If you do not specify either, then the DaemonSet controller will create Pods on all nodes.

In order to create this DaemonSet on your Kubernetes cluster, open up the command line to the master node in your cluster and copy over the file to a directory of your choice. Issue the following command.
kubectl apply -f daemonset.yaml

In order to check the status of your daemonset, issue the following command.
kubectl get daemonset -l name=nginx

In order to see the details of the daemonset, issue the following command.
kubectl describe daemonset -l name=nginx

Use the following command to delete the daemonset from the cluster.
kubectl delete -f daemonset.yaml

In my next micro blog, we will discuss more about other workload resources. Hope you learned something new! ☺️

Previous
Previous

Kubernetes Deployments

Next
Next

Kubernetes Pods