Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Kubernetes Cronjob schedule in local timezone (custom timezone) as it always UTC

I want to schedule kubernetes cronjob in my local timezone (GMT+7), currently when I schedule cronjob in k8s I need to schedule in UTC but I want to schedule in my local timezone, As specify in Kubernetes document, that I need to change timezone in kube-controller manager as follows

All CronJob schedule: times are based on the timezone of the kube-controller-manager.

If your control plane runs the kube-controller-manager in Pods or bare containers, the timezone set for the kube-controller-manager container determines the timezone that the cron job controller uses.

But I can't find a way to set timezone for kube-controller-manager, I'm using Kuberenetes on-premise v1.17, I found controller manager manifest file in - /etc/kubernetes/manifests/kube-controller-manager.yaml but can't find a way to or document to change the it's timezone.

like image 521
Teerakiat Chitawattanarat Avatar asked Dec 31 '25 10:12

Teerakiat Chitawattanarat


2 Answers

Now, if you use Kubernetes version equal or more than 1.25 can use the field timeZone to specify a time zone for a CronJob.

spec:
  timeZone: "Asia/Bangkok"
  schedule: "0 17 * * *"

But if Kubernetes version < 1.25 and >= 1.21 you can fix the timezone in line with the schedule field.

spec:
  schedule: "CRON_TZ=Asia/Bangkok 0 17 * * *"

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

TimeZone list: https://www.ibm.com/docs/en/cloudpakw3700/2.3.0.0?topic=SS6PD2_2.3.0/doc/psapsys_restapi/time_zone_list.html

like image 76
Thanawat Avatar answered Jan 02 '26 08:01

Thanawat


UPDATE 2023: WARNING OLD SYNTAX. See @Thanawat's answer, the official syntax has changed from what is shown in this answer.


spec:
  schedule: "CRON_TZ=America/New_York */5 * * * *"
spec:
  schedule: "CRON_TZ=Etc/UTC */5 * * * *"

By an "accident of implementation" timezone support was added to kubernetes cron scheduler.

available in v1.22 (next release)
Note: v1.22 should be released by end-of-year, Oct/Nov 2021

  • https://github.com/kubernetes/kubernetes/issues/47202#issuecomment-901294870
    • warning: very long issue thread
  • mentioned in https://kubernetespodcast.com/episode/160-keda/
  • this accidental feature snuck in when a dependency was updated
  • warning: not entirely clear if k8s maintainers might remove/block this feature instead of supporting it
    • unit tests to cover the new user-facing feature have not yet been merged as of 2021-08-31: https://github.com/kubernetes/kubernetes/pull/104404
    • static TZ or daylight savings schedule is one thing, but TZ and DST schedules change over time
like image 44
yzorg Avatar answered Jan 02 '26 06:01

yzorg



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!