23

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.

papanito
  • 2,349
  • 2
  • 32
  • 60
canerbis
  • 231
  • 1
  • 2
  • 6

5 Answers5

30

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.

Tony L
  • 23
  • 1
  • 6
Prafull Ladha
  • 12,341
  • 2
  • 37
  • 58
  • 1
    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 type` – Jolta Apr 25 '19 at 11:39
  • 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. – Mojtaba Apr 19 '20 at 16:13
30

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
rantoniuk
  • 1,083
  • 12
  • 18
  • 1
    So the environment variable TZ : Europe/Warsaw worked for me. – abbas Jan 07 '21 at 09:00
  • 1
    Thank you. It also works for me on Huawei Cloud Kubernetes – otidh Dec 17 '21 at 09:59
  • 1
    It did not work for me on Amzon EKS. Volume mounting answer above worked for me. – Babar Baig Feb 20 '22 at 10:38
  • 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 https://stackoverflow.com/questions/70968816/kubernetes-set-timezone-to-a-deployment?noredirect=1&lq=1 – Babar Baig Feb 20 '22 at 19:14
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 
pcampana
  • 2,413
  • 2
  • 21
  • 39
5

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").

Noam Manos
  • 15,216
  • 3
  • 86
  • 85
  • Thanks @StuartCharlton, I've updated solution (not only for Openshift) – Noam Manos Jul 09 '20 at 19:33
  • Option2 is the best approach, for security reasons hostPath is not allowed in most of the managed kubernetes platforms. – Govind Kailas Feb 18 '21 at 10:51
  • 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. – Abhinav Thakur Oct 30 '21 at 11:40
0

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