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.

Lets create a DaemonSet

The following is an example DaemonSet that runs a Prometheus container. Prometheus is an open source toolkit to monitor and alert. It is widely used for Kubernetes monitoring.

  • Lets Create the template
apiVersion: extensions/v1beta1
kind: DaemonSet
metadata:
  name: prometheus-daemonset
spec:
  selector:
    matchLabels:
      tier: monitoring
      name: prometheus-exporter
  template:
    metadata:
      labels:
        tier: monitoring
        name: prometheus-exporter
    spec:
      containers:
      - name: prometheus
        image: prom/node-exporter
        ports:
        - containerPort: 80
  • Run the following command to create the ReplicaSet and pods:
$ kubectl create -f daemonset.yaml --record

N.B : The –record flag will track changes made through each revision.

  • Lets see the status of the daemonset
$ kubectl get daemonsets/prometheus-daemonset
  • Get more details about the DaemonSet:
$ kubectl describe daemonset/prometheus-daemonset
  • Lets see the pod status
$ kubectl get pods -lname=prometheus-exporter
  • Limit DaemonSets to specific nodes

First verify that the Prometheus pod was successfully deployed to the cluster nodes:

$ kubectl get pods -o wide
  • Rename one of the node labels as follows:
$ kubectl label node NODE_NAME app=prometheus-node
  • Next, edit the DaemonSet template using the command shown:
$ kubectl edit ds/prometheus-daemonset

Change the spec.template.spec to include a nodeSelector that matches the changed label:

      nodeSelector:
        app: prometheus-node
  • After the update is performed, we have now configured Prometheus to run on a specific node:
$ kubectl get ds/prometheus-daemonset

Run the following command to delete the DaemonSet:

$ kubectl delete -f daemonset.yaml