How to set timezone for Kubernetes CronJobs?

--

The time zone feature for CronJobs was first introduced as alpha in version 1.24. Afterwards, the beta version was released in Kubernetes version 1.25. Finally, a stable version was released in Kubernetes 1.27 recently.

For CronJobs with no time zone specified, the kube-controller-manager interprets schedules relative to its local time zone.

With this feature, we can specify different time zones for different CronJob definitions. We can also prevent it from running in the wrong timezones because of kube-controller-manager.

The following CronJob definition will run every day at 23:00 in Istanbul, Turkey, regardless of the time configuration of the Kubernetes cluster.


apiVersion: batch/v1
kind: CronJob
metadata:
name: timezone-test
spec:
timeZone: 'Asia/Istanbul' #You can change this line with any timezone
schedule: "0 23 * * *"
jobTemplate:
spec:
template:
spec:
containers:
- name: timezone-test
image: busybox:1.28
imagePullPolicy: IfNotPresent
command:
- /bin/sh
- -c
- date; echo Selam!
restartPolicy: OnFailure

You can find the list of time zones you can use from the link below.

Valid TZ’s: https://en.wikipedia.org/wiki/List_of_tz_database_time_zones

I hope you enjoyed the article. See you next time :)

Resources:

https://github.com/kubernetes/enhancements/issues/3140

https://kubernetes.io/docs/concepts/workloads/controllers/cron-jobs/#time-zones

--

--