AI Hints
Home›Kubernetes Tutorial›Special Workloads
Kubernetes · Lesson 33 of 86

Jobs and CronJobs

Run to completion, or on a schedule.

⏱ 2 min read▶ Video at 1:20:22🎓 Free
Watch this lesson in the Kubernetes courseWatch on YouTube ↗Full Kubernetes course

Not every program runs forever. Some start, do their work, and finish, like a backup, a report, or a data import. Kubernetes has a workload for these, the job, and another for work that repeats on a timetable, the cron job. In this lesson, we run both.

Jobs and CronJobs
From the video: Jobs and CronJobs
  • Jobs run until finished
  • They retry on failure
  • CronJobs repeat on a schedule

A Job in YAML

Here is a job. Completions says how many successful runs we need, here three. Parallelism says how many pods may run at the same time, here two. The restart policy is never, so a failed container is not restarted in place, and the job creates a new pod, instead. The job stops when three pods have finished successfully.

job.yamlyaml
apiVersion: batch/v1
kind: Job
metadata:
  name: report
spec:
  completions: 3
  parallelism: 2
  template:
    spec:
      restartPolicy: Never
      containers:
      - name: report
        image: busybox:1.36
        command: ["sh", "-c", "echo building report; sleep 3; echo done"]

Running a Job

Let's run it. After a moment, we ask for the job. It shows complete, three of three, and how long it took. The pods are listed with the status completed. They finished, and they stay around, so that you can read their logs. Asking for the logs of the job shows the work that was done. Jobs also clean up after themselves, when you set a time to live, so finished pods do not pile up.

kubectl
kubectl apply -f job.yaml
job.batch/report created
kubectl get job report
NAME     STATUS     COMPLETIONS   DURATION   AGE
report   Complete   3/3           12s        21s
kubectl get pods -l job-name=report
NAME           READY   STATUS      RESTARTS   AGE
report-4ssvb   0/1     Completed   0          21s
report-skkb2   0/1     Completed   0          21s
report-tj292   0/1     Completed   0          15s
kubectl logs job/report | head -3
building report
done
Found 3 pods, using pod/report-4ssvb
File used: job.yaml
apiVersion: batch/v1
kind: Job
metadata:
  name: report
spec:
  completions: 3
  parallelism: 2
  template:
    spec:
      restartPolicy: Never
      containers:
      - name: report
        image: busybox:1.36
        command: ["sh", "-c", "echo building report; sleep 3; echo done"]

Reading a cron schedule

A cron job creates jobs on a schedule. The schedule has five fields, written in the classic cron style. Minute, hour, day of the month, month, and day of the week. A star means, every. So five stars means every minute. Zero two star star star means, at two in the morning, every day. And zero nine star star one, means nine o'clock, every Monday. Cron jobs are perfect for backups, cleanups, and reports.

ScheduleMeaning
* * * * *every minute
*/15 * * * *every 15 minutes
0 2 * * *every day at 02:00
0 9 * * 1every Monday at 09:00

A CronJob ticking

Let's create a cron job that runs every minute, and prints the word tick. We create it, and it waits for the next minute boundary. A little later, the cron job shows when it last ran, and the list of jobs shows one job, that it created, which completed. A minute after, it will create another. When you no longer need it, you delete the cron job, and its jobs go away, too.

kubectl
kubectl create cronjob tick --image=busybox:1.36 --schedule='* * * * *' -- echo tick
cronjob.batch/tick created
kubectl get cronjob tick
NAME   SCHEDULE    TIMEZONE   SUSPEND   ACTIVE   LAST SCHEDULE   AGE
tick   * * * * *   <none>     False     0        6s              75s
kubectl get jobs
NAME            STATUS     COMPLETIONS   DURATION   AGE
tick-29889855   Complete   1/1           2s         66s
tick-29889856   Complete   1/1           3s         6s

Summary

Wonderful. Jobs run work to completion, with retries, and cron jobs repeat them on a schedule. With deployments, stateful sets, daemon sets, and jobs, you know every main workload. Next, we make our apps good citizens, with resource requests, limits, and health checks.

Key takeaways
  • Job: run until done
  • CronJob: on a schedule
  • Pods stay for logs
Next up: Resources and Probes — be a good citizen on the cluster