scheduled jobs with kubernetes cronjobs¶
what are scheduled jobs?¶
Scheduled Jobs are automated pieces of work that can be performed at a specific time or on a recurring schedule.
Example use cases:
- Sending performance metrics on weekly/monthly basis
- Analyse and send user behaviour metrics to admins on a daily basis
what are cronjobs?¶
- A cron job is a Linux command used for scheduling tasks to be executed sometime in the future.
- This is normally used to schedule a job that is executed periodically
- Syntax:
# ┌───────────── minute (0 - 59)
# │ ┌───────────── hour (0 - 23)
# │ │ ┌───────────── day of the month (1 - 31)
# │ │ │ ┌───────────── month (1 - 12)
# │ │ │ │ ┌───────────── day of the week (0 - 6) (Sunday to Saturday;
# │ │ │ │ │ 7 is also Sunday on some systems)
# │ │ │ │ │
# │ │ │ │ │
# * * * * *
Prerequisites¶
- How Kubernetes works and schedules containers as pods
- How Kubernetes manages objects and config in a declarative way
- The differences between a service, deployment, and Horizontal Pod Autoscaler
- Aware of usual Kubernetes terms like Node, Kubelet, and the likes.
kubernetes and cronjobs¶
- Kubernetes comes with objects called cronjobs which works similar to the linux cronjobs
- The timezone is taken from the k8s/kubernetes cluster
- Cronjobs are a wrapper around kubernetes jobs
simple configuration yaml file for kubernetes cronjob¶
apiVersion: batch/v1beta1
kind: CronJob
metadata:
name: simple-job-name
spec:
schedule: "5 * * * *"
concurrencyPolicy: Replace
jobTemplate:
spec:
template:
spec:
containers:
- name: hello
image: busybox
imagePullPolicy: IfNotPresent
command:
- /bin/sh
- -c
- date; echo "Hello from the Kubernetes cluster"
restartPolicy: OnFailure
- Above example is simple job which runs every 5 minutes and executes the command
- We can have multiple containers
concurrencyPolicy
can have valuesAllow
,Forbid
,Replace
Allow
(default): The cron job allows concurrently running jobsForbid
: The cron job does not allow concurrent runs; if it is time for a new job run and the previous job run hasn't finished yet, the cron job skips the new job runReplace
: If it is time for a new job run and the previous job run hasn't finished yet, the cron job replaces the currently running job run with a new job run
restartPolicy
can have valuesOnFailure
,Never
Notes to remember before scheduling the cronjob¶
- when
restartPolicy
is set toOnFailure
then it will be restarted if any of the containers failed. - It won't follow the sheduled time when it runs for failure.
- Make sure your command didn't create any issues if it runs at irregular intervals.
Commands to work with kubernets¶
- Create the kubernetes cronjob
kubectl apply -f cronjob.yaml -n namespace
- Get all scheduled cronjobs
kubectl get cronjobs -n namespace
-
suspend kubernetes cronjob
kubectl patch cronjobs <job-name> -p '{"spec" : {"suspend" : true }}' -n namespace
-
delete kubernetes cronjob
kubectl delete cronjobs <job-name> -n namespace
-
describe a cronjob
kubectl describe cronjobs <job-name> -n namespace
Ref: https://kubernetes.io/docs/concepts/workloads/controllers/cron-jobs/