100% Free Forever
AI-Powered Learning
Industry Expert Content
Certificates & Badges
Learn At Your Own Pace

What are Kubernetes Job and CronJob?

Learn what Kubernetes Job and CronJob are, how completions and schedule work, and when to use each for batch or recurring tasks.

mediumQ48 of 224 in DevOps Est. time: 6 minsLast updated:
Open Code Lab

Expected Interview Answer

A Kubernetes Job runs one or more Pods to completion for a finite, run-once task and tracks success, while a CronJob wraps a Job template with a cron schedule so that task automatically runs again and again at defined intervals.

A Job creates Pods and keeps retrying (up to `backoffLimit`) until the required number of Pods, controlled by `completions` and `parallelism`, finish successfully, then it stops — unlike a Deployment, a Job’s Pods are not restarted forever, since exiting successfully is the goal, not a failure. This makes Jobs suited to one-off or batch work like a database migration, a report generation script, or a bulk data-processing task. A CronJob is a higher-level controller that owns a Job template plus a `schedule` field written in standard cron syntax, such as `0 2 * * *` for every day at 2 AM, and it creates a new Job object at each scheduled tick. `concurrencyPolicy` controls what happens if a previous run is still in progress when the next tick arrives — `Allow` runs both, `Forbid` skips the new one, and `Replace` cancels the old one.

  • Job guarantees a finite task runs to completion with automatic retries
  • CronJob automates recurring scheduled tasks without external schedulers
  • concurrencyPolicy prevents overlapping runs when needed
  • Both fit naturally into backups, migrations, and batch report generation

AI Mentor Explanation

A Job is like a one-off net practice session booked for a specific player: it runs until the player completes the required number of throw-downs, and if a delivery is dropped it is retried up to a set limit, but once the session goal is met it simply ends. A CronJob is like a standing arrangement with the ground staff to automatically book that exact same net session every single morning at 6 AM without anyone re-booking it. If yesterday’s session ran long into today’s slot, the concurrency policy decides whether both run together, the new one is skipped, or the old one is cut off.

Step-by-Step Explanation

  1. Step 1

    Define a Job for one-off work

    Set completions, parallelism, and backoffLimit on a Pod template that must exit successfully.

  2. Step 2

    Wrap it in a CronJob for recurrence

    Add a schedule field in cron syntax and a jobTemplate matching the Job spec.

  3. Step 3

    Kubernetes creates Jobs on schedule

    At each tick, the CronJob controller spawns a new Job object from the template.

  4. Step 4

    Control overlap and history

    concurrencyPolicy handles overlapping runs; successfulJobsHistoryLimit/failedJobsHistoryLimit control retained Job records.

What Interviewer Expects

  • Understanding that Job Pods run to completion rather than forever
  • Knowledge of completions, parallelism, and backoffLimit on Jobs
  • Ability to explain CronJob schedule syntax and concurrencyPolicy
  • Awareness of real use cases: migrations, backups, scheduled reports

Common Mistakes

  • Using a Deployment for a task meant to finish and exit, causing restart loops
  • Forgetting backoffLimit caps retries and the Job eventually gives up
  • Not setting concurrencyPolicy, leading to unexpected overlapping CronJob runs
  • Confusing CronJob schedule timezone assumptions with cluster-local time

Best Answer (HR Friendly)

A Job is for a task that needs to run once and finish, like a database migration script, and Kubernetes makes sure it actually completes, retrying if it fails. A CronJob is just a Job on a schedule — like a cron-style recurring task — for things we need to happen automatically every day or every hour, such as nightly backups or report generation.

Code Example

A nightly backup CronJob
apiVersion: batch/v1
kind: CronJob
metadata:
  name: nightly-db-backup
spec:
  schedule: "0 2 * * *"
  concurrencyPolicy: Forbid
  jobTemplate:
    spec:
      backoffLimit: 3
      template:
        spec:
          restartPolicy: OnFailure
          containers:
            - name: backup
              image: myorg/db-backup:1.2
              command: ["/bin/sh", "-c", "backup.sh"]

Follow-up Questions

  • What is the difference between completions and parallelism on a Job?
  • How does concurrencyPolicy prevent overlapping CronJob runs?
  • What restartPolicy values are valid for a Job Pod?
  • How would you clean up completed Job history automatically?

MCQ Practice

1. What is the defining behavior of a Kubernetes Job?

A Job manages Pods meant to finish successfully a set number of times, unlike long-running Deployment Pods.

2. What does a CronJob add on top of a plain Job?

A CronJob wraps a Job template with a cron schedule, spawning a new Job at each scheduled tick.

3. What does concurrencyPolicy: Forbid do on a CronJob?

Forbid prevents a new scheduled Job from starting while a previous run from the same CronJob is still active.

Flash Cards

What is a Kubernetes Job?A controller that runs Pods to completion for a finite task, with retries.

What is a CronJob?A controller that creates a new Job on a cron schedule.

What does backoffLimit control?The max number of retries before a Job is marked failed.

What does concurrencyPolicy control?Whether overlapping CronJob runs are allowed, skipped, or replaced.

1 / 4

Continue Learning