Thursday, April 28, 2022

Kubernetes: Set Local Time Zone on Deployments and Pods

  In Kubernetes Pods, the time zone is set to UTC by default and if you require logs and data to have precise local time then we need to make changes to the pods or deployments. In this post we will be changing the date and time to our local zone Asia/Kathmandu. 

Set Time Zone on Deployments

First we are going to deploy an Nginx image and make changes to the deployment.

[root@master ~]#  kubectl create deployment nginx --image=nginx
deployment.apps/nginx created
The deployment has been created and we can verify as following.
[root@master ~]# kubectl get deployments
NAME                 READY   UP-TO-DATE   AVAILABLE   AGE
nginx                1/1     1            1           32s
The pod has also been created.
[root@master ~]# kubectl get pods -n default
NAME                                  READY   STATUS    RESTARTS            AGE
nginx-6799fc88d8-lnpqt                1/1     Running   0                   81s

[root@master ~]# kubectl get pod nginx-6799fc88d8-lnpqt -n default -o wide
NAME                     READY   STATUS    RESTARTS   AGE    IP           NODE       NOMINATED NODE   READINESS GATES
nginx-6799fc88d8-lnpqt   1/1     Running   0          110s   10.42.2.37   worker01              
We need to change the local timezone as below i.e. Asia/Kathmandu. The /etc/localtime information is of the master node.

[root@master ~]# file /usr/share/zoneinfo/Asia/Kathmandu
/usr/share/zoneinfo/Asia/Kathmandu: timezone data, version 2, 3 gmt time flags, 3 std time flags, no leap seconds, 2 transition times, 3 abbreviation chars


[root@master ~]# ll /etc/localtime
lrwxrwxrwx. 1 root root 36 Dec 28 19:24 /etc/localtime -> ../usr/share/zoneinfo/Asia/Kathmandu
We edit the deployment and add environment variable TZ with value "Asia/Kathmandu".
[root@master ~]# kubectl edit deployment nginx
spec:
  progressDeadlineSeconds: 600
  replicas: 1
  revisionHistoryLimit: 10
  selector:
    matchLabels:
      app: nginx
  strategy:
    rollingUpdate:
      maxSurge: 25%
      maxUnavailable: 25%
    type: RollingUpdate
  template:
    metadata:
      creationTimestamp: null
      labels:
        app: nginx
    spec:
      containers:
      - env:
        - name: TZ
          value: Asia/Kathmandu
        image: nginx
        imagePullPolicy: IfNotPresent
        name: nginx
        resources: {}

[root@master ~]# kubectl exec  -it nginx-8766wj383u-grxpq -- date
Wed Apr 27 14:45:43 +0545 2022
We can verify above that the time zone has been changed to +05:45 GMT after adding the TZ environment variable. 

 Set Time Zone on Pods

Below we have created a YAML file for POD deployment with Nginx image and also set environment
variable named TZ with value "Asia/Kathmandu".
[root@master ~]# cat timezone-Nginx.yaml
apiVersion: v1
kind: Pod
metadata:
  name: timezone-demo
  labels:
    purpose:  demonstrate-localpodtime
spec:
  containers:
  - name: nginx
    image: nginx
    env:
    - name: TZ
      value:  Asia/Kathmandu
Now we apply the YAML file and deploy the POD.
[root@master ~]# kubectl apply -f timezone-Nginx.yaml
pod/timezone-demo created
We verify the POD deployment as below where timezone-demo is the POD name. Then we verify the timezone and date/time.
[root@master ~]# kubectl get pod/timezone-demo
NAME            READY   STATUS    RESTARTS   AGE
timezone-demo   1/1     Running   0          10s      

[root@master ~]# kubectl exec  -it timezone-demo  -- date
Wed Apr 27 15:05:43 +0545 2022
Thus, we have successfully changed the timezone to the timezone as desired. 

We can also change the timezone using configmap and volumes.
kubectl create configmap tz --from-file=/usr/share/zoneinfo/Asia/Kathmandu
  
[root@master ~]# cat timezone-Nginx.yaml
apiVersion: v1
kind: Pod
metadata:
  name: timezone-demo
  labels:
    purpose:  demonstrate-localpodtime
spec:
  containers:
  - name: nginx
    image: nginx
    volumeMounts:
      - name: tz
        mountPath: /etc/localtime
    volumes:
    - name: tz
      configMap:
        name: tz  

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.