7

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?

sumeyyeemir
  • 215
  • 2
  • 3
  • 13
  • it feels like k8s team lives on a flat earth with no timezones. cronjob tasks run based on UTC0 too. – dvdmn Feb 18 '21 at 03:48

3 Answers3

7

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
Serhii Rohoza
  • 4,287
  • 2
  • 16
  • 29
6

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.

  • 1
    Thanks, Yonatan. Your hem repo k8tz worked for me. It's also a good idea to run `helm repo update` after adding it. – nkmuturi Oct 19 '22 at 12:30
0

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.

  • As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community May 11 '22 at 15:55