Deploying System-wide Tools Using DaemonSets in Kubernetes

Updated: January 31, 2024 By: Guest Contributor Post a comment

Introduction

Kubernetes has become the de facto standard for deploying containerized applications at scale. While often used for deploying specific apps or services, it also provides powerful tools for managing system-level operations across your Kubernetes nodes. One such tool is the DaemonSet.

A DaemonSet ensures that some or all of your nodes run a copy of a pod. As nodes are added to the cluster, pods are automatically added to them. As nodes are removed from the cluster, those pods are garbage collected. Creating a DaemonSet allows developers to manage tasks such as logging, monitoring, and system maintenance at scale. In this tutorial, we’ll look at deploying system-wide tools using DaemonSets in Kubernetes.

Prerequisites

  1. A Kubernetes cluster
  2. Kubectl installed and configured
  3. Basic understanding of Kubernetes pods and nodes

Understanding DaemonSets

When would you choose to use DaemonSets instead of other resources such as Deployments or StatefulSets? Deployments are the right choice when you want to run multiple instances of a single application. StatefulSets are used when you require stable unique network identifiers, stable persistent storage, and ordered deployment and scaling. However, when you need to run a copy of a pod on each node of the cluster or specific nodes in a cluster continually, you should use DaemonSets.

Creating a DaemonSet

First, you need to define your DaemonSet manifest. Below is a simple example:

apiVersion: apps/v1
kind: DaemonSet
metadata: 
  name: example-daemonset
  labels: 
    app: example
spec: 
  selector: 
    matchLabels: 
      app: example
  template: 
    metadata: 
      labels: 
        app: example
    spec: 
      containers: 
        - name: mycontainer
          image: myimage:latest

Here’s a step by step explanation of the key sections in this DaemonSet manifest:

  • apiVersion: Specifies the API version, in this case, ‘apps/v1’.
  • kind: Specifies that this is a DaemonSet.
  • metadata: Defines the name and labels for the DaemonSet.
  • spec.selector: Specifies how the DaemonSet should identify the pods it manages, using labels.
  • spec.template: Describes the pod that will be created. The pod’s metadata and spec should match the selector’s criteria.
  • spec.template.spec.containers: Lists the containers the pod will run, along with the used images.

To deploy this DaemonSet to your cluster, save the manifest to a file, say ‘example-daemonset.yml’, and apply it using kubectl: <code>kubectl apply -f example-daemonset.yml</code>

Specifying Nodes for DaemonSets

Sometimes, you might want to limit the nodes where the DaemonSet pods are deployed. This can be done using node labels and a node selector in the manifest:

spec:
  selector:
    matchLabels:
      app: example
  template:
    metadata:
      labels:
        app: example
    spec:
      containers:
        - name: mycontainer
          image: myimage:latest
      nodeSelector:
        kubernetes.io/hostname: target-node

This will only schedule the DaemonSet pods on a node named ‘target-node’. You can also use other labels that might be more appropriate for your scheduling needs.

Updating DaemonSets

If there is a need to update the image or configuration used by the DaemonSet’s pods, simply update the manifest and re-apply it with kubectl. Kubernetes will ensure the update rolls out across all the relevant nodes.

kubectl apply -f example-daemonset.yml

You may also perform a rolling update using kubectl: <code>kubectl rollout restart daemonset/example-daemonset</code>

This will restart the pods with the new configuration in a rolling fashion to avoid downtime.

Monitoring and Maintaining DaemonSets

Monitoring your DaemonSets is crucial for ensuring they are running correctly.

kubectl get daemonsets

If you need to delete a DaemonSet, use:

kubectl delete daemonset example-daemonset

Through proper use and monitoring of DaemonSets, you can automate the deployment of system-wide services, ensuring a consistent environment across all nodes in your Kubernetes cluster.

Conclusion

In this tutorial, we examined Kubernetes DaemonSets and showed how they are essential for tasks that have to be performed on each node, such as logging and monitoring. By following the steps outlined in this guide, you will now be able to deploy, manage, and scale system-wide tools efficiently in your Kubernetes cluster.