How to set timezone for pods in OpenShift Container Platform?

Solution Verified - Updated -

Environment

  • Red Hat OpenShift Container Platform (RHOCP)
    • 3.x
    • 4.x
  • RedHat OpenShift Dedicated Cluster
  • RedHat OpenShift Service on AWS.

Issue

  • Timezone issues with application logs in OpenShift.
  • Why is the application log timezone different then the timezone set in OpenShift?

Resolution

Timezone is set based on the base container image. At the time of this writing, using the worker node as source is not done automatically by OpenShift. However, a Request for Feature Enhancement (RFE) has been filed to enable this functionality. This RFE is being tracked in Red Hat RFE list: RFE-1283.

To change the timezone of existing deployments, the following workarounds are available:

Workarounds

Workarounds listed below, be aware some of them set on the deployment/deployment config yaml, therefore are non-applicable for
most Operator based applications, which controls the deployment/deployment config/statefulset.

Workaround 1: Set an environment variable. This sets TZ in any containers to the timezone specified.

$ oc get deployments  
OR
$ oc get deploymentconfig
...
$ oc set env deployments/dc_name TZ=Asia/Tokyo 
OR
$ oc set env deploymentconfig/dc_name TZ=Asia/Tokyo 

Note: Deployments with parent ubi8-minimal image does not reflect correct TZ environment variable value. Kindly refer Why is /usr/share/zoneinfo empty in ubi8-minimal images despite the tzdata package being present? for more information.

Workaround 2: Mount /etc/localtime to use the same timezone in host machine using the "host path" volume mount method as described in the OpenShift Dev Guide on Volumes.

Workaround 3: Mount /etc/localtime to use the timezone stored in a configmap.

$ oc create configmap tz-london --from-file=localtime=/usr/share/zoneinfo/Europe/London
$ oc set volumes deployments/dc_name --add \
        --type=configmap --name=tz --configmap-name=tz-london \
        --mount-path=/etc/localtime --sub-path=localtime

Diagnostic Steps

Pod logs are reported, when using --timestamps, in current local time - 1, which might be a summer/daylight savings time related issue:

$ oc logs pod_name --timestamps
$ oc rsh pod_name
$ env | grep TZ    --> to confirm the updated timezone

This solution is part of Red Hat’s fast-track publication program, providing a huge library of solutions that Red Hat engineers have created while supporting our customers. To give you the knowledge you need the instant it becomes available, these articles may be presented in a raw and unedited form.

Comments