【转载】K8s-Pod时区与宿主时区时区同步
2020-12-28 09:54
127 查看
【原文】https://www.cnblogs.com/gdut1425/p/13560518.html
一、问题背景
容器默认的时区采用的是世界时钟即UTC时区,但是宿主机默认采用的是CST,两者时区相差8小时,如下所示,前者为容器的时间,后者为宿主机时间,容器的时间比宿主机的时间早8小时
[root@k8s-master zhanglei]# kubectl exec -ti myapp-statefulset-0 -- sh / # date Tue Aug 25 06:50:29 UTC 2020 / # exit [root@k8s-master zhanglei]# date 2020年 08月 25日 星期二 14:50:37 CST
CST应该是指(China Shanghai Time,东八区时间)
UTC应该是指(Coordinated Universal Time,标准时间)
在某些时间敏感的场景,如日志定位问题,为了方便,需要实现容器时间和宿主机时间保持一致。
二、解决方案
1、通过将宿主机的时区文件挂载到Pod
[root@k8s-master zhanglei]# cat dep-ord.yaml apiVersion: apps/v1 kind: Deployment metadata: name: nginx-dep-timesys namespace: default labels: app: nginx-deployment spec: replicas: 2 selector: matchLabels: app: nginx-deployment-ord-time template: metadata: labels: app: nginx-deployment-ord-time spec: containers: - name: nginx image: nginx:latest ports: - containerPort: 80 volumeMounts: - name: timezone mountPath: /etc/localtime # 挂载到容器的目录 volumes: - name: timezone hostPath: path: /usr/share/zoneinfo/Asia/Shanghai # 宿主机的目录
[root@k8s-master zhanglei]# kubectl get pod |grep time nginx-dep-timesys-6c7b6f86c6-2dlp7 1/1 Running 0 22m nginx-dep-timesys-6c7b6f86c6-vdzwh 1/1 Running 0 22m [root@k8s-master zhanglei]# kubectl exec -it nginx-dep-timesys-6c7b6f86c6-2dlp7 -- bash root@nginx-dep-timesys-6c7b6f86c6-2dlp7:/# date Tue Aug 25 14:57:01 CST 2020 root@nginx-dep-timesys-6c7b6f86c6-2dlp7:/# exit exit [root@k8s-master zhanglei]# date 2020年 08月 25日 星期二 14:57:07 CST
将宿主机的区间文件通过mountPath挂载到容器之后,可以看到两者时区已经保持了一致。
2、通过环境变量
[root@k8s-master zhanglei]# cat dep-test.yaml apiVersion: apps/v1 kind: Deployment metadata: name: nginx-deployment-timeenv namespace: default labels: app: nginx-deployment spec: replicas: 1 selector: matchLabels: app: nginx-deployment template: metadata: labels: app: nginx-deployment spec: containers: - name: nginx image: nginx:latest ports: - containerPort: 80 env: - name: TZ value: Asia/Shanghai # 宿主机的环境变量
root@nginx-deployment-timeenv-7b6d4c8b5-kbj9q:/# date Tue Aug 25 19:03:09 CST 2020 root@nginx-deployment-timeenv-7b6d4c8b5-kbj9q:/# exit exit [root@k8s-master zhanglei]# date 2020年 08月 25日 星期二 19:03:18 CST
可以看到容器时间和宿主机的时间保持了一致
3、通过dockfile镜像构建来同步容器和宿主机的时区
[root@k8s-master zhanglei]# touch testdockertime [root@k8s-master zhanglei]# vim testdockertime
[root@k8s-master zhanglei]# cat testdockertime FROM nginx RUN rm -f /etc/localtime \ && ln -sv /usr/share/zoneinfo/Asia/Shanghai /etc/localtime \ && echo "Asia/Shanghai" > /etc/timezone [root@k8s-master zhanglei]# docker build -t nginx:testdate -f testdockertime . Sending b 199f uild context to Docker daemon 82.43kB Step 1/2 : FROM nginx ---> 4bb46517cac3 Step 2/2 : RUN rm -f /etc/localtime && ln -sv /usr/share/zoneinfo/Asia/Shanghai /etc/localtime && echo "Asia/Shanghai" > /etc/timezone ---> Running in 29c9ffc202e6 '/etc/localtime' -> '/usr/share/zoneinfo/Asia/Shanghai' Removing intermediate container 29c9ffc202e6 ---> d3cde57b8afc Successfully built d3cde57b8afc Successfully tagged nginx:testdate
[root@k8s-master zhanglei]# docker images |grep nginx nginx testdate d3cde57b8afc 50 seconds ago 133MB
通过上面的步骤在nginx的基础上重新制作了镜像,其新的镜像的tag为testdate,有了新的镜像,就可以运行新的容器,通过docker run 运行1个容器
[root@k8s-master zhanglei]# docker run -it nginx:testdate /bin/bash root@266e44b054a5:/# date Tue Aug 25 19:35:00 CST 2020 root@266e44b054a5:/# exit exit [root@k8s-master zhanglei]# date 2020年 08月 25日 星期二 19:35:09 CST
如上可以看到通过制作镜像也可实现容器与宿主机的时区同步
三、总结
以上为容器与宿主机时区同步的几种方法,对于用户来说,可通过制作镜像、加入环境变量,通过hostPath将时区文件挂载到容器时区路径等方式,不过这需要建立在用户有一定的容器先验知识的基础上,因此对于产品服务提供方来来说,应尽量降低用户的使用门槛,因此UI化的形式比较推荐,可考虑将其作为功能,由用户决定是否开启容器与宿主机时间的同步,即可实现需求,灵活性也较高。
相关文章推荐
- Pod Preset玩转K8S容器时区自动配置
- openshift/origin工作记录(4)——Pod时区同步
- 【转载】Linux 系统时间查看 及 时区修改(自动同步时间)
- c# 异步和同步问题(转载)
- 政府出版品與POD(Print on Demand) (转载)
- 解决Docker容器时区及时间不同步问题
- Vmware中linux修改时间与主机同步,修改时区
- linux ntp 时间同步 以及时区更改
- WinXP系统时间无法同步网络时间连时区也不显示的解决方法
- Kubernetes K8S之Pod生命周期与探针检测
- 基于RTP的音视频同步方案设计【转载请注明出处】
- centos 修改时区及NTP时间同步
- vmware 虚拟系统和宿主系统 文件共享(转载)
- [转载] pthread的各种同步机制
- 强制删除k8s的pod
- android-获取网络时间、获取特定时区时间、时间同步的方法
- 设置linux时区同步当前时间
- 20160711--C# 委托的三种调用示例(同步调用 异步调用 异步回调)【转载】
- 自动调整linux系统时间和时区与Internet时间同步
- Linux(Unix)时钟同步ntpd服务配置方法(转载)