I want to show how to change the container’s timezone which runs on the Kubernetes cluster. Basically, when you push a docker image from the docker hub you can easily check which timezone already defined for the image. I just defined a basic YAML file, creates a container that sleeps “100000” seconds. We used BusyBox image.
BusyBox combines tiny versions of many common UNIX utilities into a single small executable. It provides replacements for most of the utilities you usually find in GNU fileutils, shellutils, etc.
Default TimeZone:
Step 1: Create a test POD definition file
#cat /tmp/defaulttz.yaml apiVersion: v1 kind: Pod metadata: labels: run: defaulttz name: defaulttz spec: containers: - image: busybox name: defaulttz args: - sleep - "100000" #kubectl create -f /tmp/defaulttz.yaml pod/defaulttz created #kubectl get pods NAME READY STATUS RESTARTS AGE defaulttz 1/1 Running 0 13s
Step 2: Deploy new POD without change
When you create a new Pod with a POD definition file, it performs some steps like pull image, create container and start container etc.
#kubectl describe pod defaulttz ......... ......... Type Reason Age From Message ---- ------ ---- ---- ------- Normal Scheduled <unknown> default-scheduler Successfully assigned default/defaulttz to node01 Normal Pulling 2m30s kubelet, node01 Pulling image "busybox" Normal Pulled 2m24s kubelet, node01 Successfully pulled image "busybox" Normal Created 2m24s kubelet, node01 Created container defaulttz Normal Started 2m23s kubelet, node01 Started container defaulttz
Now we can check the timezone variable for BusyBox image. First, you need to get a shell to the running container then run “date” command.
#kubectl exec defaulttz -it -- /bin/sh / # date Sat Dec 21 10:01:33 UTC 2019 / # ls -ld /etc/localtime -rw-r--r-- 1 root root 127 Sep 17 20:51 /etc/localtime / # strings /etc/localtime TZif2 TZif2 UTC0 / #
New TimeZone:
Step 1: Create new POD definition file with spesific timezone.
Let’s try to change this timezone variable from UTC to GMT+3. We defined two variables that “volumeMounts” and “volumes”. A hostPath
volume mounts a file or directory from the host node’s filesystem into your Pod. So we defined a new volume that shares the Timezone variable to the Pod. And we attached this volume to the container with volumeMounts method.
Connect one of the worker nodes that the container will schedule. You can define any timezone under this directory.
node01 $ ls -ld /usr/share/zoneinfo/* #cat /tmp/tzistanbul apiVersion: v1 kind: Pod metadata: name: tzistanbul spec: containers: - name: tzistanbul image: busybox args: - sleep - "1000000" volumeMounts: - name: tz-istanbul mountPath: /etc/localtime volumes: - name: tz-istanbul hostPath: path: /usr/share/zoneinfo/Europe/Istanbul
Step 2: Deploy new POD
Now it’s time to create POD.
#kubectl create -f /tmp/tzistanbul pod/tzistanbul created #kubectl get pods NAME READY STATUS RESTARTS AGE defaulttz 1/1 Running 0 13m tzistanbul 1/1 Running 0 4s
Get a shell to the running Container:
#kubectl exec tzistanbul -it -- /bin/sh
Step 3: Check TimeZone
Check if the container gets the timezone as you need.
/ # df -h /etc/localtime Filesystem Size Used Available Use% Mounted on /dev/vda1 44.1G 21.8G 20.0G 52% /etc/localtime / # ls -ld /etc/localtime -rw-r--r-- 1 root root 1956 Oct 3 00:06 /etc/localtime / # date Sat Dec 21 13:17:04 +03 2019 / # strings /etc/localtime TZif2 EEST TZif2 EEST <+03>-3 / #