Kubernetes Pod与宿主机时区不同步

废柴阿尤
2021-12-19 / 0 评论 / 1,837 阅读 / 正在检测是否收录...
温馨提示:
本文最后更新于2023年12月11日,已超过271天没有更新,若内容或图片失效,请留言反馈。

问题

在Kubernetes集群中运行的容器默认会使用UTC时间,即北京时间为凌晨3点时,容器时间为晚上7点,中间会有8小时时差。而有些分布式系统对于时间极为敏感,不允许出现时间误差

这里我们构建一个Nginx镜像,查看构建前的时间

apiVersion: v1
kind: Pod
metadata:
  name: time-nginx
spec:
  containers:
  - name: time-nginx
    image: nginx
    args: [/bin/sh, -c,
            'i=0; while true; do echo "$i: $(date)"; i=$((i+1)); sleep 1; done']

创建完Pod后我们查看一下时间

[root@k8s-01 test]# kubectl  logs -f time 
337: Fri Dec  1 19:01:30 UTC 2021
338: Fri Dec  1 19:01:31 UTC 2021
339: Fri Dec  1 19:01:32 UTC 2021
340: Fri Dec  1 19:01:33 UTC 2021
341: Fri Dec  1 19:01:34 UTC 2021
342: Fri Dec  1 19:01:35 UTC 2021

查看一下宿主机时间

[root@k8s-01 ~]# date
Wed Dec 15 00:00:51 CST 2021

解决

首先要确保宿主机时间同步

timedatectl set-timezone Asia/Shanghai
 #将当前的 UTC 时间写入硬件时钟
timedatectl set-local-rtc 0
 #重启依赖于系统时间的服务
systemctl restart rsyslog 
systemctl restart crond

目前解决Pod和宿主机时间不一致有以下集中解决方案

  • 通过定制Dockerfile添加时区
  • 通过将时区文件挂在到Pod中
  • 通过环境变量定义时区
  • 进入容器内修改时区
  • 网上资料还有通过PodPreset的方式,但是我测试完毕之后没有效果~ 这里就不进行整理了

通过定制Dockerfile添加时区

$ cat Dockerfile.date
FROM centos
 
RUN rm -f /etc/localtime \
    && ln -sv /usr/share/zoneinfo/Asia/Shanghai /etc/localtime \
    && echo "Asia/Shanghai" > /etc/timezone
 
# 构建容器镜像
$ docker build -t centos7-date:test -f Dockerfile.date .
Sending build context to Docker daemon  4.426GB
Step 1/2 : FROM centos
 ---> 1e1148e4cc2c
Step 2/2 : RUN rm -f /etc/localtime && ln -sv /usr/share/zoneinfo/Asia/Shanghai /etc/localtime && echo "Asia/Shanghai" > /etc/timezone
 ---> Running in fe2e931c3cf2
'/etc/localtime' -> '/usr/share/zoneinfo/Asia/Shanghai'
Removing intermediate container fe2e931c3cf2
 ---> 2120143141c8
Successfully built 2120143141c8
Successfully tagged centos7-date:test
 
$ docker run -it centos7-date:test /bin/sh
sh-4.2# date
Wed Dec  6 16:40:01 CST 2021

通过将时区文件挂在到Pod中

[root@k8s-01 test]# cat time-mount.yaml 
apiVersion: v1
kind: Pod
metadata:
  name: time
spec:
  containers:
  - name: time
    image: nginx
    args: [/bin/sh, -c,
            'i=0; while true; do echo "$i: $(date)"; i=$((i+1)); sleep 1; done']
    volumeMounts:
      - name: timezone
        mountPath: /etc/localtime
  volumes:
    - name: timezone
      hostPath:
        path: /usr/share/zoneinfo/Asia/Shanghai

我们可以通过命令查看,/etc/localtime的目录实际上就是个软连接
如果需要系统修改时区,那么只需要将时区文件覆盖到/etc/localtime,前提是我们设置好上海的时区。

[root@k8s-01 test]# ll /etc/localtime
lrwxrwxrwx. 1 root root 35 Apr 20 00:11 /etc/localtime -> ../usr/share/zoneinfo/Asia/Shanghai

通过环境变量定义时区

[root@k8s-01 test]# cat time.yaml 
apiVersion: v1
kind: Pod
metadata:
  name: time-nginx
spec:
  containers:
  - name: time-nginx
    image: nginx
    args: [/bin/sh, -c,
            'i=0; while true; do echo "$i: $(date)"; i=$((i+1)); sleep 1; done']
    env:
    - name: TZ
      value: Asia/Shanghai
635

打赏

评论 (0)

取消