Updated on 2022-09-08 GMT+08:00

Job and Cron Job

Job and Cron Job

Jobs and cron jobs allow you to run short lived, one-off tasks in batch. They ensure the task pods run to completion.

  • A job is a resource object used by Kubernetes to control batch tasks. Jobs are different from long-term servo tasks (such as Deployments and StatefulSets). The former is started and terminated at specific times, while the latter runs unceasingly unless being terminated. The pods managed by a job will be automatically removed after successfully completing tasks based on user configurations.
  • A cron job runs a job periodically on a specified schedule. A cron job object is similar to a line of a crontab file in Linux.

This run-to-completion feature of jobs is especially suitable for one-off tasks, such as continuous integration (CI).

Creating a Job

The following is an example job, which calculates π till the 2000th digit and prints the output. 50 pods need to be run before the job is ended. In this example, print π calculation results for 50 times, and run five pods concurrently. If a pod fails to be run, a maximum of five retries are supported.
apiVersion: batch/v1
kind: Job
metadata:
  name: pi-with-timeout
spec:
  completions: 50            # Number of pods that need to run successfully to end the job
  parallelism: 5             # Number of pods that run concurrently. The default value is 1.
  backoffLimit: 5            # Maximum number of retries performed if a pod fails. When the limit is reached, it will not try again.
  activeDeadlineSeconds: 10  # Timeout interval of pods. Once the time is reached, all pods of the job are terminated.
  template:                  # Pod definition
    spec: 
      containers:
      - name: pi
        image: perl
        command:
        - perl
        - "-Mbignum=bpi"
        - "-wle"
        - print bpi(2000)
      restartPolicy: Never

Based on the completions and Parallelism settings, jobs can be classified as follows:

Table 1 Job types

Job Type

Description

Example

One-off job

One pod runs until it is successfully ends.

Database migration

Jobs with a fixed completion count

One pod runs until the specified completion count is reached.

Pod for processing work queues

Parallel jobs with a fixed completion count

Multiple pods run until the specified completion count is reached.

Multiple pods for processing work queues concurrently

Parallel jobs

One or more pods run until one pod is successfully ended.

Multiple pods for processing work queues concurrently

Creating a Cron Job

Compared with a job, a cron job is a scheduled job. A cron job runs a job periodically on a specified schedule, and the job creates pods.

apiVersion: batch/v1beta1
kind: CronJob
metadata:
  name: cronjob-example
spec:
  schedule: "0,15,30,45 * * * *"           # Scheduling configuration
  jobTemplate:                             # Job definition
    spec:
      template:
        spec:
          restartPolicy: OnFailure
          containers:
          - name: main
            image: pi

The format of the cron job is as follows:

  • Minute
  • Hour
  • Day of month
  • Month
  • Day of week

For example, in 0,15,30,45 * * * *, commas separate minutes, the first asterisk (*) indicates the hour, the second asterisk indicates the day of the month, the third asterisk indicates the month, and the fourth asterisk indicates the day of the week.

If you want to run the job every half an hour on the first day of each month, set this parameter to 0,30 * 1 * *. If you want to run the job on 3:00 a.m. every Sunday, set this parameter to 0 3 * * 0.

For details about the cron job format, visit https://en.wikipedia.org/wiki/Cron.