What is a DaemonSet

Create a daemon set in Kubernetes

A DaemonSet ensures that a copy of a pod is running on all nodes in the Kubernetes cluster. Each node has a copy of the pod. When nodes are added to the cluster, pods are added to them, and when nodes are removed from the cluster, the pods are removed. When we delete a DaemonSet, the Pods it created are cleaned up.

A DaemonSet ensures that all nodes are running a copy of a Pod. Usually the node on which a pod runs is selected by the planner, but DaemonSet pods are created and scheduled by the DaemonSet controller.

Daemon set can be used:

  1. To run cluster storage on each node, such as: glusterd, ceph
  2. How to run the log collection on each node, e.g. E.g .: fluentd, logstash
  3. How to run node monitoring on every note; B: Prometheus Node Exporter, Collective, Datadog Agent

To learn more about Daemonset, visit kubernetes.io, the official Kubernetes documentation.

In this article we will create a daemon set of "fluentd_elasticsearch". This will create fluentd_elasticsearch pods on each node in the cluster. Our daemon set definition file will contain the Toleration for Taint of the master node so that the pod can also be scheduled on the master node.

Requirements

  1. Kubernetes cluster with at least 1 worker node.
    If you want to learn how to create a Kubernetes cluster, click here. This guide will help you create a Kubernetes cluster with 1 master and 2 worker nodes on AWS Ubuntu 18.04 EC2 instances.

What we gonna do?

  1. Create a daemon set

Create a daemon set

Check whether a daemon set exists in the standard namespace and in all namespaces.

kubectl get daemonsets #Get daemonsets from the default namespace kubectl get daemonsets --all-namespaces #Get daemonsets from all namespace using --all-namespace option

In the screenshot above, you can see that a few daemon sets are available. All of these daemon sets are for cluster components.

Now get pods that belong to the namespace "kube-system".

kubectl get pods -n kube-system #Get pods from the "kube-system" namespace

All of these pods, which can be seen in the screenshot above, belong to the daemon set of the cluster components.

Obtain a list of proxy pods.

kubectl get pods -n kube-system | grep proxy # Get pods from the "kube-system" namespace and grep for proxy

Check what is controlling proxy pods.

kubectl describe pod kube-proxy-s5vzp -n kube-system #Describe the pod from the "kube-system" namespace

Get details of the daemon set that controls the proxy pods.

kubectl describe daemonset kube-proxy -n kube-system #Describe the daemonset from the "kube-system" namespace

Create a file with the following daemon set definition in it.

vim my-daemonset.ymlapiVersion: apps / v1 kind: DaemonSet metadata: name: my-fluentd-elasticsearch-daemonset namespace: kube-system labels: k8s-app: fluentd-logging spec: selector: matchLabels: name: fluentd-elasticsearch template : metadata: labels: name: fluentd-elasticsearch spec: tolerations: - key: node-role.kubernetes.io/master effect: NoSchedule containers: - name: fluentd-elasticsearch image: quay.io/fluentd_elasticsearch/fluentd:v2.5.2 resources: limits: memory: 200Mi requests: cpu: 100m memory: 200Mi volumeMounts: - name: varlog mountPath: / var / log - name: varlibdockercontainers mountPath: / var / lib / docker / containers readOnly: true terminationGracePeriodSeconds: 30 volumes: - name: varlog hostPath: path: / var / log - name: varlibdockerconta iners hostPath: path: / var / lib / docker / containers

In the definition above we have a toleration to the taint of the master node. This will also place the pod on the master node.

Create a daemon set using the definition file created in the step above.

kubectl create -f my-daemonset.yml #Create a daemonset kubectl get daemonset -n kube-system # Get daemonset from the "kube-system" namespace

This daemon set was created in the namespace "kube-system".

Describe the daemon set we just created in the kube-system namespace.

kubectl describe daemonset my-fluentd-elasticsearch-daemonset -n kube-system #Describe the daemonset from the "kube-system" namespace

The screenshot above shows that the pods have been deployed on 2 nodes.

Now we can get details about the pods that were deployed as daemon sets on 2 nodes.

kubectl get pods -n kube-system | grep my-fluentd-elasticsearch-daemonset #Get pods from the "kube-system" namespace and grepkubectl describe pod my-fluentd-elasticsearch-daemonset-4t9vs -n kube-system | grep Node #Describe the pods from the "kube-system" namespace and grepkubectl describe pod my-fluentd-elasticsearch-daemonset-kxfjj -n kube-system | grep Node # Describe the pod from the "kube-system" namespace and grep

In the above figure you can see that the pods were deployed on the worker node "node01" and on the master node "master". The reason the pods were deployed on the master node is because of tolerance to the load on the master node.

conclusion

In this article, we saw the steps to create a daemon set and saw how the pods in the daemon set are deployed on each node in the Kubernetes cluster.