What is a Job and a CronJob in Kubernetes?
Learn what Kubernetes Jobs and CronJobs are, how completions, parallelism, backoffLimit, and cron schedules run finite and recurring batch tasks.
Expected Interview Answer
A Job is a Kubernetes controller that runs one or more pods to completion for a finite task, retrying until a specified number of successful completions is reached; a CronJob runs a Job on a repeating time schedule, like a Kubernetes-native crontab.
Jobs are for batch or one-off work (migrations, backups, data processing) rather than long-running services. A Job tracks completions and parallelism and marks itself complete when the required successes occur, using backoffLimit for retries. A CronJob wraps a Job template with a cron schedule and manages concurrency policy, history limits, and starting-deadline behavior, creating a new Job at each scheduled time.
- Runs finite batch tasks to guaranteed completion
- Automatic retries via backoffLimit on failure
- Parallelism and completion counts for large batches
- CronJob schedules recurring work with cron syntax
- Concurrency policy and history limits prevent overlap and clutter
AI Mentor Explanation
A Job is like sending a nightwatchman to bat until a set task is done — survive a fixed number of overs — after which their innings ends; if they get out early, a retry sends the next batter until the target is met. A CronJob is the fixed match calendar that automatically schedules a new fixture every week, spinning up that finite innings on a repeating timetable.
Step-by-Step Explanation
Step 1
Define the Job
Create a Job with a pod template, a command that exits 0 on success, and restartPolicy set to Never or OnFailure.
Step 2
Set completions and parallelism
Use completions to require N successes and parallelism to run several pods at once for large batches.
Step 3
Configure retries
Set backoffLimit to cap retry attempts and activeDeadlineSeconds to bound total runtime.
Step 4
Wrap in a CronJob
Add a CronJob with a schedule (cron syntax) whose jobTemplate points to the same pod spec.
Step 5
Control concurrency and history
Set concurrencyPolicy (Allow/Forbid/Replace) and successful/failedJobsHistoryLimit to manage overlap and cleanup.
What Interviewer Expects
- Difference between a Job and a long-running Deployment
- Role of completions, parallelism, and backoffLimit
- How a CronJob schedules Jobs using cron syntax
- Meaning of concurrencyPolicy
- Appropriate use cases like backups and migrations
Common Mistakes
- Using a Deployment for a finite batch task instead of a Job
- Setting restartPolicy to Always on a Job pod (not allowed)
- Ignoring backoffLimit so a failing Job retries endlessly in perception
- Forgetting concurrencyPolicy, causing overlapping CronJob runs
- Not setting history limits, cluttering the namespace with old Jobs
Best Answer (HR Friendly)
“A Job runs a task once and makes sure it finishes successfully, retrying if it fails. A CronJob is the scheduler that runs such a task automatically on a repeating timetable, like nightly backups or weekly reports.”
Code Example
apiVersion: batch/v1
kind: Job
metadata:
name: db-migrate
spec:
completions: 1
parallelism: 1
backoffLimit: 4
template:
spec:
restartPolicy: Never
containers:
- name: migrate
image: myapp:1.0
command: ["python", "manage.py", "migrate"]apiVersion: batch/v1
kind: CronJob
metadata:
name: nightly-backup
spec:
schedule: "0 2 * * *"
concurrencyPolicy: Forbid
successfulJobsHistoryLimit: 3
failedJobsHistoryLimit: 1
jobTemplate:
spec:
backoffLimit: 2
template:
spec:
restartPolicy: OnFailure
containers:
- name: backup
image: backup-tool:2.1
command: ["/bin/sh", "-c", "pg_dump $DB > /backup/db.sql"]Follow-up Questions
- When would you use parallelism greater than one in a Job?
- What does concurrencyPolicy Forbid vs Replace do in a CronJob?
- How does backoffLimit interact with restartPolicy OnFailure?
- How do you clean up completed Jobs automatically (ttlSecondsAfterFinished)?
- What happens if a CronJob misses its schedule window?
MCQ Practice
1. Which restartPolicy values are valid for a Job pod?
Job pods must use restartPolicy Never or OnFailure; Always is not permitted because the task is finite.
2. What does the CronJob schedule "0 2 * * *" mean?
In cron syntax minute=0 hour=2 means the Job runs daily at 02:00.
3. Which field caps the number of retries for a failing Job?
backoffLimit sets the maximum number of retries before the Job is marked failed.
Flash Cards
Job — A controller that runs pods to completion for a finite task, retrying up to backoffLimit until completions succeed.
CronJob — Runs a Job on a repeating cron schedule, like a Kubernetes-native crontab.
completions vs parallelism — completions = required successes; parallelism = how many pods run at once.
concurrencyPolicy — Allow, Forbid, or Replace controls overlap when a CronJob's previous run is still active.
Job restartPolicy — Must be Never or OnFailure, never Always, because a Job is finite work.