14
votes

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.

4

4 Answers

25
votes

You can change the timezone of your pod by using specific timezone config and hostPath volume to set specific timezone. You're 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 pod, deployment 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.

17
votes

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
7
votes

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 
3
votes

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