Sling Academy
Home/DevOps/Deploying System-wide Tools Using DaemonSets in Kubernetes

Deploying System-wide Tools Using DaemonSets in Kubernetes

Last updated: January 31, 2024

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.

Next Article: Managing Stateful Applications with StatefulSets in Kubernetes

Previous Article: Understanding Storage Classes and Dynamic Provisioning in Kubernetes (4 Examples)

Series: Kubernetes Tutorials

DevOps

You May Also Like

  • How to reset Ubuntu to factory settings (4 approaches)
  • Making GET requests with cURL: A practical guide (with examples)
  • Git: What is .DS_Store and should you ignore it?
  • NGINX underscores_in_headers: Explained with examples
  • How to use Jenkins CI with private GitHub repositories
  • Terraform: Understanding State and State Files (with Examples)
  • SHA1, SHA256, and SHA512 in Terraform: A Practical Guide
  • CSRF Protection in Jenkins: An In-depth Guide (with examples)
  • Terraform: How to Merge 2 Maps
  • Terraform: How to extract filename/extension from a path
  • JSON encoding/decoding in Terraform: Explained with examples
  • Sorting Lists in Terraform: A Practical Guide
  • Terraform: How to trigger a Lambda function on resource creation
  • How to use Terraform templates
  • Understanding terraform_remote_state data source: Explained with examples
  • Jenkins Authorization: A Practical Guide (with examples)
  • Solving Jenkins Pipeline NotSerializableException: groovy.json.internal.LazyMap
  • Understanding Artifacts in Jenkins: A Practical Guide (with examples)
  • Using Jenkins with AWS EC2 and S3: A Practical Guide