kubernetes

Kubernetes Job and CronJob

Learn how Kubernetes Jobs and CronJobs run batch workloads, retries, parallel tasks, schedules, concurrency, cleanup, troubleshooting, and production workloads.

By Shubhankar Tripathi • • 5 min read

Kubernetes Job and CronJob Explained

Batch and Scheduled Workloads

Practical guide to one-time, parallel, retryable, and scheduled workloads in Kubernetes

Introduction

Not every application in Kubernetes needs to run continuously. Some workloads have a clear beginning and end: database migrations, data exports, report generation, image processing, reconciliation tasks, backups, and one-time administrative operations. Kubernetes provides the Job resource for these workloads.

When the same type of task must be started repeatedly according to a schedule, Kubernetes provides CronJob. A CronJob creates Jobs on a repeating schedule, while each Job is responsible for creating and supervising Pods until the required work is completed.

The key mental model is simple: a Job answers “run this task to completion,” while a CronJob answers “create a Job for this task on this schedule.”

1. What Is a Kubernetes Job?

A Kubernetes Job represents a one-off task that runs to completion and then stops. The Job controller creates Pods and keeps retrying according to the Job configuration until the required number of successful completions is reached.

A simple Job normally runs one Pod to successful completion. If that Pod fails or is deleted, the Job controller can create another Pod to continue the Job's work.

  • Jobs are designed for finite work rather than continuously running services.

  • A Job can run one task or coordinate multiple Pods for parallel work.

  • The Job tracks successful completions and exposes status through the Kubernetes API.

  • Completed Jobs do not normally delete their Pods automatically, because those Pods can be useful for inspecting logs and diagnosing failures.

2. Job vs Deployment

Characteristic

Job

Deployment

Primary purpose

Run finite work to completion

Run long-lived application Pods

Lifecycle

Starts, completes, and stops

Maintains desired running replicas

Typical workloads

Migration, batch processing, export

API, web service, application server

Success condition

Required work completes successfully

Desired Pods remain available

Scheduling

Immediate / controlled execution

Continuous reconciliation

A useful rule is: if the workload should eventually finish, consider a Job. If it should keep serving traffic or remain continuously available, consider a Deployment or another long-running workload controller.

3. How a Job Works

  1. You create a Job object with a Pod template.

  2. The Job controller observes the Job and creates Pods from that template.

  3. The Pods execute the specified command or application.

  4. If Pods fail, the Job controller can retry according to the Job's failure settings.

  5. Successful Pod completions are counted.

  6. When the required completion condition is met, the Job is marked complete and no additional Pods are created for that Job.

The Job is therefore a higher-level controller around Pods. You describe the desired completion behavior rather than manually creating and monitoring each Pod.

4. Basic Kubernetes Job Example

apiVersion: batch/v1

kind: Job

metadata:

  name: batch-example

spec:

  template:

    spec:

      containers:

        - name: worker

          image: busybox:1.36

          command:

            - /bin/sh

            - -c

            - echo "Batch task started"; sleep 10; echo "Batch task completed"

      restartPolicy: Never

  backoffLimit: 4

The Pod template uses restartPolicy: Never in this example. For a Job Pod template, the allowed restart policies are Never and OnFailure.

The backoffLimit controls how many retries are permitted for failed Pods before the Job is considered failed. Choose this value based on whether the task is safely retryable.

5. Running and Inspecting a Job

kubectl apply -f job.yaml


kubectl get jobs

kubectl get pods


kubectl describe job batch-example

kubectl logs job/batch-example


kubectl get job batch-example -o yaml

For troubleshooting, inspect both the Job and its Pods. The Job tells you the controller-level status, while Pod events and logs usually explain why execution failed.

6. Job Completion and Parallelism

Jobs can represent different processing patterns. Kubernetes supports non-parallel Jobs, fixed-completion parallel Jobs, and work-queue style parallel Jobs.

Pattern

Typical configuration

Use case

Non-parallel

One successful completion

Run one migration or one export

Fixed completion count

spec.completions plus spec.parallelism

Process a known number of independent tasks

Work queue

Parallel workers coordinate through a queue

Process a large set of dynamically assigned work

Indexed Job

completionMode: Indexed

Assign a stable completion index to parallel Pods

