Kubernetes DaemonSet: What It Is and 5 Ways to Use It


Kubernetes DaemonSet is a powerful tool for managing persistent workloads on your system. It ensures that each instance of an application running on your system will always be running. When starting a Kubernetes cluster, you must configure the services or applications you want running in your cluster.
In this post, we'll explain the problem it solves and five ways to use it.
A DaemonSet ensures that a specified collection of pods runs on the specified nodes. DaemonSet makes sure one pod exists per node.
Kubernetes DaemonSets can be used for various applications, including key-value stores, caches, and servers that require high availability, like messaging apps. A DaemonSet will allow you to specify how many instances your app should run in the cluster and guarantee a consistent state among all pods running in the cluster.
Kubernetes DaemonSet can run Docker containers without managing the underlying infrastructure. You don't have to worry about scaling your clusters or where the container data is stored.

DaemonSet manages the number of pod copies to run in a node. However, a deployment manages the number of pods and where they should be on nodes. Deployment selects nodes to place replicas using labels and other functions (e.g., tolerations).
A DaemonSet doesn't need external resources like IP addresses or port numbers. However, you must provide them if you create a deployment.
Additionally, a DaemonSet doesn't need to know the number of nodes in your Kubernetes cluster. However, you must provide it if you create a deployment.
Last, a DaemonSet doesn't need runtime state information, like the number of pods currently running on each node or the number of replicas running on each pod (although these things can still be specified).
First, we need to create a Manifest file which will contain all of the necessary configuration information for our DaemonSet.
apiVersion: extensions/v1beta1
kind: DaemonSet
metadata:
name: nginx-example
spec: template: metadata : labels : app : nginx spec : nodeSelector : role : web containers : - name : nginx image : nginx ports : - containerPort : 80
Specifying the node selector allows you to control which nodes the pod will run on. You could spread your pods across multiple nodes for load balancing, high availability, and more. You can use the labels for pod selection and service discovery.
In this example, we created a DaemonSet with all nodes in the cluster.
From this example, we can use multiple containers in a DaemonSet. You could use CoreDNS (a DNS server) to spin up on every node in your cluster, or even an image library for processing images on every node for faster processing of your containers.
The “spec” section tells Kubernetes what the pod is supposed to do; the “node selector” is how you choose which machines the pod is running on.

If you add a node to your cluster, then you can create a new DaemonSet and add it to the node. If you want to ensure the required number of pods run on a subset of nodes, you can create multiple DaemonSets. Once created, they need to be managed by an operator.
Here, these steps explain how to use Kubernetes to manage your cluster's kubelet agent (Sysdig) and how it compares with creating a deployment.
curl -s create-daemonset.sh -H "Content-Type: application/json" -d '{"kind":"DaemonSet", "apiVersion":"v1", "metadata": {"name": "sysdig"}}' | kubectl
--kubeconfig=/etc/kubernetes/admin.kubeconfig apply -f-
$ kubectl --kubeconfig=/etc/kubernetes/admin.kubeconfig get svc/sysdig-k8s-agent
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
sysdig-k8s-agent ClusterIP 10.157.194.75 <none> 80:31185/TCP,443:31186/TCP 1m
curl -s http://10.157.194.75:31186/api/v1/namespaces/system:pod/pods | jq '.status.containerStatus'
"active" "running" "stopped-for-preview"
kubectl create -f daemonset.yaml --namespace=system/daemonset

DaemonSet is a collection of log files available to your application. You use it for collecting performance, usage, and error logs.
DaemonSet contains a few components—a daemon, a set of clients (logs and other activity watchers), and a central log handler. It all comes together through the standard protocol described below, allowing seamless integration with your application's ongoing activity.
DaemonSet collects logs, errors, and other system statistics and doesn't interfere with your application's behavior.
The handler receives logs from clients and does the work of processing and sending them to the desired destination. You can have multiple handlers in a single DaemonSet.
DaemonSet uses a flexible messaging format over TCP connection (by default, on port 9000) that allows for sending various messages to the handler(s).

Kubernetes DaemonSet is a great way to manage and deploy applications in a clustered environment. It's easy to use and has a wide range of features, making it an ideal choice for managing applications in a production environment.
You can use DaemonSet to run a cluster storage, log collection, and node monitoring demon on each node. Now that you know how to spin up a DaemonSet, check out Loft, a platform that gives you autonomy to fully leverage DaemonSet's capabilities.
This post was written by Mercy Kibet. Mercy is a full-stack developer with a knack for learning and writing about new and intriguing tech stacks.
Deploy your first virtual cluster today.