kubernetes timezone in POD with command and argument
Asked Answered
P

7

26

I want to change timezone with command. I know applying hostpath.

Could you know how to apply command ?

ln -snf /user/share/zoneinfor/$TZ /etc/localtime

it works well within container. But I don't know applying with command and arguments in yaml file.

Pellegrino answered 10/3, 2019 at 15:30 Comment(0)
F
37

Setting TZ environment variable as below works fine for me on GCP Kubernetes.

---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: demo
spec:
  replicas: 1
  selector:
    matchLabels:
        app: myapp
  template:
    metadata:
      labels:
        app: myapp
    spec:
      containers:
      - name: demo
        image: gcr.io/project/image:master
        imagePullPolicy: Always
        env:
            - name: TZ
              value: Europe/Warsaw
      dnsPolicy: ClusterFirst
      restartPolicy: Always
      terminationGracePeriodSeconds: 0
Funches answered 29/6, 2020 at 15:33 Comment(4)
So the environment variable TZ : Europe/Warsaw worked for me.Monody
Thank you. It also works for me on Huawei Cloud KubernetesMerous
It did not work for me on Amzon EKS. Volume mounting answer above worked for me.Evermore
So to make the environment variable work for me I had to install tzdata package since it was not installed in my case. Following questions was helpful #70969316Evermore
L
31

You can change the timezone of your pod by using specific timezone config and hostPath volume to set specific timezone. Your yaml file will look something like:

apiVersion: v1
kind: Pod
metadata:
  name: busybox-sleep
spec:
  containers:
  - name: busybox
    image: busybox
    args:
    - sleep
    - "1000000"
    volumeMounts:
    - name: tz-config
      mountPath: /etc/localtime
  volumes:
    - name: tz-config
      hostPath:
        path: /usr/share/zoneinfo/Europe/Prague
        type: File

If you want it across all pods or deployments, you need to add volume and volumeMounts to all your deployment file and change the path value in hostPath section to the timezone you want to set.

Ljubljana answered 11/3, 2019 at 5:46 Comment(2)
I'm not getting this to work on Kubernetes v1.10.11. container init caused \"rootfs_linux.go:58: mounting \\\"/etc/localtime\\\" to rootfs \\\"/var/lib/docker/overlay2/[id]/merged\\\" at \\\"/var/lib/docker/overlay2/[id]/merged/etc/localtime\\\" caused \\\"not a directory\\\"\"": unknown: Are you trying to mount a directory onto a file (or vice-versa)? Check if the specified host path exists and is the expected typePersuasive
I have got this to work on Kubernetes v1.17.4 and minikube v1.8.2. Just need to add the type: File in volumes config.Golliner
F
11

In a deployment, you can do it by creating a volumeMounts in /etc/localtime and setting its values. Here is an example I have for a mariadb:

apiVersion: apps/v1beta1
kind: Deployment
metadata:
  name: mariadb
spec:
  replicas: 1
  template:
    metadata:
      labels:
        app: mariadb
    spec:
      containers:
        - name: mariadb
          image: mariadb
          ports:
            - containerPort: 3306
              name: mariadb
          env:
            - name: MYSQL_ROOT_PASSWORD
              value: password
          volumeMounts:
          - name: tz-config
            mountPath: /etc/localtime
      volumes:
      - name: tz-config
        hostPath:
           path: /usr/share/zoneinfo/Europe/Madrid 
Faith answered 28/5, 2019 at 15:59 Comment(0)
D
6

In order to add "hostPath" in the deployment config, as suggested in previous answers, you'll need to be a privileged user. Otherwise your deployment may fail on:

"hostPath": hostPath volumes are not allowed to be used

As a workaround you can try one of theses options:

  1. Add allowedHostPaths: {} next to volumes.
  2. Add TZ environment variable. For example: TZ = Asia/Jerusalem

(Option 2 is similar to running docker exec -it openshift/origin /bin/bash -c "export TZ='Asia/Jerusalem' && /bin/bash").

Darsie answered 30/4, 2020 at 13:12 Comment(3)
Thanks @StuartCharlton, I've updated solution (not only for Openshift)Darsie
Option2 is the best approach, for security reasons hostPath is not allowed in most of the managed kubernetes platforms.Almost
I had been aimlessly trying to export external command and args through K8S manifest. All it took to make it work was using Noam's 2nd approach. Worked on a self managed cluster on AWS. Thanks @warden for present a nice YAML too.Odoriferous
M
1

Setting TZ in kubernetes deployment.yaml should fix this issue

      env:
        - name: TZ
          value: Europe/Oslo
Monasticism answered 21/11, 2023 at 10:41 Comment(1)
simple fix, works fine for me.Jackanapes
D
0

For me: Setting up of volumes and volumeMounts didn't help. Setting up of TZ environment alone works in my case.

Discreet answered 11/5, 2022 at 16:2 Comment(0)
P
0

For me the easiest way was to follow this article and write to to the Dockerfile:

https://dev.to/0xbf/set-timezone-in-your-docker-image-d22

TL;DR

RUN apt update && apt install tzdata -y
ENV TZ="America/New_York"
Patten answered 26/9, 2023 at 15:26 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.