The requested parallelism controls how many Pods the Job attempts to run concurrently. If parallelism is omitted, it defaults to 1. Actual parallelism can be lower because of scheduling constraints, quotas, failed Pod creation, controller throttling, or graceful termination.

7. Fixed Completion Example

apiVersion: batch/v1

kind: Job

metadata:

  name: parallel-job

spec:

  completions: 6

  parallelism: 3

  template:

    spec:

      containers:

        - name: worker

          image: busybox:1.36

          command: ["/bin/sh", "-c", "echo processing; sleep 10"]

      restartPolicy: Never

  backoffLimit: 3

Here, the Job requires six successful completions and requests up to three Pods in parallel. Kubernetes continues creating work until the required successful completion count is reached.

8. Indexed Jobs

For fixed-completion Jobs, completionMode can be set to Indexed. Each Pod receives a completion index in the range from 0 to completions minus 1. This is useful when each worker must process a distinct, predetermined partition of work.

apiVersion: batch/v1

kind: Job

metadata:

  name: indexed-job

spec:

  completions: 4

  parallelism: 2

  completionMode: Indexed

  template:

    spec:

      containers:

        - name: worker

          image: busybox:1.36

          command:

            - /bin/sh

            - -c

            - echo "processing indexed work"

      restartPolicy: Never

The exact application logic should use the completion index to determine which slice of work the Pod owns. Kubernetes supplies the index; your application must decide how that index maps to real data.

9. Job Failure, Retries, and Backoff

Batch processing often fails because of transient dependencies, network problems, bad input, resource pressure, or application errors. Kubernetes Jobs provide retry behavior so that a failed Pod does not necessarily mean the entire batch operation must be restarted manually.

  • Use backoffLimit to bound retries.

  • Make batch operations idempotent when possible, so retrying does not create duplicate side effects.

  • Use application-level checkpoints for long-running or partitioned processing.

  • Inspect Pod logs and events before simply increasing retry counts.

  • For advanced failure handling, Kubernetes provides Pod failure policy capabilities for Jobs.

10. Job Cleanup

A completed Job remains as a Kubernetes object unless it is deleted or cleaned up by another mechanism. Keeping completed Pods can be useful because their logs may be needed for diagnostics.

For automatic cleanup, Jobs can use ttlSecondsAfterFinished. CronJobs can also control how many successful and failed Jobs are retained in their history.

apiVersion: batch/v1

kind: Job

metadata:

  name: cleanup-example

spec:

  ttlSecondsAfterFinished: 3600

  template:

    spec:

      containers:

        - name: worker

          image: busybox:1.36

          command: ["/bin/sh", "-c", "echo done"]

      restartPolicy: Never

11. What Is a Kubernetes CronJob?

A CronJob creates Jobs on a repeating schedule. It is the Kubernetes equivalent of expressing recurring work in a cron-like schedule, but the actual unit of execution is still a Job.

This separation is important: CronJob handles when work should start; Job handles how that individual execution runs to completion.

12. CronJob Architecture

CronJob

   |

   | creates on schedule

   v

 Job #1 ----> Pods ----> Complete / Failed

 Job #2 ----> Pods ----> Complete / Failed

 Job #3 ----> Pods ----> Complete / Failed

   |

   +--> history / cleanup policies

A CronJob does not directly manage a long-running Pod. Each scheduled occurrence creates a Job, and that Job creates and supervises its Pods.

13. Basic CronJob Example

apiVersion: batch/v1

kind: CronJob

metadata:

  name: scheduled-report

spec:

  schedule: "0 2 * * *"

  jobTemplate:

    spec:

      template:

        spec:

          containers:

            - name: report

              image: busybox:1.36

              command:

                - /bin/sh

                - -c

                - echo "Generating daily report"

          restartPolicy: OnFailure

The schedule above represents 02:00 each day according to the CronJob's configured time-zone behavior. The jobTemplate contains the Job specification that Kubernetes uses for each scheduled execution.

14. Cron Schedule Syntax

Field

Allowed values

Example

Minute

0-59

0

Hour

0-23

2

Day of month

1-31

*

Month

1-12

*

Day of week

0-6 (Sunday-Saturday)

*

# Every hour

0 * * * *


# Every day at 02:00

0 2 * * *


# Every Monday at 03:00

0 3 * * 1


# Every 15 minutes

*/15 * * * *

