'Kubernetes cluster default timezone?

I had a question about the timezone used by my Kubernetes Cluster. I know I can adjust the timezone of the pods(https://evalle.xyz/posts/kubernetes-tz/).

However, I want to make sure my Cluster always uses UTC in the time zone. Is this a default option or can it change over time?



Solution 1:[1]

Have a look at the documentation Using Container-Optimized OS:

Container-Optimized OS is the default node OS Image in Kubernetes Engine and other Kubernetes deployments on Google Cloud Platform.

then move to the Changing the time zone for Container-Optimized OS:

The default time zone of Container-Optimized OS is UTC0.

and

Note that /etc is stateless, so the timezone will be reset to the default (UTC0) every reboot.

So, if you don't change Image type for your nodes from default Container-Optimized OS to Ubuntu you have nothing to do with time zone settings.

In addition, I've checked on my cluster:

$ date
Tue Feb  4 09:15:51 UTC 2020
$ ls -l /etc/ | grep localtime
lrwxrwxrwx 1 root root    25 Jan 29 08:37 localtime -> ../usr/share/zoneinfo/UTC

Solution 2:[2]

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.

Solution 3:[3]

Looks k8tz is nice, I just tried it, the question, could we control using the k8tz over the pods getting created on specific namespaces not over all namespaces within my k8s clusters.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2 Yonatan Kahana
Solution 3 user19080904