Overview

A cron job builds on a regular job by allowing you to specifically schedule how the job should be run. Cron jobs are part of the Kubernetes API, which can be managed with oc commands like other object types.

Creating a Cron Job

A cron job configuration consists of the following key parts:

  • A schedule specified in cron format.

  • A job template used when creating the next job.

  • An optional deadline (in seconds) for starting the job if it misses its scheduled time for any reason. Missed jobs executions will be counted as failed ones. If not specified, there is no deadline.

  • ConcurrencyPolicy: An optional concurrency policy, specifying how to treat concurrent jobs within a cron job. Only one of the following concurrent policies may be specified. If not specified, this defaults to allowing concurrent executions.

    • Allow allows Cron Jobs to run concurrently.

    • Forbid forbids concurrent runs, skipping the next run if the previous has not finished yet.

    • Replace cancels the currently running job and replaces it with a new one.

  • An optional flag allowing the suspension of a cron job. If set to true, all subsequent executions will be suspended.

The following is an example of a CronJob resource:

apiVersion: batch/v2alpha1
kind: CronJob
metadata:
  name: pi
spec:
  schedule: "*/1 * * * *"  (1)
  jobTemplate:             (2)
    spec:
      template:
        spec:
          containers:
          - name: pi
            image: perl
            command: ["perl",  "-Mbignum=bpi", "-wle", "print bpi(2000)"]
          restartPolicy: Never
  1. Schedule for the job. In this example, a job will run every minute.

  2. Job template. This is similar to the job example.