Kubernetes also supports cron macros such as @hourly, @daily, @weekly, @monthly, and @yearly. Always verify the schedule against the desired operational time zone and business requirement.

15. CronJob Time Zones

Kubernetes supports an explicit .spec.timeZone field for CronJobs. The feature is stable since Kubernetes v1.27. If no time zone is specified, the kube-controller-manager's local time zone is used to interpret the schedule.

spec:

  schedule: "0 2 * * *"

  timeZone: "Etc/UTC" 

For production workloads, an explicit time zone can make the schedule easier to understand and operate, especially when teams and systems span multiple regions.

16. CronJob Concurrency Policy

A recurring task can still be running when the next scheduled occurrence arrives. CronJob provides concurrencyPolicy to define what should happen.

Policy

Behavior

When useful

Allow

Allow concurrent Jobs from the same CronJob

Independent runs are safe to overlap

Forbid

Skip a new run while the previous run is still active

Prevent overlapping backups or reports

Replace

Replace the currently running Job with the new run

Only the newest run should continue

The default is Allow. Concurrency policy applies to Jobs created by the same CronJob; separate CronJobs are not constrained by each other's policy.

17. Preventing Overlapping Batch Work

apiVersion: batch/v1

kind: CronJob

metadata:

  name: nightly-export

spec:

  schedule: "0 1 * * *"

  concurrencyPolicy: Forbid

  jobTemplate:

    spec:

      template:

        spec:

          containers:

            - name: export

              image: my-exporter:v1

              command: ["/app/export"]

          restartPolicy: Never

This is a common production pattern when a second run would conflict with the first run, consume excessive resources, or generate duplicate output.

18. startingDeadlineSeconds

A CronJob can miss a scheduled execution because of controller downtime, scheduling problems, or other conditions. startingDeadlineSeconds defines how late a missed execution may be started. If the deadline is exceeded, that occurrence is skipped.

spec:

  schedule: "0 2 * * *"

  startingDeadlineSeconds: 1800

In this example, an occurrence can be started up to 30 minutes after its scheduled time. Future occurrences remain scheduled normally.

19. Suspending a CronJob

Set spec.suspend to true to stop new scheduled executions from being started. Suspending a CronJob does not terminate Jobs that have already started.

spec:

  suspend: true

When a suspended CronJob is later resumed, missed schedules can affect execution behavior. Review startingDeadlineSeconds and the number of missed schedules before using suspension as an operational control.

20. CronJob History and Cleanup

CronJobs retain completed Jobs according to successfulJobsHistoryLimit and failedJobsHistoryLimit. The documented defaults are three successful Jobs and one failed Job.

spec:

  successfulJobsHistoryLimit: 3

  failedJobsHistoryLimit: 1

Retention should balance operational debugging needs against unnecessary accumulation of historical objects.

21. Job vs CronJob

Question

Job

CronJob

Runs once?

Yes, typically

Each scheduled occurrence creates a Job

Repeating schedule?

No built-in repeating schedule

Yes

Creates Pods?

Yes

Indirectly, through Jobs

Tracks completion?

Yes

Each generated Job tracks completion

Concurrency control?

Job-level execution settings

Allow / Forbid / Replace

Typical use

Migration, batch, one-time processing

Backup, report, recurring cleanup

22. Real-World Architecture: Daily Data Pipeline

CronJob

  |

  | 02:00 UTC

  v

Job: daily-ingestion

  |

  +--> Pod: extract

  |

  +--> Pod: transform

  |

  +--> Pod: load

  |

  +--> Success / Failure

          |

          +--> logs / metrics / alerts

In a real system, the Job may invoke a single application that performs the complete pipeline, or it may coordinate parallel workers. External systems such as object storage, databases, queues, and monitoring platforms can provide the durable state and observability required by the batch process.

23. Common Production Use Cases

  • Database schema migrations during controlled releases.
  • Nightly or hourly data exports.
  • Generating business reports.
  • Processing files uploaded to object storage.
  • Periodic reconciliation between systems.
  • Cache or temporary-data cleanup.
  • Batch machine-learning inference.
  • Media conversion or document processing.
  • Periodic integrity checks.
  • Scheduled operational maintenance.

24. Common Mistakes

Mistake

Why it is a problem

Better approach

Using Deployment for finite work

Deployment continuously tries to keep Pods running

Use Job for completion-based work

Ignoring retries

Transient failures can cause unnecessary manual intervention

