Kubernetes - 为部署设置时区

Kubernetes - Set timezone to a deployment

我需要部署具有 Europe/Rome 时区的应用程序。

我应用了以下部署:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: myapp
  labels:
    app: myapp
spec:
  replicas: 1
  selector:
    matchLabels:
      app: myapp
  template:
    metadata:
      labels:
        app: myapp
    spec:
      containers:
        - name: myapp
          image: 10.166.23.73:5000/local/myapp:latest
          imagePullPolicy: Always
          ports:
            - containerPort: 8080
          env:
            - name: TZ
              value: Europe/Rome
          volumeMounts:
          - name: tz-rome
            mountPath: /etc/localtime
      volumes:
        - name: tz-rome
          hostPath:
            path: /usr/share/zoneinfo/Europe/Rome

但是,当我在 POD 中 运行“日期”命令时,我没有得到“Europe/Rome”时区...

上面的部署yaml有什么问题?

如果删除 env 变量,应该可以。例如:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: myapp
  namespace: test-timezone
  labels:
    app: myapp
spec:
  replicas: 1
  selector:
    matchLabels:
      app: myapp
  template:
    metadata:
      labels:
        app: myapp
    spec:
      containers:
        - name: myapp
          image: busybox
          imagePullPolicy: Always
          command: [ "sleep", "10000" ]
          volumeMounts:
          - name: tz-rome
            mountPath: /etc/localtime
      volumes:
        - name: tz-rome
          hostPath:
            path: /usr/share/zoneinfo/Europe/Rome

输出:

/ # date
Fri Feb  4 02:16:16 CET 2022

如果要使用TZ环境设置时区,需要容器中有tzdata包,例如:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: myapp3
  namespace: test-timezone
  labels:
    app: myapp
spec:
  replicas: 1
  selector:
    matchLabels:
      app: myapp
  template:
    metadata:
      labels:
        app: myapp
    spec:
      containers:
        - name: myapp
          image: nginx
          imagePullPolicy: Always
          command: [ "sleep", "10000" ]
          env:
            - name: TZ
              value: Europe/Rome

Nginx里面有tzdata包:

root@myapp2-6f5bbdf56-nnx66:/# apt list --installed | grep tzdata

WARNING: apt does not have a stable CLI interface. Use with caution in scripts.

tzdata/now 2021a-1+deb11u2 all [installed,local]

root@myapp2-6f5bbdf56-nnx66:/# date
Fri Feb  4 02:32:48 CET 2022