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
[root@master ~]# kubectl get deployments
NAME                 READY   UP-TO-DATE   AVAILABLE   AGE
nginx                1/1     1            1           32s
[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              
  
[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
[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 2022Set 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
[root@master ~]# kubectl apply -f timezone-Nginx.yaml
pod/timezone-demo created
[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
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.