Run DaemonSets, Jobs and CronJobs
In this task you'll choose the right workload type for agents, one-off tasks and scheduled tasks.
A Deployment keeps a number of identical pods running. A DaemonSet runs exactly one pod on every node (or every matching node): log collectors, monitoring agents and network plugins. A Job runs pods until a task finishes successfully, with retries (backoffLimit) and optional parallelism. A CronJob creates a Job on a schedule.
CronJobs need three settings thought through: concurrencyPolicy (Forbid stops a slow run overlapping the next one), startingDeadlineSeconds (skip a run that's too late) and history limits. A Job that keeps failing stops after backoffLimit tries; its pods stay so you can read their logs.
Example. Kestwick Hotels exports the day's bookings to finance at 02:00 with a CronJob. After one night when two slow exports overlapped and sent duplicates, they set concurrencyPolicy: Forbid and a 30-minute activeDeadlineSeconds on the Job.
Before you start
You need: Docker Desktop, kind and kubectl on your computer (all free), and a cluster from Run a practice cluster on your computer. Time: 30 minutes.
Where to look
- Terminal
- kubectl
Steps
- Create a DaemonSet running busybox that prints the node name, and check there's one pod per worker with
kubectl get pods -o wide. - Create a Job that runs
sh -c 'sleep 5; echo done'with completions 3 and parallelism 2; watch it finish. - Create a Job that always fails (
exit 1) with backoffLimit 2, and read its status and pods.It stops after the retries. - Create a CronJob that runs every minute and sleeps 90 seconds, first with concurrencyPolicy Allow, then Forbid, and compare how many Jobs run at once.
- Suspend the CronJob with
kubectl patch cronjob report -p '{"spec":{"suspend":true}}'.
Commands
kubectl create job once --image=busybox:1.36 -- sh -c 'sleep 5; echo done'
kubectl create cronjob report --image=busybox:1.36 --schedule='*/1 * * * *' -- sh -c 'sleep 90'
kubectl patch cronjob report -p '{"spec":{"concurrencyPolicy":"Forbid"}}'
kubectl get jobs --watch
Watch out for
- Cost
- Free.
- Security
- DaemonSets often need host access (hostPath, host network); grant those only to trusted system agents.
- Common mistakes
- Using a Deployment for a task that should finish, forgetting concurrencyPolicy on long CronJobs, and leaving thousands of finished Jobs (set ttlSecondsAfterFinished).
Clean up
Delete the DaemonSet, Jobs and CronJob.
Words to know
- DaemonSet
- Runs one copy of a pod on each node.
- Job
- Runs pods until a task completes, retrying on failure.
- CronJob
- Creates Jobs on a schedule written in cron format.
- concurrencyPolicy
- Whether a CronJob may start a run while the last one is still going.
Tick off each step and track your progress in the guide.
Open this task in DevOps, Hands On