Kubernetes设置Pod时区

在kubernetes集群中运行的容器默认会使用格林威治时间,即北京时间为12点时,容器时间为4点,而有些分布式系统对于时间是极为敏感的,不允许出现时间误差。

为了保持容器时间与宿主机时间同步,可以使用hostPath的方式将宿主机上的时区文件挂载到容器中。

比如当前宿主机的时区为Asia/Shanghai,那么用ll /etc/localtime时会显示链接到/usr/share/zoneinfo/Asia/Shanghai

[root@localhost ~]# ll /etc/localtime

lrwxrwxrwx. 1 root root 35 Jul 12 14:26 /etc/localtime -> ../usr/share/zoneinfo/Asia/Shanghai

如果需要系统修改时区,那么只需要将时区文件覆盖到/etc/localtime,如

[root@localhost ~]# cp -f /usr/share/zoneinfo/{{时区文件}} /etc/localtime

要更新容器中的时区也是用同样的方式,比方说下面的例子:

---
apiVersion: extensions/v1beta1
kind: Deployment
metadata:
  name: nginx
spec:
  template:
    metadata:
      labels:
        app: nginx
    spec:
      containers:
        - name: nginx
          image: 'nginx:latest'
          imagePullPolicy: IfNotPresent
          resources:
            requests:
              cpu: 100m
              memory: 100Mi
          ports:
            - containerPort: 80
          volumeMounts:
            - name: timezone
              mountPath: /etc/localtime
      volumes:
        - name: timezone
          hostPath:
            path: /usr/share/zoneinfo/Asia/Shanghai

这样相当于为nginx容器设置了上海时区,这样容器中的时间就会和宿主机保持一致,当然也可以使用其他的方法,只要能将时区文件更新到/etc/localtime即可

猜你喜欢

转载自my.oschina.net/u/3390908/blog/1580250