Set appropriate retry behavior

Allowing unsafe overlap

Two batch runs may update the same data

Use concurrencyPolicy: Forbid when appropriate

No cleanup policy

Old Jobs can accumulate

Use TTL or CronJob history limits

Hard-coding unclear schedule time

Operators may misinterpret when it runs

Use explicit timeZone where appropriate

Non-idempotent processing

Retries can duplicate side effects

Design operations to be safely repeatable

No resource requests/limits

Batch jobs can compete unpredictably for node resources

Size resources based on measured workload behavior

25. Troubleshooting Workflow

  1. Check whether the Job or CronJob object exists.

  2. Inspect Job status and conditions.

  3. List Pods owned by the Job.

  4. Inspect Pod status, events, and logs.

  5. Check whether the image can be pulled.

  6. Check resource requests, quotas, and scheduling constraints.

  7. Check retry and failure settings.

  8. For CronJobs, inspect schedule, suspension, concurrency policy, deadlines, and recent Job history.

  9. Confirm that the application itself exits successfully when the work is complete.

kubectl get cronjobs

kubectl get jobs --sort-by=.metadata.creationTimestamp

kubectl get pods


kubectl describe cronjob scheduled-report

kubectl describe job <job-name>

kubectl describe pod <pod-name>


kubectl logs job/<job-name>

kubectl get events --sort-by=.lastTimestamp

26. Troubleshooting: Job Never Completes

  • Check whether the container command actually exits.
  • Inspect application logs for blocked network calls or external dependencies.
  • Check Pod restarts and termination reasons.
  • Verify the completion and parallelism configuration.
  • Check whether the application is waiting indefinitely for work.
  • Check resource availability and scheduling events.

27. Troubleshooting: CronJob Does Not Run

  • Check the CronJob's schedule and namespace.
  • Check whether spec.suspend is true.
  • Inspect recent Jobs created by the CronJob.
  • Review startingDeadlineSeconds if executions are being skipped.
  • Check controller and cluster health if no new Job appears.
  • Confirm the time zone and expected execution time.

28. Security and Reliability Best Practices

  • Run batch containers as non-root when the application permits it.
  • Use minimal, trusted container images and pin versions according to your release strategy.
  • Use Kubernetes Secrets or an external secret-management solution instead of embedding credentials in manifests.
  • Apply least-privilege ServiceAccounts and RBAC.
  • Set resource requests and appropriate limits based on workload measurements.
  • Design tasks to be idempotent because retries can occur.
  • Use explicit time zones for important schedules when that improves operational clarity.
  • Control CronJob concurrency when overlapping executions are unsafe.
  • Retain enough Job history to support troubleshooting, but clean up unnecessary objects.
  • Emit useful application logs and metrics so batch failures can be detected quickly.

29. Hands-On Lab: Create a One-Time Job

  1. Create job.yaml with the basic Job example.

  2. Apply it using kubectl apply -f job.yaml.

  3. Run kubectl get jobs and wait for the completion count to show success.

  4. Find the Pod with kubectl get pods.

  5. Read the Pod logs.

  6. Describe the Job and inspect its events.

  7. Delete the Job after completing the lab.

kubectl apply -f job.yaml

kubectl get jobs

kubectl get pods

kubectl logs job/batch-example

kubectl describe job batch-example

kubectl delete job batch-example

30. Hands-On Lab: Create a CronJob

  1. Create cronjob.yaml with a short test schedule such as every minute.

  2. Apply the manifest.

  3. Watch the CronJob and Jobs with kubectl get cronjob,jobs.

  4. Inspect the generated Pod and its logs.

  5. Change the schedule to the desired production interval.

  6. Test concurrencyPolicy with a workload that runs longer than its schedule interval.

  7. Clean up the CronJob after testing.

kubectl apply -f cronjob.yaml

kubectl get cronjobs

kubectl get jobs

kubectl get pods

kubectl logs job/<generated-job-name>

kubectl delete cronjob scheduled-report

31. Interview Questions

1. What is a Kubernetes Job?

Answer: A controller for finite work that creates Pods and tracks successful completion.

2. What is a CronJob?

Answer: A controller that creates Jobs according to a repeating schedule.

3. What is the relationship between CronJob, Job, and Pod?

Answer: CronJob creates Jobs; Jobs create and supervise Pods.

