CronJob in Kubernetes – Automating Tasks on a Schedule

Running Cron Jobs in Kubernetes

CronJobs are a way to run a task on a time-based schedule and have been around for a long time in Linux and UNIX systems. They can be used in Kubernetes (K8S) to run recurring tasks such as backup jobs, triggering emails, report generation, or automating restarts of containers.

In this article, we will dive into how to define a CronJob, look at how to implement them in K8S manifest files with an example, and the available options.

CronJob Syntax

To define a Cronjob, the schedule is defined using the CronJob syntax below:

# ┌───────────── minute (0 - 59)
# │ ┌───────────── hour (0 - 23)
# │ │ ┌───────────── day of the month (1 - 31)
# │ │ │ ┌───────────── month (1 - 12)
# │ │ │ │ ┌───────────── day of the week (0 - 6) (Sun to Sat;
# │ │ │ │ │                      7 is also Sunday on some systems)
# │ │ │ │ │                   OR sun, mon, tue, wed, thu, fri, sat
# │ │ │ │ │
# * * * * *
0 23 * * *

A job running every minute would look like this:

* * * * *

Check out Crontab.guru to experiment with defining CronJobs.

For CronJobs with no time zone specified, the kube-controller-manager interprets schedules relative to its local time zone. As of Kubernetes v1.25 [beta] the CronJobTimeZone feature gate can be enabled, which enables a specific time zone to be specified should it be required. For example:

spec.timeZone: "Etc/UTC"

CronJob Example

apiVersion: batch/v1
kind: CronJob
  name: hello
  schedule: "* * * * *"
          - name: hello
            image: busybox:1.28
            imagePullPolicy: IfNotPresent
            - /bin/sh
            - -c
            - date; echo Hello from the Kubernetes cluster
          restartPolicy: OnFailure

Create the deployment:

kubectl create -f .\cronjob.yaml

Verify the CronJob has been created:

kubectl get cronjob hello
kubectl get jobs --watch

To view the pods that have been created to run the jobs:

kubectl get pods
kubectl get pods

If you have lots of running pods you’ll want to filter the selection to the job name in your system using the --selector argument. Note that only the last three pods will be shown by default unless a different value has been specified by the optional field spec.successfulJobsHistoryLimit.

kubectl get pods --selector=job-name=hello-27827258

To view the logs from the pod to verify the command ran successfully:

kubectl logs hello-27827258--1-rdf4s
kubectl logs hello-27827258--1-rdf4s

To clean up, delete the CronJob. Deleting the CronJob removes all the jobs and pods it created and stops it from creating additional jobs:

kubectl delete cronjob hello

CronJob Spec Options

1. startingDeadlineSeconds

  • Allow (default): The CronJob allows concurrently running jobs.
  • Forbid: The CronJob does not allow concurrent runs; if it is time for a new job run and the previous job run hasn’t finished yet, the CronJob skips the new job run.
  • Replace: If it is time for a new job run and the previous job run hasn’t finished yet, the CronJob replaces the currently running job run with a new job run.

3. suspend

kubectl get cronjob hello
kubectl get cronjob hello

Key Points

And take a look at how Spacelift helps you manage the complexities and compliance challenges of using Kubernetes. Anything that can be run via kubectl can be run within a Spacelift stack. Find out more about how Spacelift works with Kubernetes.

The Most Flexible CI/CD Automation Tool

Spacelift is an alternative to using homegrown solutions on top of a generic CI. It helps overcome common state management issues and adds several must-have capabilities s for infrastructure management.

Start free trial