Kubernetes Cronjobs
Kubernetes has the concept of Cronjobs which create Jobs according to a schedule. You can use CronJobs to run tasks at a specific time or interval. CronJobs are a good choice for automatic tasks such as backups, reporting, sending emails, or cleanup tasks.
Usage
CronJobs use Job objects to complete their tasks. A CronJob creates a Job object each time it runs. CronJobs are created, managed, scaled, and deleted in the same way as Jobs. Cron jobs require a config file. Follow this guide to create a Cronjob.
The example cron job below prints the current time and a hello message every first minute of every hour from 0200 to 1400 UTC on Sunday, Monday, Friday, and Saturday:
apiVersion: batch/v1
kind: CronJob
metadata:
name: hello
spec:
schedule: "1 2-14 * * 0-1,5-6"
successfulJobsHistoryLimit: 1
failedJobsHistoryLimit: 1
jobTemplate:
spec:
ttlSecondsAfterFinished: 100
template:
spec:
containers:
- name: hello
image: bitnami/nginx
args:
- /bin/sh
- -c
- date; echo "Hello, World!"
restartPolicy: OnFailure
Deploying a Cronjob to your Namespace
You need a yaml file to define your cronjob. You can either create your own, or use this one as an example. This periodically deletes untagged images in the ECR docker image repository (to ensure we stay within the number of images we are allowed).
kubectl apply --filename cronjob-ecr.yaml --namespace [your namespace]
Verify the Cronjob is created:
kubectl get cronjob --namespace [your namespace]
Clean up finished jobs
Jobs created by CronJobs are usually not needed after they have run to completion. Use the TTL mechanism to automatically clean them up by settting ttlSecondsAfterFinished
, to tell kubernetes when it can delete the completed Job.
We also have a delete-completed-jobs concourse job which will clean up all completed jobs which do not have ttlSecondsAfterFinished
defined.
Deleting completed Jobs cleans up the Pods they create, which helps the kubernetes cluster to use its CPU and memory resources efficiently. You can still see the logs of the deleted Pod in kibana.
Note: Kubernetes uses UTC exclusively. Make sure you take that into account when you’re creating your schedule or defining
ttlSecondsAfterFinished
.