一、实现原理:首先宿主机间实现时间同步,然后容器启动时以只读方式挂载宿主机的/etc/localtime文件实现各宿主机上的Docker容器间的时间同步。
二、操作步骤
1、宿主机修改时区
[[email protected] ~]# cp -f /usr/share/zoneinfo/Asia/Shanghai /etc/localtime cp: overwrite ‘/etc/localtime’? y [[email protected] ~]# date Sun Jan 3 11:49:33 CST 2016
2、安装ntpd服务,并设置开机自启动
[[email protected] ~]# yum install ntp -y [[email protected] ~]# systemctl enable ntpd ln -s ‘/usr/lib/systemd/system/ntpd.service‘ ‘/etc/systemd/system/multi-user.target.wants/ntpd.service‘
3、宿主机与ntp.api.bz同步
[[email protected] ~]# ntpdate ntp.api.bz 3 Jan 11:52:33 ntpdate[12298]: adjust time server 61.153.197.226 offset 0.034242 sec
4、启动ntpd service
[[email protected] ~]# systemctl start ntpd.service
5、启动容器
[[email protected] ~]# docker run -ti -d --name test registry.tingyun.com/tingyun/common/centos:7 -v /etc/localtime:/etc/localtime:ro /bin/bash fd8e9ab132d3802b2c8eee3caf79dc547a1e3f025adc3ee5316eda7d967a1b11 [[email protected] ~]# date Sun Jan 3 12:07:21 CST 2016 [[email protected] ~]# docker exec -ti test /bin/bash [[email protected] /]# date Sun Jan 3 12:07:34 CST 2016
时间: 2024-10-20 01:33:15