Linux使用chrony让局域网内的服务器时间同步
在生产环境经常会因为时间的问题出现过问题,例如应用节点和数据存节点时间不一致,造成检索不到数据的问题等。在现在不管是公有云、私有云还是混合云等在建设过程中,都首先需要群集中的每台服务器时间调节一致,这就需要使用一台服务器作为时间服务器来维护时间。chrony就是现在群集的选择方案之一

Chrony简介
chrony是一款开源的软件,它能帮助你保持系统时钟与时钟服务器(NTP)同步,因此让你的时间保持精确。它由两个程序组成,分别是chronyd和chronyc。chronyd是一个后台运行的守护进程,用于调整内核中运行的系统时钟和时钟服务器同步。它确定计算机增减时间的比率,并对此进行补偿。chronyc提供了一个用户界面,用于监控性能并进行多样化的配置。它可以在chronyd实例控制的计算机上工作,也可以在一台不同的远程计算机上工作。

Chrony 的优势
更快的同步只需要数分钟而非数小时时间,从而最大程度减少了时间和频率误差,对于并非全天 24 小时运行的虚拟计算机而言非常有用
能够更好地响应时钟频率的快速变化,对于具备不稳定时钟的虚拟机或导致时钟频率发生变化的节能技术而言非常有用
在初始同步后,它不会停止时钟,以防对需要系统时间保持单调的应用程序造成影响
在应对临时非对称延迟时(例如,在大规模下载造成链接饱和时)提供了更好的稳定性
无需对服务器进行定期轮询,因此具备间歇性网络连接的系统仍然可以快速同步时钟
Chrony程序
chrony两个主要程序chronyd和chronyc。

chronyd:后台运行的守护进程,用于调整内核中运行的系统时钟和时钟服务器同步。它确定计算机增减时间的比率,并对此进行补偿

chronyc:命令行用户工具,用于监控性能并进行多样化的配置。它可以在chronyd实例控制的计算机上工作,也可在一台不同的远程计算机上工作

实际生产环境中,服务器之间的时间是需要同步,但并不是所有机器可以直接连外网,这时可以用Chrony工具解决。 解决方法是将其中一台设为时间服务器,然后其它服务器和这台时间服务器同步即可。具体步骤如下:

节点 节点IP地址 类型
master01 172.16.24.65 服务端
worker01 172.16.24.188 客户端
一、部署Chrony
在所有节点上执行chrony服务安装命令,将主节点设置时间服务器,其他的节点都从主节点同步时间。

安装服务

yum -y install chrony

查看状态

systemctl status chronyd

重启chronyd

systemctl restart chronyd

1
2
3
4
5
6
7
二、检查设置时区
在所有节点上设置统一的时间区域,本文中将其设置为亚洲时区,用户可自行定义。

查看时区

[root@x ~]# timedatectl
Local time: Sun 2022-04-17 11:15:48 CST
Universal time: Sun 2022-04-17 03:15:48 UTC
RTC time: Sun 2022-04-17 11:15:48
Time zone: Asia/Shanghai (CST, +0800)
NTP enabled: yes
NTP synchronized: yes
RTC in local TZ: yes
DST active: n/a

筛选式查看在亚洲S开的上海可用时区:

[root@x ~]# timedatectl list-timezones | grep -E “Asia/S.*”
Asia/Sakhalin
Asia/Samarkand
Asia/Seoul
Asia/Shanghai
Asia/Singapore
Asia/Srednekolymsk

设置当前系统为Asia/Shanghai上海时区:

[root@x ~]# timedatectl set-timezone Asia/Shanghai

修改日期时间(可选,主节时钟源无法同步,可以先关闭NTP同步)

timedatectl set-ntp false
timedatectl set-time “2022-04-17 15:50:20”

开启 NTP

timedatectl set-ntp true

#设置完时区后,强制同步下系统时钟:
[root@x ~]# chronyc -a makestep
200 OK

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
三、防火墙设置
因NTP使用123/UDP端口协议,所以允许NTP服务即可。但是我一般都会把防火墙和selinux禁止。

查看防火墙状态

[root@x ~]# systemctl status firewalld.service
● firewalld.service - firewalld - dynamic firewall daemon
Loaded: loaded (/usr/lib/systemd/system/firewalld.service; disabled; vendor preset: enabled)
Active: inactive (dead)
Docs: man:firewalld(1)

启动防火墙

[root@x ~]# systemctl start firewalld.service

