Daemonsets, Job & Cronjob in — Kubernetes

Always learning
3 min readJul 30, 2024

--

What is Daemonset?

A DaemonSet is a Kubernetes resource that ensures that a specific pod runs on every node in the cluster. It’s commonly used to deploy system daemons (or) cluster-wide services that should be available on every node.

Key characteristics of a DaemonSet:

  1. One Pod Per Node
  2. Automated Deployment and Scaling
  3. Node Selector and Affinity
  4. Update Strategies

A basic example of a DaemonSet manifest:

Let's create a Daemon.yml file and save it in our file system.

apiVersion: apps/v1
kind: DaemonSet
metadata:
name: demo-ds
labels:
name: demo-ds
spec:
template:
metadata:
labels:
name: demo-ds
spec:
containers:
- name: nginx-ds
image: nginx
selector:
matchLabels:
name: demo-ds

Go to the directory where the file is located and run the below command.

kubectl apply -f Daemon.yml

This will create a DaemonSet, and you will see one pod created per node.

To view all DaemonSets in the cluster

kubectl get ds -A

To view DaemonSets in a specific namespace:

kubectl get ds -n <namespace-name>
kubectl get ds -n default

Cronjob and Jobs

  • Job → Runs immediately and performs a one-time task without scheduling.
  • CronJob → Runs Jobs on a schedule, ideal for recurring tasks.

Jobs

A Job in Kubernetes is a resource to create one (or) more pods that run a specific task and ensure completion.

Once a Job is completed successfully, all its associated pods terminate. Jobs are typically used for batch processing, data migration, (or) other tasks that need to run to completion.

Create a job.yml file

apiVersion: batch/v1
kind: Job
metadata:
name: example-job
spec:
template:
spec:
containers:
- name: task-container
image: busybox
command: ["echo", "Hello from the job"]
restartPolicy: Never
backoffLimit: 4

Go to the directory where the file is located and run the below command.

kubectl apply -f job.yml

To view jobs in the cluster

kubectl get jobs -A
kubectl get jobs -n default

CronJobs

A cron job is a Linux command for scheduling specific tasks. It’s useful for tasks that need to be run periodically, such as backups, data synchronization, (or) regular maintenance tasks.

Refer to the link https://crontab.guru/

A basic example of a CronJob manifest:

Create a cronjob.yml file

apiVersion: batch/v1
kind: CronJob
metadata:
name: demo-cronjob
spec:
schedule: "*/1 * * * *" # This schedule means the job will run every 1 minutes.
jobTemplate:
spec:
template:
metadata:
labels:
job: demo-cronjob
spec:
containers:
- name: print-message
image: busybox
command: ["sh", "-c", "echo '40daysofkubernetes'"]
restartPolicy: OnFailure

Go to the directory where the file is located and run the below command.

kubectl apply -f cronjob.yml

List of the pods

kubectl get pods

Delete the CronJob

kubectl delete cronjob/<cronjob-name>
kubectl delete cronjob/demo-cronjob

Thank you 🙏 for taking the time to read our blog.

--

--

Always learning

கற்றுக் கொள்ளும் மாணவன்...