Sub topic 7
Writing Schedules and Setting the Time Zone
This chapter assumes you can write a CronJob manifest (see the previous chapter) and apply it with kubectl. You don't need prior cron experience.
The five fields
A CronJob's spec.schedule is a standard cron expression with five space-separated fields.
Each field accepts these forms:
| Syntax | Meaning | Example |
|---|---|---|
| `*` | Every value | `* * * * *` runs every minute |
| `5` | One value | `5 * * * *` runs at minute 5 of every hour |
| `1,15` | A list | `0 0 1,15 * *` runs on the 1st and 15th |
| `1-5` | A range | `0 9 * * 1-5` runs at 09:00 Monday to Friday |
| `*/15` | A step | `*/15 * * * *` runs every 15 minutes |
Kubernetes also accepts the shortcuts @hourly, @daily, @weekly, @monthly, and @yearly. These run at the start of the period. For example, @daily is the same as 0 0 * * *.
Setting the time zone
By default, the schedule is read in the time zone of the kube-controller-manager. On managed clusters you often can't see or change that value. Set spec.timeZone to make the schedule explicit. It takes an IANA time zone name such as Europe/Berlin or America/New_York.
apiVersion: batch/v1
kind: CronJob
metadata:
name: weekday-digest
spec:
schedule: "0 9 * * 1-5"
timeZone: "Europe/Berlin"
jobTemplate:
spec:
template:
spec:
restartPolicy: OnFailure
containers:
- name: digest
image: busybox:1.36
command: ["sh", "-c", "date; echo sending digest"]This runs at 09:00 Berlin time on weekdays, in both winter and summer.
The API server rejects a timeZone value it doesn't recognize. Don't put CRON_TZ= or TZ= inside schedule, because that form is not supported. Use the timeZone field instead.
Common errors
- Day fields combine with OR. If you restrict both day of month and day of week, such as
0 0 13 * 5, the job runs on the 13th and on every Friday. It does not run only on Friday the 13th. - Daylight saving time shifts runs. Suppose the clocks skip 02:00 to 03:00. A job scheduled for 02:30 in that zone may not run that day. When clocks repeat an hour, a time in that hour can occur twice. Avoid hours between 01:00 and 03:00 for important jobs in zones that change clocks. You can also schedule in
Etc/UTC, which never shifts. - Step values don't carry over.
*/45in the minute field runs at minutes 0 and 45 of each hour. The gap between 45 and the next hour's 0 is only 15 minutes, not 45. - Schedules start from creation. A new CronJob doesn't run until the next matching time. It does not run immediately.
To see the schedule and time zone a CronJob uses, and when it last ran, run this:
kubectl get cronjob weekday-digest -o jsonpath='{.spec.schedule} {.spec.timeZone}{"\n"}'
kubectl get cronjob weekday-digestThe LAST SCHEDULE column shows the last time the controller created a Job.
Check your understanding
Exercise
Write the schedule and timeZone lines for a CronJob that runs at 06:00 on Monday through Friday in New York local time.
Show answer
schedule: "0 6 1-5" and timeZone: "America/New_York". The fields are minute 0, hour 6, any day of month, any month, and days of week 1 to 5.