5

I have a schedule: schedule: "0 10,14,18 * * *", I'd like to run this job in 10:00 am, 2:00pm, 6:00pm.

Since I located at UTC+8 timezone, this cronjob looks like not run as I expected.

Anyway to add a config for timezone?

xring
  • 727
  • 2
  • 8
  • 29
  • 3
    why not specify time in UTC in cronjob? Like "0 0 2,6,10 * *" – Vasili Angapov May 13 '19 at 03:15
  • In addition to what Vasily correctly pointed out, using timezone-based crontab would require that the cluster keep the [timezone database](https://www.iana.org/time-zones) up-to-date \[the latest update was in March, for example] with all the political theater that accompanies such a thing. That's not something traditionally associated with a container orchestration system – mdaniel May 13 '19 at 04:12

5 Answers5

5

In Kubernetes v1.25, you can enable the CronJobTimeZone feature gate and set timezone by setting spec.timeZone to a valid time zone name.

For example, setting spec.timeZone: "Australia/Melbourne" makes Kubernetes to interpret the schedule relative to the UTC+10 (STD) timezone.

You can refer to the official documentation for more detailed info.

starshine wang
  • 616
  • 1
  • 8
  • 13
4

From Kubernetes documentation:

Note: All CronJob schedule: times are based on the timezone of the master where the job is initiated.

You should be ok if you deploy your master in Hong Kong. GCP does not have a region in China (see here)

You may consider deploying Kubernetes on Aws. Aws has regions in Beijing, China and Ningxia, China (see here)

Or may be on Azure (see here)

With above setup in place, schedule: "0 10,14,18 * * *" should work

rajesh-nitc
  • 5,049
  • 3
  • 25
  • 33
  • 1
    PS: The quote from the documentation is no longer true. – dvdmn Feb 17 '21 at 15:30
  • 1
    It uses the time zone of kube controller manager. If the kube controller manager runs in a container, it will be running based on the localtime in that container, not the master/host OS. – Nilucshan Siva Mar 29 '22 at 14:20
4

If you are using managed GCP K8s, times are in UTC

Docs

Cmag
  • 14,946
  • 25
  • 89
  • 140
0

For vanila kubernetes you can fix static pod. Add block

    volumeMounts:
    - name: localtime
      mountPath: /etc/localtime
      readOnly: true
volumes:
  - hostPath:
    path: /etc/localtime
    name: localtime

This make kube-controller-manager running in same timezone with host.

  • `Caution: 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.` seems like the suggestion has been adapted – Markus Mar 30 '22 at 10:34
0

In v1.22, there is a way to do it.

set something like CRON_TZ=Asia/Tehran 0 0 * * * as .spec.schedule value

In addition, the CronJob schedule supports timezone handling, you can specify the timezone by adding "CRON_TZ=" at the beginning of the CronJob schedule, and it is recommended to always set CRON_TZ.

the document is at https://kubernetes.io/docs/concepts/workloads/controllers/cron-jobs/#:~:text=In%20addition%2C%20the%20CronJob%20schedule,the%20kube%2Dcontroller%2Dmanager.

maxisam
  • 21,975
  • 9
  • 75
  • 84