Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I specify cron timezone in k8s cron job?

According to documentation (https://kubernetes.io/docs/concepts/workloads/controllers/cron-jobs/) I can create cron job in k8s with specify timezone like: "CRON_TZ=UTC 0 23 * * *"

My deployment file is:

apiVersion: batch/v1
kind: CronJob
metadata:
  name: scheduler
spec:
  schedule: "CRON_TZ=UTC 0 23 * * *"
...

During the deploy I am getting an error:

The CronJob "scheduler" is invalid: spec.schedule: Invalid value: "CRON_TZ=UTC 0 23 * * *": Expected exactly 5 fields, found 6: CRON_TZ=UTC 0 23 * * *

Cron is working without perfectly timezone (schedule: "0 23 * * *")

Cluster version is: Kubernetes 1.21.2-do.2 - digitalocean.

What is wrong?

like image 667
yihereg819 Avatar asked Aug 27 '21 09:08

yihereg819


People also ask

How do I change the timezone in cron job Kubernetes?

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.

How do you specify the time zone in cron expression?

Thankfully, you can configure a specific timezone for your Cron job as follows: First, you need to export the TZ variable in your Shell script before any other Shell entries. Next, access your crontab and use the crontab environment variable CRON_TZ at the start of the crontab file.

What is the use of * * * * * In cron?

It is a wildcard for every part of the cron schedule expression. So * * * * * means every minute of every hour of every day of every month and every day of the week .

Does cron use local time or UTC?

Cron by default uses UTC time zone.


1 Answers

The CRON_TZ=<timezone> prefix won't be available yet, not until 1.22. The inclusion in the 1.21 release docs was an error.

Originally, the change adding the syntax was included for 1.22, but it appears someone got confused and moved the documentation over to 1.21. Supporting the CRON_TZ=<timezone> syntax is accidental, purely because the package used to handle the scheduling was recently upgraded to version 3, which added support for the syntax. The package is the key component that makes the syntax possible and is only part of 1.22.

like image 141
Martijn Pieters Avatar answered Sep 21 '22 17:09

Martijn Pieters