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

时间:2019-03-10 15:30:16

标签: kubernetes timezone command yaml

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

您知道如何应用命令吗?

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

在容器内效果很好。 但是我不知道在yaml文件中应用命令和参数。

4 个答案:

答案 0 :(得分:2)

在GCP Kubernetes上,如下设置TZ环境变量对我来说很好。

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

答案 1 :(得分:1)

您可以通过使用特定的时区配置和hostPath卷来设置特定的时区来更改pod的timezone。您的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

如果要在所有Pod中进行部署,则需要向所有部署文件中添加volume和volumeMounts并将path部分中的hostPath值更改为要设置的时区。

答案 2 :(得分:1)

要按照先前答案中的建议,在部署配置中添加“ 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"类似)。

答案 3 :(得分:0)

在部署中,可以通过在/ 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