时间服务和chrony

多主机协作工作时,各个主机的时间同步很总要,时间不一致会造成很多重要应用的故障,如:加密协议,日志,集群等,利用NTP协议使网络中的各个计算机时间达到同步。目前NTP协议属于运维基础架构中必备的基本服务之一。

时间同步实现:ntp,chrony

ntp将系统时间和世界协调时UTC同步,精度在局域网内可达到0.1ms,在互联网上绝大多数的地方精度可以达到1-50ms。目前CentOS6上所使用的就是ntp服务。

chrony为CentOS7上所使用的时间服务,其同步的速度比ntp更快。


ntp服务的部署

主机A从互联网上的主机同步时间并作为局域网内的时间服务器使用,主机B自动去向主机A同步时间

环境准备

准备A、B、两台主机

主机 系统 ip
A CentOS6 192.168.73.137
B CentOS6 192.168.73.136

将主机B的时间调慢

1
2
3
4
[root@HostB ~]# date -s "-10 days"
Sun Apr 7 10:50:51 CST 2019
[root@HostB ~]# date
Sun Apr 7 10:50:58 CST 2019

查看下主机A时间

1
2
[root@HostA ~]# date
Wed Apr 17 10:53:35 CST 2019

配置ntp服务

一、将主机A设置为时间服务器

1.修改/etc/ntp

1
2
3
4
5
6
7
8
9
10
11
[root@HostA ~]# vim /etc/ntp.conf 
...
#restrict default kod nomodify notrap nopeer noquery #将文件中的此行注释,或者修改为下面行
restrict default kod nomodify
...
#server 0.centos.pool.ntp.org iburst
#server 1.centos.pool.ntp.org iburst
#server 2.centos.pool.ntp.org iburst
#server 3.centos.pool.ntp.org iburst
server 172.22.0.1 iburst #将时间服务器指向外部的时间服务器。
...

2.将主机A与外网的时间服务器同步

1
2
[root@HostA ~]# ntpdate 172.22.0.1
18 Apr 10:27:53 ntpdate[3825]: adjust time server 172.22.0.1 offset 0.004437 sec

3.启动ntp服务,将ntp服务设置为开机启动

1
2
3
[root@HostA ~]# service ntpd start
Starting ntpd: [ OK ]
[root@HostA ~]# chkconfig ntpd on

二、修改主机B配置文件修改为自动和主机A同步时间

1.修改配置文件,将时间服务器指向主机A

1
2
3
4
5
#server 0.centos.pool.ntp.org iburst
#server 1.centos.pool.ntp.org iburst
#server 2.centos.pool.ntp.org iburst
#server 3.centos.pool.ntp.org iburst
server 192.168.73.140 iburst #添加此行

2.启动服务。

1
2
3
4
5
6
7
8
9
[root@HostB ~]# service ntpd start
Starting ntpd: [ OK ]
[root@HostB ~]# date #由于ntp服务同步速度较慢,需要很长一段时间才能同步使劲按
Mon Apr 8 10:34:43 CST 2019
[root@HostB ~]# service ntpd restart
Shutting down ntpd: [ OK ]
Starting ntpd: [ OK ]
[root@HostB ~]# date #再次重启服务,此时时间已经自动同步。
Thu Apr 18 10:34:54 CST 2019

chrony的部署

此处以刚才配置的主机A为互联网中的时间服务器,主机7A从主机A同步时间并作为局域网内的时间服务器使用,主机7B自动去向主机7A同步时间

环境准备

准备7A、7B、两台主机

主机名 系统 IP
7A CentOS7 192.168.73.150
7B CentOS7 192.168.73.139

一、配置时间服务器

1.修改主机7A配置文件修改/etc/chrony.conf

1
2
3
4
5
6
7
[root@7a ~]# vim /etc/chrony.conf
...
server 192.168.73.140 iburst #添加此行指向网络中的时间服务器
...
allow 192.168.73.0/24 #添加当自己为时间服务器时允许访问的网段
...
local stratum 10 #此行前的注释去掉

2.启动chronyd服务,并设置为开机启动

1
2
3
[root@7a ~]# systemctl start chronyd.service
[root@7a ~]# systemctl enable chronyd.service
Created symlink from /etc/systemd/system/multi-user.target.wants/chronyd.service to /usr/lib/systemd/system/chronyd.service.

二、配置局域网中的服务器,将时间服务器指向为7A

1.修改配置文件

1
2
3
4
5
6
[root@7b ~]# vim /etc/chrony.conf 
#server 0.centos.pool.ntp.org iburst
#server 1.centos.pool.ntp.org iburst
#server 2.centos.pool.ntp.org iburst
#server 3.centos.pool.ntp.org iburst
server 192.168.73.150 iburst #添加此行

2.启动chrony服务,并设置为开机自动启动

1
2
3
4
[root@7b ~]# systemctl start chronyd
[root@7b ~]# systemctl enable chronyd
Created symlink from /etc/systemd/system/multi-user.target.wants/chronyd.service to /usr/lib/systemd/system/chronyd.service.

3.查看时间同步情况

1
2
3
4
5
6
[root@7b ~]# chronyc sources
210 Number of sources = 1
MS Name/IP address Stratum Poll Reach LastRx Last sample
===============================================================================
^* 192.168.73.150 5 6 177 31 +50us[ +77us] +/- 218ms
[root@7b ~]#