Millet Porridge

English version of https://corvo.myseu.cn

0%

The CronJob Timezone Problem in Kubernetes

The earlier blog: Some Usage of Cron Jobs in Kubernetes introduced basic cron job usage. But during use I found a task scheduled for 8 a.m. executes in the afternoon — exactly 8 hours apart, so I suspected a timezone problem.

Documentation

Facing this problem I went straight to the docs: CronJob.

This document’s updates went back and forth for a long time; the doc kept agonizing over what the scheduled task’s execution references. The content below is a version updated after March 2020 — I won’t paste the earlier incorrect versions, only a correct one:

1
2
3
4
Caution:
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.

The Chinese translation of the doc had never been updated, so I helped update it too:

1
2
3
4
警告:
所有 CronJob 的 schedule: 时间都是基于初始 Job 的主控节点的时区。

如果你的控制平面在 Pod 或是裸容器中运行了主控程序 (kube-controller-manager), 那么为该容器设置的时区将会决定定时任务的控制器所使用的时区。

Solution

Since we learned the scheduled task’s scheduling relates to the control-manager container’s timezone, just consider modifying that container’s timezone. For maintainability, build a new image based on the original one.

1
2
3
4
5
6
FROM <dockerhub>/fengyuhao/kube-controller-manager:v1.17.0

ENV DEBIAN_FRONTEND noninteractive
RUN apt-get update && apt-get install -y tzdata
RUN ln -fs /usr/share/zoneinfo/Asia/Hong_Kong /etc/localtime
RUN dpkg-reconfigure -f noninteractive tzdata
1
docker build . -t <dockerhub>/fengyuhao/kube-controller-manager:hk-v1.17.0

Then for the kubernetes master machine, change the control-manager configuration; kubernetes will apply the new configuration automatically:

1
2
#  file location: /etc/kubernetes/manifests/kube-controller-manager.yaml
image: <dockerhub>/fengyuhao/kube-controller-manager:hk-v1.17.0

Please replace the corresponding dockerhub and image addresses yourselves.

You can use kubectl -n kube-system get pods to confirm the controller update.