Difficulty: Intermediate
When would you use a DaemonSet vs a Job vs a CronJob, and how are they different from a Deployment?
Use a DaemonSet when exactly one copy of a Pod must run on every node (or every matching node), such as a log collector or node exporter. Use a Job for a task that must run to completion, like a database migration or a batch report, and a CronJob when that Job should run on a schedule. A Deployment, by contrast, keeps N interchangeable long-running replicas alive somewhere in the cluster.
The DaemonSet controller watches nodes. When a node joins, it creates a Pod there; when a node leaves, the Pod is garbage collected. You do not set replicas.
Typical uses:
/var/log/pods.node-exporter, Datadog agent.kube-proxy, CNI agents like Cilium or Calico, CSI node plugins.Restrict it to a subset of nodes with nodeSelector or node affinity. DaemonSet Pods automatically get tolerations for conditions like not-ready and unschedulable, which is why they keep running on cordoned nodes. Updates use RollingUpdate (default) or OnDelete.
A Job creates Pods until a given number succeed. Key fields:
completions: how many successful Pods are needed (default 1).parallelism: how many run at once.backoffLimit: retries before the Job is marked failed (default 6, with exponential back-off).activeDeadlineSeconds: a hard timeout for the whole Job.ttlSecondsAfterFinished: auto-delete the Job and its Pods after it finishes.completionMode: Indexed: each Pod gets JOB_COMPLETION_INDEX, useful for sharding work.The Pod's restartPolicy must be OnFailure or Never; Always is not allowed.
A CronJob creates a Job object at each tick of its schedule (standard five-field cron syntax). Important fields:
timeZone (stable since 1.27), for example Asia/Kolkata. Without it, the schedule uses the kube-controller-manager's time zone, usually UTC.concurrencyPolicy: Allow (default), Forbid (skip if the previous run is still going) or Replace.startingDeadlineSeconds: how late a missed run may still start.successfulJobsHistoryLimit (default 3) and failedJobsHistoryLimit (default 1).A nightly settlement report at a fintech should use Forbid so two runs never process the same day twice, and the job itself should be idempotent because a CronJob can occasionally create two Jobs or none.
| Need | Controller |
|---|---|
| Stateless web API with N replicas | Deployment |
| Database with stable identity and storage | StatefulSet |
| Agent on every node | DaemonSet |
| One-off migration or backfill | Job |
| Daily 2 AM cleanup or report | CronJob |
| DaemonSet | Job | CronJob | |
|---|---|---|---|
| How many Pods | One per node | Until completions reached | One Job per schedule tick |
| Lifetime | Long running | Runs to completion | Repeats on schedule |
| restartPolicy | Always | OnFailure or Never | OnFailure or Never |
| Example | Fluent Bit log agent | DB migration | Nightly report at 2 AM IST |
apiVersion: apps/v1
kind: DaemonSet
metadata:
name: fluent-bit
namespace: logging
spec:
selector:
matchLabels: { app: fluent-bit }
template:
metadata:
labels: { app: fluent-bit }
spec:
tolerations:
- operator: Exists # also run on tainted nodes
containers:
- name: fluent-bit
image: fluent/fluent-bit:3.1
volumeMounts:
- name: varlog
mountPath: /var/log
readOnly: true
volumes:
- name: varlog
hostPath: { path: /var/log }
No replicas field: the controller creates exactly one Pod per eligible node and reads host logs through a hostPath mount.
apiVersion: batch/v1
kind: CronJob
metadata:
name: settlement-report
spec:
schedule: "0 2 * * *" # 02:00 every day
timeZone: Asia/Kolkata
concurrencyPolicy: Forbid # never overlap runs
startingDeadlineSeconds: 600
jobTemplate:
spec:
backoffLimit: 3
activeDeadlineSeconds: 3600
ttlSecondsAfterFinished: 86400
template:
spec:
restartPolicy: OnFailure
containers:
- name: report
image: myorg/settlement:1.8.0
args: ["--date=yesterday"]
Each tick creates a Job; Forbid skips a run if yesterday's is still going, and the Job retries up to three times within a one hour deadline.
DaemonSet, Job, CronJob, Parallelism, backoffLimit, concurrencyPolicy