Kubernetes设置Pod时区

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

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

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

[root@localhost ~]# ll /etc/localtime 分布式

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

若是须要系统修改时区,那么只须要将时区文件覆盖到/etc/localtime,如code

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

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

---
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便可kubernetes

相关文章
相关标签/搜索