天天看点

[问题已处理]-k8s时间正常但无法触发定时任务

导语:最近k8s测试环境一直有一个问题。定时任务时区不对,但是进入容器date -R是没问题的。在node节点上手动跑jar进程,定时任务可以触发,在该节点上运行pod 无法触发定时任务。

尝试挂载volome /etc/localtime 无法解决

volumeMounts:
    -  name: config
       mountPath: /etc/localtime
       readOnly: true
   volumes:
    - name: config
       hostPath:
           path: /etc/localtime
           

尝试添加env 无法解决

env:
    - name: TZ
      value: Asia/Shanghai
           

后面尝试在构建容器的dockerfile时加入一条 解决了这个问题

# 设置时区
echo 'RUN ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime  && echo "Asia/Shanghai" > /etc/timezone ' >> ./Dockerfile
           

原因是/etc/localtime是用来描述本机时间,而 /etc/timezone是用来描述本机所属的时区。猜测由于pod里没有/etc/timezone导致定时任务无法运行。

继续阅读