防火墙内放行NTP服务

[root@x ~]# firewall-cmd --add-service=ntp --permanent
success

reload才能生效

[root@x ~]# firewall-cmd --reload
success
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
内部访问一般直接关闭防火墙

[root@x ~]# systemctl stop firewalld
[root@x ~]# systemctl disalbe firewalld
1
2
四、配置Chrony
1.服务器端配置
​ 将主节点设置为内部NTP Server,编辑“/etc/chrony.conf”文件,如有公网的情况,可以配置阿里云的ntp服务地址作为源 。

ntp1.aliyun.com

授时中心参考

210.72.145.44 国家授时中心
ntp.aliyun.com 阿里云
s1a.time.edu.cn 北京邮电大学
s1b.time.edu.cn 清华大学
s1c.time.edu.cn 北京大学
s1d.time.edu.cn 东南大学
s1e.time.edu.cn 清华大学
s2a.time.edu.cn 清华大学
s2b.time.edu.cn 清华大学
s2c.time.edu.cn 北京邮电大学
s2d.time.edu.cn 西南地区网络中心
s2e.time.edu.cn 西北地区网络中心
s2f.time.edu.cn 东北地区网络中心
s2g.time.edu.cn 华东南地区网络中心
s2h.time.edu.cn 四川大学网络管理中心
s2j.time.edu.cn 大连理工大学网络中心
s2k.time.edu.cn CERNET桂林主节点
s2m.time.edu.cn 北京大学
ntp.sjtu.edu.cn 202.120.2.101 上海交通大学
​ master01作为服务端,当前节点IP地址为172.16.24.65,网段是172.16.24.0/24,配置详情如下

server ntp1.aliyun.com iburst

allow 172.16.24.0/24

local stratum 10

[root@master01 ~]# vim /etc/chrony.conf
1

Use public servers from the pool.ntp.org project.

