Skip to main content
Kubernetes coreLesson 12 of 13

Run Once and Finish (Job · CronJob)

A program that builds a monthly report does not need to stay up. It just needs to run once and finish. Follow the Job that handles this kind of work, and the CronJob that stamps out Jobs on a schedule.

Job statusnot started
Failures / limit0 / 3
CronJob runs—
UserkubectlCronJobdaily 02:00Jobmonthly reportJob · 02:00scheduled runpod-11st trypod-2retrypod02:00 runsubmit Job
RequestWatchAppears in a later step

What you will learn

  • How a Job creates a Pod and watches it to completion
  • How retries up to backoffLimit work
  • A CronJob does no work itself; it stamps out Jobs
  • The concurrency policy for overlapping runs

Concurrency policy at a glance​

concurrencyPolicyWhen the previous run is still goingGood for
Allow (default)Creates a new Job that runs alongsideRuns that do not affect each other
ForbidSkips this scheduled runWork on the same data that must not overlap
ReplaceDeletes the running Job and starts a new oneWork where only the latest run matters

Try it yourself​

kubectl get cronjob -A # SCHEDULE and LAST SCHEDULE
kubectl create job try-job --image=busybox -- sh -c 'echo done' # Throwaway Job
kubectl get job try-job # COMPLETIONS reaches 1/1
kubectl logs job/try-job # Logs live in the Job's Pod
kubectl delete job try-job # Clean up (deletes its Pod too)

Check yourself

  1. A Job has backoffLimit: 3, and its first Pod exits with an error. What happens next?
  2. A nightly CronJob at 02:00 is sometimes still running at 02:00 the next day, and two runs must not edit the same data at once. What should you do?
  3. After a Job finishes, its Pod shows Completed and does not restart. Is something wrong?

Further reading: Jobs · CronJob