10

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.

Jonas
  • 121,568
  • 97
  • 310
  • 388

3 Answers3

5

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

yzorg
  • 4,224
  • 3
  • 39
  • 57
  • [Alternative mentioned on GH thread](https://github.com/kubernetes/kubernetes/issues/47202#issuecomment-859283642) "Argo Cron Workflow" not to be confused with full "Argo Workflow" which are more complex. – yzorg Aug 31 '21 at 19:36
  • 1
    Seems like official K8s documentation is warning from using it in production: `The v1 CronJob API does not officially support setting timezone as explained above. Setting variables such as CRON_TZ or TZ is not officially supported by the Kubernetes project. CRON_TZ or TZ is an implementation detail of the internal library being used for parsing and calculating the next Job creation time. Any usage of it is not recommended in a production cluster.` https://kubernetes.io/docs/reference/kubernetes-api/workload-resources/cron-job-v1/ – Markus Mar 30 '22 at 11:04
  • There is an outstanding issue to add official support for such a feature: https://github.com/kubernetes/enhancements/issues/3140. Notably, it seems the maintainers may plan to block the workaround mentioned in this answer: https://github.com/kubernetes/enhancements/issues/3140#issuecomment-1439241990 – bekfen Apr 26 '23 at 16:49
  • 1
    @bekfen My devops team told me recently official support for non-host timezones were coming (officially supported). I think the final version might use different prop for the TZ name instead of in the `schedule:` line. – yzorg Apr 27 '23 at 13:49
5

Now, if you use Kubernetes version 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

Thanawat
  • 51
  • 1
  • 4
1

It uses the system timezone, whatever that happens to be. Pretty much all server images default to UTC these days, but run date +"%Z %z" to check. Also remember that if you run kcm in a container, what actually matters is the localtime in that container, not the host OS. How you would change it depends on your OS, usually it's either something in systemd land or making a symlink from /etc/localtime to the right tzdata file.

I would not recommend doing this.

coderanger
  • 52,400
  • 4
  • 52
  • 75
  • My environment run kcm in container, in the container it shows UTC, is there anyway to set kubernetes manifest file to link /etc/localtime in the container to specific timezone so that kcm will run on my timezone? – Teerakiat Chitawattanarat Nov 07 '20 at 23:50
  • You would have to build your own container for it, with a localtime symlink in place. But to repeat you shouldn't do this, there is a reason you're finding this hard, it's really not a thing you are expected to do. – coderanger Nov 08 '20 at 09:14
  • I end-up using UTC. Thanks – Teerakiat Chitawattanarat May 29 '21 at 00:54