DaemonSets, Jobs, & CronJobs

Difficulty: Intermediate

Question

When would you use a DaemonSet vs a Job vs a CronJob, and how are they different from a Deployment?

Answer

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.

DaemonSet: one Pod per node

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:

  • Log shipping: Fluent Bit or Vector reading /var/log/pods.
  • Monitoring: Prometheus node-exporter, Datadog agent.
  • Networking and storage plumbing: 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.

Job: run to completion

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.

CronJob: a Job on a timer

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.

Choosing the right controller

NeedController
Stateless web API with N replicasDeployment
Database with stable identity and storageStatefulSet
Agent on every nodeDaemonSet
One-off migration or backfillJob
Daily 2 AM cleanup or reportCronJob
DaemonSet vs Job vs CronJob. Each controller answers a different question about how many Pods and for how long.
DaemonSetJobCronJob
How many PodsOne per nodeUntil completions reachedOne Job per schedule tick
LifetimeLong runningRuns to completionRepeats on schedule
restartPolicyAlwaysOnFailure or NeverOnFailure or Never
ExampleFluent Bit log agentDB migrationNightly report at 2 AM IST

Code examples

DaemonSet for a node log agent

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.

CronJob with time zone and concurrency control

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.

Key points

  • DaemonSet = one Pod per node, no replicas field; used for agents and node plumbing.
  • Job = run to completion; restartPolicy must be OnFailure or Never; backoffLimit defaults to 6.
  • CronJob = schedule + jobTemplate; set timeZone or it defaults to the controller's zone (usually UTC).
  • concurrencyPolicy Forbid prevents overlapping runs; make jobs idempotent anyway.
  • ttlSecondsAfterFinished cleans up finished Jobs automatically.

Common follow-up questions

  • How would you run a DaemonSet only on GPU nodes?
  • What happens if a CronJob misses its schedule because the controller was down?
  • How do you run a Job that processes 100 shards with 10 workers in parallel?
  • Why are DaemonSet Pods not evicted by kubectl drain by default?

Interview tips

  • Anchor each controller to a real example (log agent, migration, nightly report).
  • Mention idempotency for CronJobs; interviewers like hearing you expect duplicate or missed runs.
  • Bring up the timeZone field; many candidates still think CronJobs are UTC only.

Concepts covered

DaemonSet, Job, CronJob, Parallelism, backoffLimit, concurrencyPolicy

More Kubernetes & Orchestration questions