Please consider joining the pool (http://www.pool.ntp.org/join.html).

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 ntp1.aliyun.com iburst

Record the rate at which the system clock gains/losses time.

driftfile /var/lib/chrony/drift

Allow the system clock to be stepped in the first three updates

if its offset is larger than 1 second.

makestep 1.0 3

Enable kernel synchronization of the real-time clock (RTC).

rtcsync

Enable hardware timestamping on all interfaces that support it.

#hwtimestamp *

Increase the minimum number of selectable sources required to adjust

the system clock.

#minsources 2

Allow NTP client access from local network.

指定一台主机、子网,或者网络以允许或拒绝访问本服务器

allow 172.16.24.0/24

Serve time even if not synchronized to a time source.

即使server指令中时间服务器不可用,也允许将本地时间作为标准时间授时给其它客户端chronyc命令

local stratum 10

Specify file containing keys for NTP authentication.

#keyfile /etc/chrony.keys

Specify directory for log files.

logdir /var/log/chrony

Select which information is logged.

#log measurements statistics tracking

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
2.客户端节点配置
其他work节点使用主节点(master01)作为时钟源,增加内网时钟源

server master01 iburst

[root@worker01 ~]# vim /etc/chrony.conf
1

Use public servers from the pool.ntp.org project.

Please consider joining the pool (http://www.pool.ntp.org/join.html).

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 master01 iburst

Record the rate at which the system clock gains/losses time.

driftfile /var/lib/chrony/drift

Allow the system clock to be stepped in the first three updates

if its offset is larger than 1 second.

makestep 1.0 3

Enable kernel synchronization of the real-time clock (RTC).

rtcsync

Enable hardware timestamping on all interfaces that support it.

#hwtimestamp *

Increase the minimum number of selectable sources required to adjust

the system clock.

#minsources 2

Allow NTP client access from local network.

#allow 192.168.0.0/16

Serve time even if not synchronized to a time source.

#local stratum 10

Specify file containing keys for NTP authentication.

#keyfile /etc/chrony.keys

Specify directory for log files.

logdir /var/log/chrony

Select which information is logged.

#log measurements statistics tracking

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
3.配置文件说明:
/etc/chrony.conf

server
可用于时钟服务器,iburst 选项当服务器可达时,发送一个八个数据包而不是通常的一个数据包。 包间隔通常为2秒,可加快初始同步速度,这个可以添加外网服务器或者内网服务器,域名或者IP都可以。

driftfile
根据实际时间计算出计算机增减时间的比率,将它记录到一个文件中,会在重启后为系统时钟作出补偿

stratumweight
stratumweight指令设置当chronyd从可用源中选择同步源时,每个层应该添加多少距离到同步距离。默认情况下,CentOS中设置为0,让chronyd在选择源时忽略源的层级。

rtcsync
启用内核模式,系统时间每11分钟会拷贝到实时时钟(RTC)

allow / deny
指定一台主机、子网,或者网络以允许或拒绝访问本服务器

cmdallow / cmddeny
可以指定哪台主机可以通过chronyd使用控制命令

bindcmdaddress
允许chronyd监听哪个接口来接收由chronyc执行的命令

makestep
通常chronyd将根据需求通过减慢或加速时钟,使得系统逐步纠正所有时间偏差。在某些特定情况下,系统时钟可能会漂移过快,导致该调整过程消耗很长的时间来纠正系统时钟。该指令强制chronyd在调整期大于某个阀值时调整系统时钟

local stratum 10
即使server指令中时间服务器不可用,也允许将本地时间作为标准时间授时给其它客户端chronyc命令

四、启动chrony服务
在所有节点上将其配置为在系统引导时启动。

设置开机启动

[root@x~]# systemctl enable chronyd.service

重启服务

[root@x~]# systemctl restart chronyd.service

查看时间同步状态

[root@x~]# timedatectl status

手动强制同步下系统时钟

[root@x~]# chronyc -a makestep

查看时间同步源

[root@x~]# chronyc sources -v

查看时间同步源状态

[root@x~]# chronyc sourcestats -v

硬件时间默认为UTC

[root@x~]# timedatectl set-local-rtc 1

启用NTP时间同步

[root@x~]# timedatectl set-ntp yes

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
五、chronyc相关命令
help 命令可以查看更多chronyc的交互命令

accheck 检查是否对特定主机可访问当前服务器

activity 显示有多少NTP源在线/离线

sources [-v] 显示当前时间源的同步信息

sourcestats [-v] 显示当前时间源的同步统计信息

add server 手动添加一台新的NTP服务器

clients 报告已访问本服务器的客户端列表

delete 手动移除NTP服务器或对等服务器

settime 手动设置守护进程时间

sracking 显示系统时间信息

chronyc sources 主要用于查看时钟源授时时间偏差值。

[root@iZbp1fy7y89tqjvmlp1dvhZ ~]# chronyc sources
210 Number of sources = 15
MS Name/IP address Stratum Poll Reach LastRx Last sample

^+ 120.25.115.20 2 10 255 987 -982us[ -985us] +/- 15ms
^? 10.143.33.49 0 10 0 - +0ns[ +0ns] +/- 0ns
^+ 100.100.3.1 2 10 377 112 +126us[ +126us] +/- 1601us
^+ 100.100.3.2 2 10 377 258 -75us[ -75us] +/- 1738us
^+ 100.100.3.3 2 10 375 720 -191us[ -191us] +/- 1708us
^+ 203.107.6.88 2 10 377 344 +2988us[+2988us] +/- 18ms
^? 10.143.33.50 0 10 0 - +0ns[ +0ns] +/- 0ns
^? 10.143.33.51 0 10 0 - +0ns[ +0ns] +/- 0ns
^? 10.143.0.44 0 10 0 - +0ns[ +0ns] +/- 0ns
^? 10.143.0.45 0 10 0 - +0ns[ +0ns] +/- 0ns
^? 10.143.0.46 0 10 0 - +0ns[ +0ns] +/- 0ns
^+ 100.100.5.1 2 10 377 537 +324us[ +324us] +/- 1975us
^+ 100.100.5.2 2 10 377 269 -209us[ -209us] +/- 1946us
^+ 100.100.5.3 2 10 377 281 +126us[ +126us] +/- 2078us
^* 100.100.61.88 1 10 377 913 -465us[ -468us] +/- 11ms

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
这里总共输出8列信息,分别对应含义如下:

列名 含义 具体说明
M 表示授时时钟源 ^表示服务器,= 表示二级时钟源 ,#表示本地连接的参考时钟
S 指示源的状态 *当前同步的源,+表示其他可接受的源,?表示连接丢失的源,x表示一个认为是falseticker 的时钟(即它的时间与大多数其他来源不一致),~表示其时间似乎具有太多可变性的来源
Name/IP address 表示源的名称或IP地址,或者参考时钟的refid值 无
Stratum 表示源的层级 层级1表示本地连接的参考时钟,第2层表示通过第1层级计算机的时钟实现同步,依此类推。
Poll 表示源轮询的频率 以秒为单位,值是基数2的对数,例如值6表示每64秒进行一次测量,chronyd会根据当时的情况自动改变轮询频率
Reach 表示源的可达性的锁存值(八进制数值) 该锁存值有8位,并在当接收或丢失一次时进行一次更新,值377表示最后八次传输都收到了有效的回复
LastRx 表示从源收到最近的一次的时间 通常是几秒钟,字母m,h,d或y分别表示分钟,小时,天或年,值10年表示从未从该来源收到时间同步信息
Last sample 表示本地时钟与上次测量时源的偏移量 方括号中的数字表示实际测量的偏移值,这可以以ns(表示纳秒),us(表示微秒),ms(表示毫秒)或s(表示秒)为后缀;方括号左侧的数字表示原始测量值,这个值是经过调整以允许应用于本地时钟的任何偏差;方括号右侧表示偏差值,+/-指示器后面的数字表示测量中的误差范围,+偏移表示本地时钟快速来源
chronyc sourcestats 主要偏移率及每个时钟源的偏移评估值:
[root@iZbp1fy7y89tqjvmlp1dvhZ ~]# chronyc sourcestats
210 Number of sources = 15
Name/IP Address NP NR Span Frequency Freq Skew Offset Std Dev

120.25.115.20 37 15 12h -0.001 0.024 -216us 516us
10.143.33.49 0 0 0 +0.000 2000.000 +0ns 4000ms
100.100.3.1 26 18 431m -0.001 0.004 +94us 44us
100.100.3.2 22 12 362m +0.001 0.005 +5134ns 44us
100.100.3.3 17 9 362m -0.003 0.006 -236us 38us
203.107.6.88 64 32 18h +0.013 0.039 +553us 1730us
10.143.33.50 0 0 0 +0.000 2000.000 +0ns 4000ms
10.143.33.51 0 0 0 +0.000 2000.000 +0ns 4000ms
10.143.0.44 0 0 0 +0.000 2000.000 +0ns 4000ms
10.143.0.45 0 0 0 +0.000 2000.000 +0ns 4000ms
10.143.0.46 0 0 0 +0.000 2000.000 +0ns 4000ms
100.100.5.1 16 8 258m +0.001 0.009 +321us 44us
100.100.5.2 17 8 276m +0.001 0.007 -219us 35us
100.100.5.3 21 11 344m +0.001 0.006 +80us 48us
100.100.61.88 17 11 276m -0.001 0.009 -389us 39us

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
列名 含义
Name/IP address 表示源的名称或IP地址,或者参考时钟的refid值
NP 这是当前为服务器保留的采样点数,通过这些点执行线性回归方法来估算出偏移值
NR 这是在最后一次回归之后具有相同符号的偏差值的运行次数。如果此数字相对于样本数量开始变得太小,则表明直线不再适合数据。如果运行次数太少,则chronyd丢弃旧样本并重新运行回归,直到运行次数变得可接受为止
Span 这是最旧和最新样本之间的间隔。如果未显示任何单位,则该值以秒为单位。在该示例中,间隔为46分钟
Frequency 这是服务器的估算偏差值的频率,单位为百万分之一。在这种情况下,计算机的时钟估计相对于服务器以10 ** 9的速度运行1个部分
Freq Skew 这是Freq的估计误差范围(再次以百万分率计)
Offset 这是源的估计偏移量
Std Dev 这是估计的样本标准偏差
chronyc tracking 检查时间是否同步
[root@iZbp1fy7y89tqjvmlp1dvhZ ~]# chronyc tracking
Reference ID : 64643D58 (100.100.61.88)
Stratum : 2
Ref time (UTC) : Sun Apr 17 08:14:49 2022
System time : 0.000007649 seconds slow of NTP time
Last offset : -0.000069499 seconds
RMS offset : 0.000063396 seconds
Frequency : 32.603 ppm slow
Residual freq : -0.638 ppm
Skew : 0.008 ppm
Root delay : 0.001351137 seconds
Root dispersion : 0.010351420 seconds
Update interval : 16.2 seconds
Leap status : Normal
————————————————
版权声明:本文为CSDN博主「探路者之旅」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/xinle0320/article/details/124232910

Logo

为开发者提供学习成长、分享交流、生态实践、资源工具等服务,帮助开发者快速成长。

更多推荐