带有命令和参数的 POD 中的 kubernetes 时区

can*_*bis 11 timezone command yaml kubernetes

我想用命令更改时区。我知道应用主机路径。

你知道如何应用命令吗?

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

它在容器内运行良好。但我不知道在 yaml 文件中使用命令和参数。

Pra*_*dha 22

您可以timezone通过使用特定时区配置和 hostPath 卷来设置特定时区来更改pod。您的 yaml 文件将类似于:

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
Run Code Online (Sandbox Code Playgroud)

如果您希望它跨所有 pod,部署,您需要将 volume 和 volumeMounts 添加到所有部署文件,并将部分中的path值更改为hostPath您要设置的时区。


war*_*den 11

如下设置 TZ 环境变量对我来说在 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
Run Code Online (Sandbox Code Playgroud)


pca*_*ana 7

在部署中,您可以通过在 /etc/localtime 中创建一个 volumeMounts 并设置其值来实现。这是我为 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 
Run Code Online (Sandbox Code Playgroud)


Noa*_*nos 6

为了按照前面的答案中的建议在部署配置中添加“hostPath”,您需要成为特权用户。否则您的部署可能会失败:

“hostPath”:不允许使用hostPath卷

作为解决方法,您可以尝试以下选项之一:

  1. 添加allowedHostPaths: {}到卷旁边。
  2. 添加TZ环境变量。例如:TZ = Asia/Jerusalem

(选项 2 与运行类似docker exec -it openshift/origin /bin/bash -c "export TZ='Asia/Jerusalem' && /bin/bash")。