How to Change the Time Zone in Kubernetes

In the previous post I’ve shown you how to change the time zone in a single Docker container or in your stack of Docker containers. But what if you’re already using Kubernetes for management of containerized applications?

First, let’s find out which time zone our pods are using: imagine we have the following pod description (by the way, you should not create the pod from the yaml file directly, use deployment instead)

apiVersion: v1
kind: Pod
metadata:
  name: busybox-sleep
spec:
  containers:
  - name: busybox
    image: busybox
    args:
    - sleep
    - "apiVersion: v1
kind: Pod
metadata:
  name: busybox-sleep
spec:
  containers:
  - name: busybox
    image: busybox
    args:
    - sleep
    - "1000000"

I think it’s obvious what it does - it runs sleep 1000000 command inside of a busybox pod. Just save this yaml file as pod_before.yaml and let’s create this pod and find out which time zone this pod is using:

[root@k8s-master02:] kubectl apply -f pod_before.yaml
pod "busybox-sleep" created

[root@k8s-master02:~/test_blog] kubectl exec busybox-sleep date
Thu Jun 14 12:38:46 UTC 2018

Heh, our dear UTC friend is back again. So as you can see it uses UTC time zone. Let’s change it - we will do it via adding the volumes to the pod:

apiVersion: v1
kind: Pod
metadata:
  name: busybox-sleep
spec:
  containers:
  - name: busybox
    image: busybox
    args:
    - sleep
    - "1000000"
    volumeMounts:
    - name: tz-config
      mountPath: /etc/localtime
  volumes:
    - name: tz-config
      hostPath:
        path: /usr/share/zoneinfo/Europe/Prague

I’ve added volumes and volumeMounts values to the yaml file, let’s save it as the pod_after.yaml and apply t again:

[root@k8s-master02:] kubectl apply -f pod_before.yaml
pod "busybox-sleep" changed

[root@k8s-master02] kubectl exec busybox-sleep date
Thu Jun 14 14:41:34 CEST 2018

Here we go, we have CEST time zone inside of the pod.

If you want to have it across all the Kubernetes pods you just need to add these volumes and volumeMounts values to your yaml files and don’t forget to change the path from /usr/share/zoneinfo/Europe/Prague to your time zone!