Kubernetes Essentials: Understanding DaemonSets, CronJobs, and Jobs

SHRIRAM SAHUSHRIRAM SAHU
6 min read

Introduction

In today's blog, we explore essential components of Kubernetes: DaemonSets, CronJobs, and Jobs. These elements are vital for efficiently managing workloads within a Kubernetes cluster, and understanding their distinctions can significantly elevate your Kubernetes expertise. We will delve into what these components are, why they are used, and how to implement them through practical examples.

DaemonSet in Kubernetes?

A DaemonSet is a Kubernetes object that ensures a copy of a specific pod runs on every node (or specific nodes) in the cluster. DaemonSets are particularly useful for deploying cluster-wide services like logging agents, monitoring tools, or storage daemons that need to run on every node.

  • A daemon set is another type of Kubernetes object that controls pods. Unlike deployment, the DS automatically deploys 1 pod to each available node. You don't need to update the replica based on demand; the DS takes care of it by spinning X number of pods for X number of nodes.

  • If you create a ds in a cluster of 5 nodes, then 5 pods will be created.

  • If you add another node to the cluster, a new pod will be automatically created on the new node.

Examples of DaemonSet

  • kube-proxy

  • calico

  • weave-net

  • monitoring agents etc

For example, if you need a log collector to run on each node to gather logs from different applications, you would deploy it as a DaemonSet.

Why Do We Use DaemonSets in Kubernetes?

DaemonSets are used to:

  • Ensure a pod is running on every node, which is ideal for monitoring, logging, or networking tasks.

  • Automate the deployment of a specific service across the entire cluster without manually scheduling pods on each node.

  • Automatically handle the addition and removal of nodes from the cluster by ensuring that the necessary pods are created or removed as nodes are added or deleted.

DaemonSet vs Deployment: What’s the Difference?

While both DaemonSets and Deployments are used to manage pods, they serve different purposes:

  • DaemonSet: Ensures that a pod runs on all (or specific) nodes. It’s perfect for node-specific tasks like log collection or monitoring.

  • Deployment: Manages a replicated application, scaling the number of replicas based on the desired state. It’s ideal for stateless applications where multiple instances can run across different nodes.

The key difference lies in their use cases: DaemonSets are for per-node tasks, while Deployments are for application scalability and availability.

What is a CronJob in Kubernetes?

A CronJob in Kubernetes is used to run jobs on a scheduled basis, similar to cron jobs in Linux. It is ideal for tasks that need to be executed at regular intervals, like database backups, sending emails, or generating reports.

What is a Job in Kubernetes?

A Job in Kubernetes is responsible for running a specified number of instances of a pod to completion. Unlike Deployments, which ensure that a specific number of pods are always running, Jobs are meant to perform a specific task and then exit.

Jobs are useful for one-time tasks like data processing, sending out emails, or performing batch operations.

apiVersion: batch/v1
kind: Job
metadata:
  name: batch-job
spec:
  template:
    spec:
      containers:
      - name: job
        image: my-batch-job-image
        args:
        - /bin/sh
        - -c
        - /usr/local/bin/process-data.sh
      restartPolicy: OnFailure

This YAML file defines a Job

Task: Create a DaemonSet in Kubernetes

In this task, you'll learn how to create a DaemonSet in Kubernetes. DaemonSets ensure that a specific pod runs on every node in the cluster, making them ideal for tasks such as logging, monitoring, and networking.

Steps to Create a DaemonSet:

  1. Create a YAML Configuration File:

    First, define the DaemonSet in a YAML file. Here is the YAML configuration I used for this task:

     apiVersion: apps/v1
     kind: DaemonSet
     metadata:
       name: nginx-ds
       labels:
         env: demo
     spec:
       template:
         metadata:
           labels:
             env: demo
           name: nginx
         spec:
           containers:
           - image: nginx
             name: nginx
             ports:
             - containerPort: 80
       selector:
         matchLabels:
           env: demo
    
  2. Apply the YAML Configuration:

    Use the kubectl apply command to create the DaemonSet:

     kubectl apply -f logds.yaml
    

    This command will create the DaemonSet based on the configuration in the logds.yaml file.

  3. Verify the DaemonSet:

    After applying the configuration, verify that the DaemonSet has been created and that pods are running on each node:

     kubectl get ds
    

    You can also check the status of the pods created by the DaemonSet:

     kubectl get pods
    

    "I have only one worker node, which is why only one pod is created and scheduled on that node."

  4. Check Logs and Events:

    To ensure that the DaemonSet is running correctly, you can check the logs of the pods:

     kubectl logs -l env=demo
    

    If there are issues, you can describe the DaemonSet to see detailed events and errors:

     kubectl describe ds nginx-ds
    


Task: Create a CronJob in Kubernetes?

To create a CronJob in Kubernetes that prints "40daysofkubernetes" every 5 minutes using the BusyBox image, you can use the following YAML configuration:

apiVersion: batch/v1
kind: CronJob
metadata:
  name: print-message-cronjob
  labels:
    env: demo
spec:
  schedule: "*/5 * * * *"  # Runs every 5 minutes
  jobTemplate:
    spec:
      template:
        metadata:
          labels:
            env: demo
        spec:
          containers:
          - name: print-message
            image: busybox
            command: ['sh', '-c', 'echo 40daysofkubernetes']
          restartPolicy: OnFailure

To create the CronJob in your Kubernetes cluster, save the YAML configuration to a file, for example, cronjob.yaml, and apply it using:

kubectl apply -f cronjob.yaml

You can check the status of the CronJob and its jobs using:

kubectl get cronjobs
kubectl get jobs
kubectl get pods

This setup will ensure that "40daysofkubernetes" is printed every 5 minutes.

Check the Logs of the Pod

Once you have the Pod name, you can check the logs to see the output of the command:

kubectl logs <pod-name>

This command displays the output from the container, including the message "40daysofkubernetes" if the CronJob is running correctly.

Each Job creates Pods.

Check Job Events:

Describe the Jobs created by the CronJob to get details about their execution:

kubectl describe job <job-name>

This command shows events and errors related to the Job.


Conclusion

Understanding DaemonSets, CronJobs, and Jobs is fundamental for anyone working with Kubernetes. DaemonSets ensure that crucial services are deployed across all nodes in a cluster, CronJobs automate tasks at scheduled intervals, and Jobs handle one-time tasks efficiently. By mastering these components, you can optimize your Kubernetes environment for better resource management and automation. As you continue to explore Kubernetes, these concepts will be invaluable in building and maintaining robust, scalable applications.


References

11
Subscribe to my newsletter

Read articles from SHRIRAM SAHU directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

SHRIRAM SAHU
SHRIRAM SAHU