Containers timezone not consistent with host

I find that although host timezone is set to asia/shanghai, container timezone is still incorrect. Is there way to synchronize these two? thank you.

Cluster information:

Kubernetes version: 1.21
Cloud being used: (put bare-metal if not on a public cloud) bare metal
Installation method:
Host OS: ubuntu 18.04
CNI and version:
CRI and version:

Should just need to fix /etc/localtime in your container images.

Something like this probably.

RUN rm -f /etc/localtime \
&& ln -sv /usr/share/zoneinfo/Asia/Shanghai /etc/localtime \
&& echo "Asia/Shanghai" > /etc/timezone

Timezone stuff is just superficial though. Most of the base images I checked are on UTC-0 by default. If you work between multiple data centers, it makes more sense to just use UTC-0 everywhere and coordinate on that time.

There are some base images like ubuntu:20.04 that don’t even ship with tzdata installed.

Thank you very much for reply.

Modify Dockerfile and fix timezone works for me.

I am curious why k8s does not manage /etc/localtime just like /etc/resolv.conf?

An opinionated handling of DNS resolution is a necessity, because you can’t connect to things inside (and maybe outside) of the cluster without this opinion. It’s arguable that everyone needs this and it should be standardized.

Timezones are superficial and it’s not necessary to set it to anything specific for applications to work. If you aggregate your log data somewhere, tools like Kibana will expect it to be in UTC anyway (I think this is because timestamps in Elasticsearch are UTC, don’t quote me on this, I’ve not messed with ELK stacks in a while).

Generally speaking, you can normalize timezone data later from wherever application you have processing it later.

it might be true for most applications but we also run virtual machines in container which kind of making it trickier because some virtual machine prefer to use loal time…

Are you using Kata containers and the virtual bios time is causing the time in the container to be wrong?

you can use k8tz to automate timezone settings to all pods running in your cluster. you can also set different timezones for specific namespaces or pods using annotations.

here’s an example how to install k8tz with Europe/London as default timezone, and apply annotation to set UTC as the default timezone for namespace special-namespace.

helm repo add k8tz https://k8tz.github.io/k8tz/
helm install k8tz k8tz/k8tz --set timezone=Europe/London
kubectl annotate namespace special-namespace k8tz.io/timezone=UTC