xring
xring

Reputation: 767

Kubernetes run cronjob in wrong time

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?

Upvotes: 8

Views: 22712

Answers (5)

starshine wang
starshine wang

Reputation: 636

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.

Upvotes: 7

maxisam
maxisam

Reputation: 22705

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.

Upvotes: 1

Sergey Bespalov
Sergey Bespalov

Reputation: 21

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.

Upvotes: 0

Cmag
Cmag

Reputation: 15750

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

Docs

Upvotes: 5

rajesh-nitc
rajesh-nitc

Reputation: 5529

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

Upvotes: 5

Related Questions