DEV Community

Cover image for Mastering Kubernetes: Understanding Cron Jobs and DaemonSets
Jensen Jose
Jensen Jose

Posted on

Mastering Kubernetes: Understanding Cron Jobs and DaemonSets

Welcome back to our Kubernetes Mastery series! This is the 12th installment in the CK 2024 series and today we'll dive into CronJobs and DaemonSets in Kubernetes. Let's get started!

The Importance of DaemonSets

In our previous posts, we explored ReplicaSets and Deployments, detailing how they deploy pods across multiple nodes. When we specify a replicas field in the manifest, Kubernetes creates multiple pods distributed across nodes. For example, if we set replicas: 3, Kubernetes might create three NGINX pods, each on different nodes.

DaemonSets function similarly but with a key difference: they ensure that a copy of a pod runs on all nodes in the cluster. If a new node is added to the cluster, the DaemonSet controller automatically adds a pod to it. Conversely, if a node is removed, the corresponding pod is also deleted. This behavior is useful for tasks such as running monitoring or logging agents on every node.

Use Cases for DaemonSets:

  1. Monitoring Agents: Collect metrics from each node.
  2. Logging Agents: Stream logs from each node to a central system.
  3. Network Plugins: Components like kube-proxy, Weave, Flannel, or Calico often run as DaemonSets.

Creating a DaemonSet:

Here’s a simplified YAML manifest for a DaemonSet:

apiVersion: apps/v1
kind: DaemonSet
metadata:
  name: sample-agent
spec:
  selector:
    matchLabels:
      name: sample-agent
  template:
    metadata:
      labels:
        name: sample-agent
    spec:
      containers:
      - name: sample-agent
        image: nginx
Enter fullscreen mode Exit fullscreen mode

This manifest ensures that an NGINX pod runs on all nodes in the cluster.

To apply this configuration:

kubectl apply -f daemonset.yaml
kubectl get pods
Enter fullscreen mode Exit fullscreen mode

You’ll notice that a pod is created on each node except the master node due to taints.

CronJobs in Kubernetes

CronJobs are a type of job that runs on a scheduled basis, similar to cron jobs in Unix/Linux systems. They are not directly relevant to the CKA exam but are essential for automating repetitive tasks.

Image description

CronJob Syntax:

The cron syntax includes five fields:

  • Minute (0-59)
  • Hour (0-23)
  • Day of Month (1-31)
  • Month (1-12)
  • Day of Week (0-6) starting with Sunday

Examples:

Run at 11:00 PM every Saturday: 0 23 * * 6
Run every 5 minutes: */5 * * * *
Creating a CronJob:

Here’s a simple example to print the date and a message every minute:

apiVersion: batch/v1beta1
kind: CronJob
metadata:
  name: hello-cron
spec:
  schedule: "* * * * *"
  jobTemplate:
    spec:
      template:
        spec:
          containers:
          - name: hello
            image: busybox
            args:
            - /bin/sh
            - -c
            - date; echo Hello from the Kubernetes cluster
          restartPolicy: OnFailure
Enter fullscreen mode Exit fullscreen mode

To apply this configuration:

kubectl apply -f cronjob.yaml
kubectl get cronjobs
Enter fullscreen mode Exit fullscreen mode

This CronJob runs every minute, prints the date, and outputs a hello message.

Understanding Jobs

Jobs in Kubernetes run a pod to completion. Unlike CronJobs, they are typically used for one-time tasks like data processing or batch operations.

Here’s a simple Job manifest:

apiVersion: batch/v1
kind: Job
metadata:
  name: simple-job
spec:
  template:
    spec:
      containers:
      - name: pi
        image: perl
        command: ["perl",  "-Mbignum=bpi", "-wle", "print bpi(2000)"]
      restartPolicy: Never
Enter fullscreen mode Exit fullscreen mode

To apply this configuration:

kubectl apply -f job.yaml
kubectl get jobs
Enter fullscreen mode Exit fullscreen mode

This job calculates pi to 2000 decimal places and completes.

Conclusion:

In this post, we covered the fundamentals of DaemonSets, CronJobs, and Jobs in Kubernetes. DaemonSets ensure that critical pods run on every node, while CronJobs automate tasks on a schedule, and Jobs execute one-time tasks to completion. Understanding these components is crucial for managing Kubernetes clusters effectively.

Stay tuned for our next post where we’ll dive deeper into Kubernetes networking components and their deployment strategies.

For further reference, check out the detailed YouTube video here:

Top comments (1)

Collapse
 
der_gopher profile image
Alex Pliutau

Great write-up, we also have a bunch of articles on Kubernetes, check it out here - packagemain.tech