Containers do not inherit timezones from host machines and have only accessed to the clock from the kernel - which is always UTC. The default timezone for most images is UTC, yet it is not guaranteed and may be different from container to container since it can be changed on a pod or image level.
You can set pod's timezone by mounting the UTC TZif file from the node machine to /etc/localtime
in the container. For example:
apiVersion: v1
kind: Pod
metadata:
name: date-pod-amsterdam
spec:
containers:
- image: ubuntu:21.04
name: ubuntu
args:
- date
volumeMounts:
- name: zoneinfo
mountPath: /etc/localtime
subPath: UTC
readOnly: true
volumes:
- name: zoneinfo
hostPath:
path: /usr/share/zoneinfo
restartPolicy: OnFailure
Sometimes, containers sets their timezone with TZ
environment variable which is prior to /etc/localtime
and it is required to set it to UTC
too.
spec:
containers:
- env:
- name: TZ
value: UTC
This process can be simplified by using k8tz
, its a kubernetes admission controller and a CLI tool to inject timezones into Pods. You can install it with helm easily and it will automatically sets those properties on any created pod in the cluster. By default (if not specified otherwise) it enforces UTC.
helm repo add k8tz https://k8tz.github.io/k8tz/
helm install k8tz k8tz/k8tz
DISCLAIMER: I am the author of k8tz.