DaemonSets, Jobs, and CronJobs
Choose between a system agent on each node, a one-off task, and a scheduled batch task.
What you will understand
- Explain how a DaemonSet responds when an eligible node joins the cluster.
- Read Job completion, retry, and deadline behavior.
- Explain how CronJob timing and concurrency affect the application code.
Builds on: StatefulSets and stable identity
Match the controller to the work
A cluster needs a log collector on every worker, a report generated once today, and a fresh report every morning. Each is a workload, but success means something different for each.
A DaemonSet aims to run a Pod on every eligible node, or on a subset selected by labels and other constraints. A log collector often needs one copy per node because it reads that node’s logs. When a new eligible node joins, the DaemonSet controller creates a Pod for it. Use the node coverage scene to see why adding a node can create a new Pod even though no replica number was edited.
Why does adding a worker add a collector Pod?
A DaemonSet’s target is the set of eligible nodes, so a newly eligible worker gets an instance without a requested replica count change.
Find the eligible nodes
- DaemonSet logsWorker Aeligible
- DaemonSet logsControl nodeselector mismatch
The controller applies node selection and placement rules; the control node is excluded in this example.
Run one collector on each
- DaemonSet logsAgent Acreate
- Agent AWorker Aassigned here
Agent A is assigned to Worker A. Its purpose is coverage of that node rather than satisfying a generic replica count.
Add a new eligible worker
- DaemonSet logsWorker Bnew eligible node
Worker B joins with matching labels and acceptable taints, creating another node that needs coverage.
Start its collector
- DaemonSet logsAgent Bcreate
- Agent BWorker Bassigned here
Agent B runs on Worker B and consumes that worker resources. Requests and permissions still matter for infrastructure agents.
One agent Pod per eligible worker in steady state; update surge and transient overlap are omitted.
Explore another case
Run one report to completion
A Job runs a task to completion. It creates Pods, tracks their successful completions, and can retry failures within its policy. The following Job runs a command, writes a timestamp and marker, then exits successfully. restartPolicy: Never lets you see separate Pods for retry attempts. backoffLimit bounds retries, activeDeadlineSeconds bounds the Job’s running time, and the TTL cleans up finished work after 600 seconds. For a real report, store the output where it will still be available after the Pod is gone.
apiVersion: batch/v1
kind: Job
metadata:
name: report-once
spec:
backoffLimit: 2 # Bound retry attempts after failures.
activeDeadlineSeconds: 120 # Bound total running time.
ttlSecondsAfterFinished: 600 # Clean up later; export needed evidence first.
template:
spec:
restartPolicy: Never
containers:
- name: report
image: busybox:1.36
command: ["sh", "-c", "date -u; echo report-complete"]Follow a scheduled task through retries
A CronJob creates Jobs on its schedule. A Job tracks completion and may retry a failed attempt until its policy stops it.
CronJob triggers a run
- CronJobJob runscheduled run
The CronJob evaluates its schedule and concurrency policy, then creates a Job for an eligible run.
Job starts the first attempt
- Job runAttempt 1attempt
- Attempt 1Result storewrite result
The Job starts a Pod. It may produce an external side effect before its final exit status is known.
A failed attempt can be retried
- Job runAttempt 2retry
- Attempt 1Job runfailed
A failure within backoffLimit can create another attempt. The controller does not know whether the previous external write was committed.
Successful completion records the result
- Attempt 2Result storesame work key
- Attempt 2Job runsucceeded
The second attempt uses the same business work key so the destination can deduplicate the result, then Job completion is recorded.
One CronJob and finite work; no exactly-once business guarantee.
Explore another case
Check Job results and node coverage
Save the file as report-once.yaml. Compare the Job’s completion status with the Pod’s phase and read the output. Then list a DaemonSet already in the cluster and compare its assigned Pods with eligible nodes. For an exercise that uses both ownership and workload status, use the workload management lab.
kubectl apply -f report-once.yaml
kubectl get jobs,pods
kubectl logs job/report-once
kubectl describe job report-once
# Inspect node-local coverage where the cluster already has DaemonSets.
kubectl get daemonsets -A
kubectl get pods -A -o widePlan for missed or overlapping scheduled runs
A CronJob creates Jobs according to a schedule. If a new run comes due while the previous one is active, Allow permits overlap, Forbid skips a concurrent run, and Replace replaces the active Job with a new one. These policies apply to Jobs created by the same CronJob. Check the time zone used for the schedule and decide how late a missed run may start.
A CronJob can miss a run or create more than one Job for a scheduled time in some conditions. Job retries can also repeat work after a partial failure. Make the task safe to repeat. One report generator might write to a path keyed by the report’s date and replace the completed file atomically after a successful run.
Check your understanding
A daily report usually takes 10 minutes but sometimes runs past the next scheduled time. What does Forbid protect, and what do you still need to handle?
It prevents the same CronJob from starting an overlapping run. That can mean skipping a report at its scheduled time. The report code still needs to handle retries and partial results safely, and the operator needs a way to see missing outputs.
Primary documentation
Go to the source
Reviewed 2026-10-27 · Kubernetes 1.37 · Related local exercise