Howto Use the Host TimeZone in Kubernetes PODs

The timezone setting for a docker container comes from the docker image. Quite often the timezone settings of the docker image and the docker host will not be the same. This creates issues in production deployments especially w.r.to correlating container and host logs.

My colleague Christy has put up a nice blog describing the steps required to set up the docker container timezone same as the host. The blog can be found here.

The above solution will work perfectly fine for a standalone docker environment. In this article let’s see how you can achieve the same thing in a Kubernetes environment. Note that the solution described here works for both Intel and PowerPC architectures.

All Linux distributions honours the ‘/etc/localtime’ file settings for timezone. Get the local timezone settings by running the following command:

# ls -l /etc/localtime
lrwxrwxrwx. 1 root root 32 Oct 15  2015 /etc/localtime -> ../usr/share/zoneinfo/Asia/Kolkata

As we can see from the above output, the host timezone is Asia/Kolkata. We’ll use the specific┬átimezone config file and leverage hostPath volume mechanism to set the appropriate timezone for the PODs. Here is an example POD yaml file which sets the timezone to the host timezone ie, Asia/Kolkata

apiVersion: "v1"
kind: "Pod"
metadata:
 name: "webpod"
 labels:
   name: "web"
spec:
    containers:
    - name: "web"
      image: "ppc64le/web"
      imagePullPolicy: "IfNotPresent"
      command: ["apache2ctl", "-D", "FOREGROUND"]
      env:
         - name: MYSQL_USER
           value: test
         - name: MYSQL_PASSWORD
           value: test
         - name: MYSQL_DB
           value: BucketList
      ports:
         - containerPort: 80
      volumeMounts:
      - name: tz-config
        mountPath: /etc/localtime
    volumes:
      - name: tz-config
        hostPath:
           path: /usr/share/zoneinfo/Asia/Kolkata

And that’s all. The POD will use the same timezone as the host. Hope you find this useful.

Pradipta Kumar Banerjee

I'm a Cloud and Linux/ OpenSource enthusiast, with 16 years of industry experience at IBM. You can find more details about me here - Linkedin

You may also like...