Newsletter
TechAnV Blog
Get updates on security engineering, Rust, eBPF, and DevSecOps. No spam, unsubscribe anytime.
Check your inbox and click the confirmation link to complete your subscription.
Kubernetes offers a rich set of resources to manage different types of workloads. In this post, we’ll dive deep into two powerful Kubernetes objects: DaemonSets and CronJobs. We’ll explore their use cases, implementation, and provide hands-on examples to solidify your understanding.
Understanding DaemonSets#
A DaemonSet is a Kubernetes object that ensures that all (or some) nodes run a copy of a pod. As nodes are added to the cluster, pods are added to them. As nodes are removed from the cluster, those pods are garbage collected.
Key Features of DaemonSets:#
- Runs one pod per node automatically
- Ideal for cluster-wide services
- Automatically scales with cluster size
Common Use Cases:#
- Cluster storage daemons (e.g.,
glusterd,ceph) - Log collection daemons (e.g.,
fluentd,logstash) - Node monitoring daemons (e.g.,
Prometheus Node Exporter)
Let’s create a DaemonSet that runs an Nginx server on each node:
1apiVersion: apps/v12kind: DaemonSet3metadata:4 name: nginx-ds5 labels:6 env: demo7spec:8 selector:9 matchLabels:10 env: demo11 template:12 metadata:13 labels:14 env: demo15 name: nginx16 spec:17 containers:18 - name: nginx19 image: nginx20 ports:21 - containerPort: 80To apply this DaemonSet:
1kubectl apply -f nginx-daemonset.yamlVerify the DaemonSet:
1kubectl get daemonset2kubectl get pods -o wideYou should see one Nginx pod running on each node in your cluster.
Understanding CronJobs#
CronJobs are useful for creating periodic and recurring tasks, like backups, report generation, or sending emails.
Key Features of CronJobs:#
- Runs jobs on a time-based schedule
- Uses cron format for scheduling
- Ideal for automated tasks
Let’s create a CronJob that prints “40daysofkubernetes” every 5 minutes:
1apiVersion: batch/v12kind: CronJob3metadata:4 name: print-message5spec:6 schedule: "*/5 * * * *"7 jobTemplate:8 spec:9 template:10 spec:11 containers:12 - name: print-message13 image: busybox14 args:15 - /bin/sh16 - -c17 - echo "40daysofkubernetes"18 restartPolicy: OnFailureTo apply this CronJob:
1kubectl apply -f print-message-cronjob.yamlVerify the CronJob:
1kubectl get cronjobs2kubectl get jobs --watchYou should see a new job created every 5 minutes.
Understanding Cron Syntax#
The cron syntax used in Kubernetes CronJobs follows the standard Unix cron format:
1┌───────────── minute (0 - 59)2│ ┌───────────── hour (0 - 23)3│ │ ┌───────────── day of the month (1 - 31)4│ │ │ ┌───────────── month (1 - 12)5│ │ │ │ ┌───────────── day of the week (0 - 6) (Sunday to Saturday)6│ │ │ │ │7* * * * *In our example, */5 * * * * means “every 5 minutes”.
Conclusion#
DaemonSets and CronJobs are powerful tools in the Kubernetes ecosystem. DaemonSets ensure that specific pods run on every node, making them perfect for cluster-wide operations. CronJobs, on the other hand, allow you to schedule recurring tasks with ease.
By mastering these resources, you can create more sophisticated and automated Kubernetes deployments. Remember to always consider the specific needs of your application when choosing between different Kubernetes objects.
As you continue your Kubernetes journey, experiment with different configurations and use cases for DaemonSets and CronJobs. Happy Kuberneting!