4. What is the difference between Job and Deployment?

Answer: Job is completion-oriented; Deployment maintains long-running application replicas.

5. What does parallelism control?

Answer: It specifies the requested maximum number of Pods running concurrently for a Job.

6. What does completions control?

Answer: It specifies the number of successful Pod completions required for a fixed-completion Job.

7. What is Indexed completion mode?

Answer: It gives fixed-completion Pods distinct completion indexes.

8. What does backoffLimit do?

Answer: It limits retries after Pod failures before the Job is considered failed.

9. What does concurrencyPolicy do on a CronJob?

Answer: It determines whether scheduled Jobs may overlap: Allow, Forbid, or Replace.

10. Why use timeZone?

Answer: It makes the intended CronJob schedule time zone explicit.

11. What does suspend do?

Answer: It prevents new scheduled executions from being started while true.

12. How can completed Jobs be cleaned up?

Answer: Use Job TTL for finished Jobs or CronJob history limits for Jobs created by a CronJob.

32. Scenario-Based Interview Questions

A nightly report sometimes takes 90 minutes, but it is scheduled every hour. How would you prevent overlapping runs?

Expected reasoning: Consider concurrencyPolicy: Forbid if skipping overlapping runs is acceptable. If the business requires a different behavior, choose the policy deliberately and make the batch operation safe for the chosen semantics.

A Job fails because an external API temporarily returns errors. What should you consider before increasing backoffLimit?

Expected reasoning: Determine whether the operation is safely retryable, whether the failure is transient, and whether application-level backoff, idempotency, and dependency limits also need to be addressed.

A CronJob appears to have stopped running after a maintenance window. What would you inspect?

Expected reasoning: Check CronJob status, suspend, schedule, time zone, recent Jobs, startingDeadlineSeconds, and cluster/controller health.

A parallel Job creates fewer Pods than requested. Is that automatically a Job failure?

Expected reasoning: No. Requested parallelism is not always the actual instantaneous Pod count. Scheduling capacity, quotas, controller behavior, failed Pod creation, and other constraints can reduce actual parallelism.

33. Production Readiness Checklist

  • The workload has a clear completion condition.
  • The container exits with an appropriate status when work completes.
  • Retry behavior is understood and bounded.
  • The workload is idempotent or has safe deduplication/checkpointing.
  • Resource requests are sized from real workload behavior.
  • Credentials are not hard-coded in the image or manifest.
  • CronJob concurrency behavior is explicitly chosen.
  • The schedule and time zone are documented.
  • Missed execution behavior is understood.
  • Finished Job retention and cleanup are configured.
  • Logs and metrics are available for operations.
  • Alerts exist for important failures or missed processing windows.

34. Key Takeaways

  • Use a Job for work that should run to completion.
  • Use a CronJob when Jobs need to be created repeatedly on a schedule.
  • A CronJob creates Jobs; the Jobs create and manage Pods.
  • Use completions and parallelism to model batch processing.
  • Use Indexed Jobs when fixed parallel work needs distinct completion indexes.
  • Use retry and failure controls deliberately; retries are not a substitute for idempotent application design.
  • Use CronJob concurrencyPolicy when overlapping executions need explicit control.
  • Use time zones and cleanup policies to make scheduled workloads easier to operate.
  • Troubleshoot from CronJob → Job → Pod → application logs and events.

35. Suggested Next Kubernetes Articles

  • Kubernetes Service Explained: ClusterIP, NodePort, LoadBalancer and Service Discovery
  • Kubernetes ConfigMap and Secret Explained: Configuration and Sensitive Data
  • Kubernetes PersistentVolume and PersistentVolumeClaim Explained
  • Kubernetes Ingress Explained: Routing External HTTP and HTTPS Traffic
  • Kubernetes Probes Explained: Liveness, Readiness and Startup Probes
  • Kubernetes Scheduling Explained: Requests, Limits, Taints, Tolerations and Affinity

Official References

The technical details in this article were checked against the official Kubernetes documentation, including the current Job and CronJob concepts and API behavior.

Kubernetes Jobs documentation: https://kubernetes.io/docs/concepts/workloads/controllers/job/

Kubernetes CronJobs documentation: https://kubernetes.io/docs/concepts/workloads/controllers/cron-jobs/

Kubernetes Workloads overview: https://kubernetes.io/docs/concepts/workloads/