[k8s] Setup timezone on cronjob
Standard time and daylight time have been a nightmare for cron configuration in Kubernetes. Fortunately it is an old nightmare thanks to the cronjob .spec.timeZone configuration
ℹ️
Any resemblance to real events and/or to real persons, living or dead, is purely coincidental
⏳ Once upon a time, there was a poor guy who had to configure a fake meeting reminder to edit the UTC Kubernetes cronjob configuration two times in a year:
- on standard time start
- on the switch to the daylight time
All that stuff was to guarantee that a routine/sw was running at a specific local time (ex. Europe/Rome
)
But since 1.27
kubernetes version, this is finally an old nightmare since .spec.timeZone
(talking about CronJob
resource) has promoted to GA.
FEATURE STATE: Kubernetes v1.27 [stable]
For CronJobs with no time zone specified, the kube-controller-manager interprets schedules relative to its local time zone.
You can specify a time zone for a CronJob by setting.spec.timeZone
to the name of a valid time zone. For example, setting.spec.timeZone: "Etc/UTC"
instructs Kubernetes to interpret the schedule relative to Coordinated Universal Time.
A time zone database from the Go standard library is included in the binaries and used as a fallback in case an external database is not available on the system.
CronJob limitations
Unsupported TimeZone specification
Specifying a timezone usingCRON_TZ
orTZ
variables inside.spec.schedule
is not officially supported (and never has been).
Starting with Kubernetes 1.29 if you try to set a schedule that includesTZ
orCRON_TZ
timezone specification, Kubernetes will fail to create the resource with a validation error. Updates to CronJobs already usingTZ
orCRON_TZ
will continue to report a warning to the client.