Linux时间延迟平滑对时方案的分析-Ntp和Chrony的不同表现

2023-05-16

系列文章目录


文章目录

  • 系列文章目录
  • 前言
  • 一、环境信息
  • 二、ntpd模式
    • 2.1 版本信息
    • 2.2 配置ntpd对时源
      • 2.2.1 配置ntpd服务配置,启动ntpd服务
      • 2.3.2 查看对时情况
    • 2.3 配置ntpd客户端
      • 2.3.1 配置ntpd服务配置,启动ntpd服务
      • 2.3.2 查看对时情况
    • 2.4 关键场景
      • 2.4.1 场景1:能否持续有效的长时间进行平滑同步,直到时间追上了?
        • 2.4.1.1 场景1.1:当前节点和时钟源时间差别在600s以内,能否稳定追上?
        • 2.4.1.1 场景1.2:当前节点和时钟源时间差别在600s-1000s,能否稳定追上?
        • 2.4.1.1 场景1.3:当前节点和时钟源时间差别在1000s以上,能否稳定追上?
      • 2.4.2 场景2:如果进行时间同步期间,ntpd服务发生了重启,是否会引发时间跳变?
  • 三、chronyd模式
    • 3.1 版本信息
    • 3.2 配置chronyd对时源
      • 3.2.1 配置chronyd服务配置,启动chronyd服务
      • 3.3.2 查看对时情况
    • 3.3 配置chronyd客户端
      • 3.3.1 配置chronyd服务配置,启动chronyd服务
      • 3.3.2 查看对时情况
    • 3.4 关键场景
      • 3.4.1 场景1:能否持续有效的长时间进行平滑同步,直到时间追上了?
      • 3.4.2 场景2:如果进行时间同步期间,chronyd服务发生了重启,是否会引发时间跳变?
  • 四、总结
    • 4.1 ntpd模式下
      • 4.1.1 平滑追时间表现
      • 4.1.2 发生服务重启表现
    • 4.2 chronyd模式下
      • 4.2.1 平滑追时间表现
      • 4.2.2 发生服务重启表现
    • 4.3 建议

前言

生产环境中,特别是私有云环境中,由于网络问题,导致各个服务器的时间不一致,部分时间跟时钟源存在较大偏差,而生产环境下,如果机器不能直接下线,而需要在线平滑追数据(大部分场景下,是不允许跳变的),本问题主要探讨ntpd模和chrony模式下,平滑追数据时的表现。


一、环境信息

使用本地的vmstation创建2个虚拟机,信息如下

节点名称节点IP节点配置操作系统备注
host-1192.168.82.1281c1g 20gCentOS7.4作为系统内部的对时源,时间跟外部公有云的对时源一致
host-2192.168.82.1291c1g 20gCentOS7.4对时源是192.168.82.128,通过提调整本地时间,模拟时间延迟的场景

二、ntpd模式

2.1 版本信息

本文使用的版本是 Ver. 4.2.6p5,通过yum安装完成

2.2 配置ntpd对时源

通过启动服务器192.168.82.128的chronyd服务,并配置本节点作为系统内部的对时源

2.2.1 配置ntpd服务配置,启动ntpd服务

ntpd的主要配置文件是

cat /etc/sysconfig/ntpd
cat /etc/ntp.conf

/etc/ntpd.conf关键配置

[root@host-1 ~]# cat /etc/ntp.conf |grep -v '#'
driftfile /var/lib/ntp/drift
restrict default nomodify notrap nopeer noquery
restrict 127.0.0.1 
restrict ::1
includefile /etc/ntp/crypto/pw
keys /etc/ntp/keys
server time.cloud.tencent.com iburst
server time1.cloud.tencent.com iburst
server time2.cloud.tencent.com iburst
server time3.cloud.tencent.com iburst
server time4.cloud.tencent.com iburst
server time5.cloud.tencent.com iburs

/etc/sysconfig/ntpd关键配置

[root@host-1 ~]# cat /etc/sysconfig/ntpd
OPTIONS="-g"
[root@host-1 ~]# 

查看服务启动情况

[root@host-1 ~]# systemctl status ntpd
● ntpd.service - Network Time Service
   Loaded: loaded (/usr/lib/systemd/system/ntpd.service; disabled; vendor preset: disabled)
   Active: active (running) since Sun 2022-08-07 06:25:06 PDT; 7min ago
  Process: 39906 ExecStart=/usr/sbin/ntpd -u ntp:ntp $OPTIONS (code=exited, status=0/SUCCESS)
 Main PID: 39907 (ntpd)
    Tasks: 1
   Memory: 1.5M
   CGroup: /system.slice/ntpd.service
           └─39907 /usr/sbin/ntpd -u ntp:ntp -g

Aug 07 06:25:06 host-1 ntpd[39907]: Listen normally on 4 docker0 172.17.0.1 UDP 123
Aug 07 06:25:06 host-1 ntpd[39907]: Listen normally on 5 virbr0 192.168.122.1 UDP 123
Aug 07 06:25:06 host-1 ntpd[39907]: Listen normally on 6 lo ::1 UDP 123
Aug 07 06:25:06 host-1 ntpd[39907]: Listen normally on 7 vetha5a8b14 fe80::9448:baff:fe4e:caf3 UDP 123
Aug 07 06:25:06 host-1 ntpd[39907]: Listen normally on 8 ens33 fe80::20c:29ff:feb9:8849 UDP 123
Aug 07 06:25:06 host-1 ntpd[39907]: Listen normally on 9 docker0 fe80::42:88ff:fee9:b582 UDP 123
Aug 07 06:25:06 host-1 ntpd[39907]: Listening on routing socket on fd #26 for interface updates
Aug 07 06:25:07 host-1 ntpd[39907]: 0.0.0.0 c016 06 restart
Aug 07 06:25:07 host-1 ntpd[39907]: 0.0.0.0 c012 02 freq_set kernel 8.020 PPM
Aug 07 06:25:13 host-1 ntpd[39907]: 0.0.0.0 c615 05 clock_sync
[root@host-1 ~]# 

2.3.2 查看对时情况

查看对时源的同步情况

[root@host-1 ~]# ntpstat 
synchronised to NTP server (111.230.189.174) at stratum 3
   time correct to within 45 ms
   polling server every 64 s
[root@host-1 ~]# ntpq -p
     remote           refid      st t when poll reach   delay   offset  jitter
==============================================================================
-139.199.215.251 100.122.36.196   2 u   15   64  375   36.717    2.310   3.776
*111.230.189.174 100.122.36.196   2 u   81   64  276   34.307   -0.278   1.757
+139.199.214.202 100.122.36.196   2 u   87   64  356   36.572   -1.035   2.475
+134.175.254.134 100.122.36.196   2 u   20   64  377   33.943   -0.948   3.200
-134.175.253.104 9.20.184.92      2 u   17   64  177   32.041    2.798   1.885

说明时钟同步完成

注意使用ntpq -p查询时间差异时,会有缓存数据,并不完全实时,可以通过ntpdate -q ${ip}代替

2.3 配置ntpd客户端

通过启动服务器192.168.82.129的ntpd服务,作为客户端,并设置192.168.82.128作为对时源,并进行时钟同步。

2.3.1 配置ntpd服务配置,启动ntpd服务

具体的配置如下

[root@host-2 chrony]# cat /etc/ntp.conf |grep -v '#'
driftfile /var/lib/ntp/drift
restrict default nomodify notrap nopeer noquery
restrict 127.0.0.1 
restrict ::1
server 192.168.92.128 iburst
includefile /etc/ntp/crypto/pw
keys /etc/ntp/keys
disable monitor
[root@host-2 chrony]# cat /etc/sysconfig/ntpd
# Command line options for ntpd
#SYNC_HWCLOCK=yes
OPTIONS="-g -x" 
[root@host-2 chrony]# 

关键配置如下
通过bash man ntpd可以获取服务的说明,有几个关键配置需要单独拎出来说明一下。也可以参考官方说明,把说明列出来,并加以一些说明

-g
Normally,  ntpd  exits  with  a  message  to  the system log if the offset exceeds the panic threshold, 
which is 1000 s by default.
This option allows the time to be set to any value without restriction; however, this can happen only once. 
If the threshold is exceeded after that, ntpd will exit with a message to the system log. 
This option can be used with the -q and -x options.
See the tinker command for other options.

大致的关键信息是,该参数允许完成一次任意时间的跳变,也就是说,如果当前节点的时钟源相差大于1000s以外,也是运行当前节点能够快速跳变到任意时间的。如果1次完成跳变后,依然还是在1000s以外并持续10min后,ntpd服务主动退出(通俗点,就是ntpd服务1次的保命机会)。

-x
Normally,  the time is slewed if the offset is less than the step threshold, 
which is 128 ms by default, and stepped if above the threshold. 
This option sets the threshold to 600 s, which is well within the accuracy window to set the clock manually.
Note: Since the slew rate of typical Unix kernels is limited to 0.5 ms/s, each second of adjustment requires an amortization inter‐val  of 2000 s. 
Thus, an adjustment as much as 600 s will take almost 14 days to complete. 
This option can be used with the -g and -q options. See the tinker command for other options. 
Note: The kernel time discipline is disabled with this option and the step threshold is applied also to leap second corrections.

这个参数允许当前节点和时钟源的时间差在600s能够实现渐变的进行时间同步,但是超过600s以后,还是会进行跳变。
套用别人的一个总结。
在这里插入图片描述

没有开启的配置,也单独说明一下

tinker panic 0

该参数允许当前节点的时间差和时钟源的时间差能够突破1000s的限制,能够允许超过1000s的时间差而ntpd服务进程不退出,但是不影响当前节点同步时间动作。

查看服务启动情况

[root@host-2 chrony]# systemctl status ntpd
● ntpd.service - Network Time Service
   Loaded: loaded (/usr/lib/systemd/system/ntpd.service; disabled; vendor preset: disabled)
   Active: active (running) since Thu 2022-08-04 20:06:56 PDT; 2 days ago
  Process: 106375 ExecStart=/usr/sbin/ntpd -u ntp:ntp $OPTIONS (code=exited, status=0/SUCCESS)
 Main PID: 106377 (ntpd)
    Tasks: 1
   Memory: 1.2M
   CGroup: /system.slice/ntpd.service
           └─106377 /usr/sbin/ntpd -u ntp:ntp -g -x

Aug 04 20:06:56 host-2 ntpd[106377]: 0.0.0.0 c016 06 restart
Aug 04 20:06:56 host-2 ntpd[106377]: 0.0.0.0 c012 02 freq_set ntpd 0.867 PPM
Aug 04 20:07:03 host-2 ntpd[106377]: 0.0.0.0 c61c 0c clock_step +210615.841292 s
Aug 07 06:37:19 host-2 ntpd[106377]: 0.0.0.0 c614 04 freq_mode
Aug 07 06:37:20 host-2 ntpd[106377]: 0.0.0.0 c618 08 no_sys_peer
Aug 07 06:42:09 host-2 ntpd[106377]: Deleting interface #3 ens33, 192.168.92.129#123, interface stats: received=12, sent=12, dropped=0, active_time=297 secs
Aug 07 06:42:09 host-2 ntpd[106377]: 192.168.92.128 interface 192.168.92.129 -> (none)
Aug 07 06:42:12 host-2 ntpd[106377]: Listen normally on 8 ens33 192.168.92.129 UDP 123
Aug 07 06:42:12 host-2 ntpd[106377]: new interface(s) found: waking up resolver
Aug 07 06:42:13 host-2 ntpd[106377]: 0.0.0.0 c628 08 no_sys_peer
[root@host-2 chrony]# 

2.3.2 查看对时情况

查看对时源的同步情况

[root@host-2 chrony]# ntpstat 
synchronised to NTP server (192.168.92.128) at stratum 4
   time correct to within 1007 ms
   polling server every 64 s
[root@host-2 chrony]# ntpstat 
synchronised to NTP server (192.168.92.128) at stratum 4
   time correct to within 1007 ms
   polling server every 64 s
[root@host-2 chrony]# ntpq -p
     remote           refid      st t when poll reach   delay   offset  jitter
==============================================================================
*192.168.92.128  111.230.189.174  3 u   24   64    1    0.275    1.557   0.049
[root@host-2 chrony]# ntpdate -q 192.168.92.128
server 192.168.92.128, stratum 3, offset 0.001456, delay 0.02597
 7 Aug 06:53:35 ntpdate[109915]: adjust time server 192.168.92.128 offset 0.001456 sec
[root@host-2 chrony]# 

说明时间已经完成同步。

2.4 关键场景

如下几个问题,是生产环境中最关注的问题。大致如下

2.4.1 场景1:能否持续有效的长时间进行平滑同步,直到时间追上了?

经过测试,这种场景下,又分3个子场景。

2.4.1.1 场景1.1:当前节点和时钟源时间差别在600s以内,能否稳定追上?

人工设置当前节点的时间,跟时钟源的节点时间相差低于600s(383s),并观察20min,验证能否稳定在追时间。按照计算,追时间的速度是0.5ms/1s(需要2000s时间追1s),大约需要383*2000/3600=212.7h才能追完

[root@host-2 chrony]# date -s 'Sun Aug  7 06:50:36 PDT 2022'
Sun Aug  7 06:50:36 PDT 2022
[root@host-2 ~]# while [[ 1 ]];do ntpdate -q 192.168.92.128;sleep 1;done
 7 Aug 06:56:49 ntpdate[110416]: adjust time server 192.168.92.128 offset 0.001041 sec
server 192.168.92.128, stratum 3, offset 0.001061, delay 0.02582
 7 Aug 06:56:56 ntpdate[110418]: adjust time server 192.168.92.128 offset 0.001061 sec
server 192.168.92.128, stratum 3, offset 383.287000, delay 0.02589
 7 Aug 06:50:40 ntpdate[110420]: step time server 192.168.92.128 offset 383.287000 sec
server 192.168.92.128, stratum 3, offset 383.287006, delay 0.02591
 7 Aug 06:50:47 ntpdate[110443]: step time server 192.168.92.128 offset 383.287006 sec
server 192.168.92.128, stratum 3, offset 383.287000, delay 0.02586
 7 Aug 06:50:54 ntpdate[110445]: step time server 192.168.92.128 offset 383.287000 sec
server 192.168.92.128, stratum 3, offset 383.286995, delay 0.02583
 7 Aug 06:51:01 ntpdate[110455]: step time server 192.168.92.128 offset 383.286995 sec
server 192.168.92.128, stratum 3, offset 383.286954, delay 0.02589
 7 Aug 06:51:08 ntpdate[110457]: step time server 192.168.92.128 offset 383.286954 sec

时间差是383s,并通过截图可以看出,时间差是在持续缩小的,说明正在同步时间,持续观察20min
在这里插入图片描述
启动时间是

server 192.168.92.128, stratum 3, offset 383.287000, delay 0.02589
 7 Aug 06:50:40 ntpdate[110420]: step time server 192.168.92.128 offset 383.287000 sec

持续了20min后

server 192.168.92.128, stratum 3, offset 383.065839, delay 0.02583
 7 Aug 07:14:10 ntpdate[130815]: step time server 192.168.92.128 offset 383.065839 sec
Sun Aug  7 07:14:11 PDT 2022

比较稳定,该种场景下,应该能够持续稳定的实现平滑追时间。

2.4.1.1 场景1.2:当前节点和时钟源时间差别在600s-1000s,能否稳定追上?

人工设置当前节点的时间,跟时钟源的节点时间相差在600s-1000s(733s),并观察20min,验证能否稳定在追时间。按照计算,追时间的速度是0.5ms/1s(需要2000s时间追1s),大约需要733*2000/3600=407.7h才能追完

根据截图,确实也在平滑的追数据,观察20min
在这里插入图片描述
启动时间

server 192.168.92.128, stratum 3, offset 733.321759, delay 0.02589
 7 Aug 07:12:44 ntpdate[6681]: step time server 192.168.92.128 offset 733.321759 sec
Sun Aug  7 07:12:45 PDT 2022

跳变时间

server 192.168.92.128, stratum 3, offset 732.909001, delay 0.02591
 7 Aug 07:26:36 ntpdate[7262]: step time server 192.168.92.128 offset 732.909001 sec
Sun Aug  7 07:26:37 PDT 2022

在这里插入图片描述
当时间为Sun Aug 7 07:26:37 PDT 2022时,也就是说,追数据的时间只持续了15min,发生了跳变,直接跟时钟源的时间相同!!! 。

2.4.1.1 场景1.3:当前节点和时钟源时间差别在1000s以上,能否稳定追上?

人工设置当前节点的时间,跟时钟源的节点时间相差1000s以上(518415s),并观察20min,验证能否稳定在追时间。按照计算,追时间的速度是0.5ms/1s(需要2000s时间追1s),大约需要518415*2000/3600288088h才能追完。

根据截图,确实也在平滑的追数据,观察20min
在这里插入图片描述
启动时间

[root@localhost etc]# date -s 'Sun Aug  1 07:31:48 PDT 2022'
Mon Aug  1 07:31:48 PDT 2022

开始接受

server 192.168.92.128, stratum 3, offset 733.321759, delay 0.02589
 7 Aug 07:12:44 ntpdate[6681]: step time server 192.168.92.128 offset 733.321759 sec
Sun Aug  7 07:12:45 PDT 2022

第一次同步时间

cat /var/log/messags
Aug  1 07:40:17 localhost ntpd[34356]: 0.0.0.0 0613 03 spike_detect +518415.746602 s

跳变时间

server 192.168.92.128, stratum 3, offset 518415.741053, delay 0.02594
 7 Aug 07:54:10 ntpdate[35608]: step time server 192.168.92.128 offset 518415.741053 sec
Sun Aug  7 07:54:11 PDT 2022

在这里插入图片描述

当时间为Sun Aug 7 07:26:37 PDT 2022时,也就是说,追数据的时间只持续了15min,发生了跳变,直接跟时钟源的时间相同!! 。

本次测试使用的是bash -g -x模式,在ntpd服务运行过程中第1次发生时间跳变,因此ntpd服务并不主动退出。如果在进程运行的范围内,再次出现1000s以外并且持续10min以上,则ntpd会主动退出。

bash -g -x模式,1次跳变,ntpd服务不退出
在这里插入图片描述
2次跳变,时间差超过1000s并且持续10min,ntpd服务直接退出
在这里插入图片描述

经过测试,如果单纯使用bash -x模式,时间差超过1000s并且持续10min,ntpd服务会直接退出。
在这里插入图片描述

2.4.2 场景2:如果进行时间同步期间,ntpd服务发生了重启,是否会引发时间跳变?

关键参数配置为bash -g -x,如果重启ntpd服务会引发跳变,直接直接跟时钟源的时间相同

[root@host-2 ~]# cat /etc/sysconfig/ntpd
OPTIONS="-g -x" 
[root@host-2 ~]#

在这里插入图片描述

关键参数配置为bash -x,如果重启ntpd服务会引发跳变,直接直接跟时钟源的时间相同。如果是时间相差超过1000s,则会启动失败,需要人工同步时间后再启动。
在这里插入图片描述
查看/var/log/messages日志,发现必须要求在1000s内,才能启动,这是bash -g的作用
在这里插入图片描述
总结一下,ntpd模式下,如果ntpd服务重启,节点时间的表现情况
|–|–|
| -g | 时间直接跳变到跟时钟源一致,ntpd正常启动 |
| -x | ntpd无法启动,需要人工将时间设置到1000s以内,才能启动 |
| -g -x |时间直接跳变到跟时钟源一致,ntpd正常启动|

三、chronyd模式

3.1 版本信息

chronyd时ntpd的加强版本,本文不重点讨论2者的区别,而通过验证chronyd模式下,在平滑对时过程中的表现。本文使用的版本是 3.4 (+READLINE +SECHASH +IPV6 +DEBUG),通过yum安装完成

root@host-2 chrony]# chronyc -v
chronyc (chrony) version 3.4 (+READLINE +SECHASH +IPV6 +DEBUG)

3.2 配置chronyd对时源

通过启动服务器192.168.82.128的chronyd服务,并配置本节点作为系统内部的对时源

3.2.1 配置chronyd服务配置,启动chronyd服务

具体的配置如下

[root@host-1 ~]# cat /etc/chrony.conf 
# Use public servers from the pool.ntp.org project.
# Please consider joining the pool (http://www.pool.ntp.org/join.html).
server time.cloud.tencent.com iburst
server time1.cloud.tencent.com iburst
server time2.cloud.tencent.com iburst
server time3.cloud.tencent.com iburst
server time4.cloud.tencent.com iburst
server time5.cloud.tencent.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 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

其中设置本节点作为系统的对时源,关键配置是

# 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

查看服务启动情况

oot@host-1 ~]# systemctl status chronyd
● chronyd.service - NTP client/server
   Loaded: loaded (/usr/lib/systemd/system/chronyd.service; enabled; vendor preset: enabled)
   Active: active (running) since Sun 2022-08-07 02:17:22 PDT; 1h 57min ago
     Docs: man:chronyd(8)
           man:chrony.conf(5)
  Process: 36016 ExecStartPost=/usr/libexec/chrony-helper update-daemon (code=exited, status=0/SUCCESS)
  Process: 36011 ExecStart=/usr/sbin/chronyd $OPTIONS (code=exited, status=0/SUCCESS)
 Main PID: 36014 (chronyd)
    Tasks: 1
   Memory: 440.0K
   CGroup: /system.slice/chronyd.service
           └─36014 /usr/sbin/chronyd

Aug 07 02:17:22 host-1 systemd[1]: Stopping NTP client/server...
Aug 07 02:17:22 host-1 systemd[1]: Stopped NTP client/server.
Aug 07 02:17:22 host-1 systemd[1]: Starting NTP client/server...
Aug 07 02:17:22 host-1 chronyd[36014]: chronyd version 3.4 starting (+CMDMON +NTP +REFCLOCK +RTC +PRIVDROP +SCFILTER +SIGND +ASYNCDNS +SECHASH +IPV6 +DEBUG)
Aug 07 02:17:22 host-1 chronyd[36014]: Frequency -7.495 +/- 6.949 ppm read from /var/lib/chrony/drift
Aug 07 02:17:22 host-1 systemd[1]: Started NTP client/server.
Aug 07 02:17:28 host-1 chronyd[36014]: Selected source 134.175.254.134
Aug 07 02:18:33 host-1 chronyd[36014]: Selected source 134.175.253.104
[root@host-1 ~]# 

3.3.2 查看对时情况

查看对时源的同步情况

[root@host-1 ~]# chronyc sources
210 Number of sources = 5
MS Name/IP address         Stratum Poll Reach LastRx Last sample               
===============================================================================
^+ 139.199.215.251               2  10   277   113   -234us[ -234us] +/-   47ms
^+ 111.230.189.174               2  10   333    91   -900us[ -900us] +/-   50ms
^+ 139.199.214.202               2  10   377   22m   -392us[ -577us] +/-   65ms
^+ 134.175.254.134               2  10   377   560  -1356us[-1356us] +/-   48ms
^* 134.175.253.104               2  10   277   21m  -1578us[-1765us] +/-   35ms

查看时钟同步情况

[root@host-1 ~]# chronyc tracking
Reference ID    : 86AFFD68 (134.175.253.104)
Stratum         : 3
Ref time (UTC)  : Sun Aug 07 10:51:11 2022
System time     : 0.000320631 seconds slow of NTP time
Last offset     : -0.000187386 seconds
RMS offset      : 0.000809337 seconds
Frequency       : 8.419 ppm slow
Residual freq   : -0.008 ppm
Skew            : 0.413 ppm
Root delay      : 0.035052553 seconds
Root dispersion : 0.010563156 seconds
Update interval : 1026.9 seconds
Leap status     : Normal
[root@host-1 ~]# 

其中System time配置是表示,当前节点和时钟源时间的时间差异,本节点只有0.000320631 seconds,说明对时已经完成。至此,已经完成系统内部时钟源的配置,可以为系统内部的其他节点提供对时服务。

3.3 配置chronyd客户端

通过启动服务器192.168.82.129的chronyd服务,作为客户端,并设置192.168.82.128作为对时源,并进行时钟同步。

3.3.1 配置chronyd服务配置,启动chronyd服务

具体的配置如下

[root@host-2 chrony]# cat /etc/chrony.conf 
# Use public servers from the pool.ntp.org project.
# Please consider joining the pool (http://www.pool.ntp.org/join.html).
server 192.168.92.128 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 99999999999 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

stratumweight 0
driftfile /var/lib/chrony/drift
rtcsync
bindcmdaddress 127.0.0.1
bindcmdaddress ::1
keyfile /etc/chrony.keys
commandkey 1
generatecommandkey
noclientlog
logchange 0.5
logdir /var/log/chrony
maxupdateskew 500
maxslewrate 500

[root@host-2 chrony]# 

关键配置列表如下
server 192.168.92.128 iburst 设置对时源,本节点设置为** 192.168.92.128**,并进行时钟同步
makestep 99999999999 3 这个配置很关键,设置进行时间跳变的时间差,以及跳变次数。在启动chronyd服务时,会检查当前节点的时钟和对时源的时钟差异,如果超出时钟差阈值(99999999999s),则会通过3次时间跳变,进行时间的强制同步(这一点很重要,在生产系统内,应该是不允许这种情况发生的
maxupdateskew 500 

设置平滑追数据的频率
maxslewrate 500

查看服务启动情况

oot@host-1 ~]# systemctl status chronyd
● chronyd.service - NTP client/server
   Loaded: loaded (/usr/lib/systemd/system/chronyd.service; enabled; vendor preset: enabled)
   Active: active (running) since Sun 2022-08-07 02:17:22 PDT; 1h 57min ago
     Docs: man:chronyd(8)
           man:chrony.conf(5)
  Process: 36016 ExecStartPost=/usr/libexec/chrony-helper update-daemon (code=exited, status=0/SUCCESS)
  Process: 36011 ExecStart=/usr/sbin/chronyd $OPTIONS (code=exited, status=0/SUCCESS)
 Main PID: 36014 (chronyd)
    Tasks: 1
   Memory: 440.0K
   CGroup: /system.slice/chronyd.service
           └─36014 /usr/sbin/chronyd

Aug 07 02:17:22 host-1 systemd[1]: Stopping NTP client/server...
Aug 07 02:17:22 host-1 systemd[1]: Stopped NTP client/server.
Aug 07 02:17:22 host-1 systemd[1]: Starting NTP client/server...
Aug 07 02:17:22 host-1 chronyd[36014]: chronyd version 3.4 starting (+CMDMON +NTP +REFCLOCK +RTC +PRIVDROP +SCFILTER +SIGND +ASYNCDNS +SECHASH +IPV6 +DEBUG)
Aug 07 02:17:22 host-1 chronyd[36014]: Frequency -7.495 +/- 6.949 ppm read from /var/lib/chrony/drift
Aug 07 02:17:22 host-1 systemd[1]: Started NTP client/server.
Aug 07 02:17:28 host-1 chronyd[36014]: Selected source 134.175.254.134
Aug 07 02:18:33 host-1 chronyd[36014]: Selected source 134.175.253.104
[root@host-1 ~]# 

3.3.2 查看对时情况

查看对时源的同步情况

[root@host-2 chrony]# chronyc sources
210 Number of sources = 1
MS Name/IP address         Stratum Poll Reach LastRx Last sample               
===============================================================================
^* 192.168.92.128                3   6    17     9  +8390ns[  +31us] +/-   25ms
[root@host-2 chrony]# 

查看时钟同步情况

[root@host-2 chrony]# chronyc tracking
Reference ID    : C0A85C80 (192.168.92.128)
Stratum         : 4
Ref time (UTC)  : Sun Aug 07 11:33:14 2022
System time     : 0.000000021 seconds slow of NTP time
Last offset     : +0.000022861 seconds
RMS offset      : 0.000022861 seconds
Frequency       : 7.997 ppm slow
Residual freq   : +3.821 ppm
Skew            : 0.423 ppm
Root delay      : 0.039650649 seconds
Root dispersion : 0.005133089 seconds
Update interval : 2.0 seconds
Leap status     : Normal
[root@host-2 chrony]# 

其中System time配置是表示,当前节点和时钟源时间的时间差异,本节点只有0.000000021 seconds,说明对时已经完成。

3.4 关键场景

如下几个问题,是生产环境中最关注的问题。大致如下

3.4.1 场景1:能否持续有效的长时间进行平滑同步,直到时间追上了?

在启动192.168.31.129节点的chronyd服务后,人工设置当前节点的时间,使其小于时钟源的时间

[root@host-2 chrony]# date -s 'Fri Aug  4 18:30:08 PDT 2022'
Thu Aug  4 18:30:08 PDT 2022
[root@host-2 chrony]# date
Thu Aug  4 18:30:27 PDT 2022
[root@host-2 chrony]# 

查看对时情况

[root@host-2 chrony]# chronyc  sources
210 Number of sources = 1
MS Name/IP address         Stratum Poll Reach LastRx Last sample               
===============================================================================
^? 192.168.92.128                3   6   377     8   -3489m[ -3489m] +/-   25ms
[root@host-2 chrony]# 
[root@host-2 chrony]# chronyc tracking
Reference ID    : C0A85C80 (192.168.92.128)
Stratum         : 4
Ref time (UTC)  : Fri Aug 05 01:28:06 2022
System time     : 0.000000000 seconds slow of NTP time
Last offset     : +0.000000808 seconds
RMS offset      : 0.000033040 seconds
Frequency       : 8.521 ppm slow
Residual freq   : -0.002 ppm
Skew            : 0.365 ppm
Root delay      : 0.039602775 seconds
Root dispersion : 0.005342626 seconds
Update interval : 64.4 seconds
Leap status     : Normal
[root@host-2 chrony]# 

并没有马上完成时间对时,稍微等几分钟,等他完成对时
在这里插入图片描述
大约等待了1min后,时钟源对时上了,并开始进行平滑时间同步
在这里插入图片描述
[root@host-2 ~]# while [[ 1 ]];do chronyc tracking|grep ‘System time’;sleep 1;done
System time : 209367.500000000 seconds slow of NTP time
System time : 209367.500000000 seconds slow of NTP time
System time : 209367.500000000 seconds slow of NTP time
System time : 209367.500000000 seconds slow of NTP time
System time : 209367.500000000 seconds slow of NTP time
在这里插入图片描述
时间在缩小,说明确实在进行平滑的追时间。

经过实际的验证测试,持续同步2h(更多时间没有持续验证,有时间进一步验证),并没有出现跳变的情况,平滑同步时间表现稳定,符合预期

3.4.2 场景2:如果进行时间同步期间,chronyd服务发生了重启,是否会引发时间跳变?

这里涉及到关键配置makestep 99999999999 3,由于当前节点跟时钟源的时间差是209367.500000000s,并没有超出99999999999 s,因此没有触发时间跳变的逻辑,需要重点注意!!!

当前节点跟时钟源的时间差是209367.500000000s,配置makestep 99999999999 3重启chronyd服务,不会引发时间跳变
在这里插入图片描述

作为验证对比,我们将makestep 99999999999 3设置为makestep 1 3,重启chronyd服务,会引发当前节点的时间跳变
在这里插入图片描述


四、总结

4.1 ntpd模式下

4.1.1 平滑追时间表现

不同时间范围情况下, ntpd平滑追时间的表现

时间范围备注
0-600s追时间比较平稳,时间平稳
600s-1000s平稳追15分钟后,会发生跳变,时间跳变成时钟源相同
1000s以上平稳追15分钟,会发生跳变,时间跳变成时钟源相同。如果此后时间再次出现1000s以上并持续10min,ntpd进程会退出

4.1.2 发生服务重启表现

当前节点与时钟源的时间超过1000s时,3种模式下的重启ntpd服务

模式备注
-g时间直接跳变到跟时钟源一致,ntpd正常启动
-xntpd无法启动,需要人工将时间设置到1000s以内,才能启动
-g -x时间直接跳变到跟时钟源一致,ntpd正常启动

4.2 chronyd模式下

4.2.1 平滑追时间表现

时间范围备注
任意时间范围追时间比较平稳,时间平稳

4.2.2 发生服务重启表现

如果在追时间期间重启chronyd服务
需要注意设置makestop配置,只需要阈值高于时间差,则不会触发跳变,反正则会触发跳变

4.3 建议

日常生产环境过程,节点的时间跟时钟源相差几天是比较常见的现象,在进行追时间时建议使用chrony服务进行,比较稳定可靠。ntpd服务的设计,就是对当前节点和时钟源的时间差有要求,从理念上要去两者需要在1000s以内,否则则认为不符合生产条件,应当人工干预修复。

转载至Linux时间延迟平滑对时方案的分析-Ntp和Chrony的不同表现 – 源码巴士

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Linux时间延迟平滑对时方案的分析-Ntp和Chrony的不同表现 的相关文章

  • arm-linux-gnueabi 编译器选项

    我在用 ARM Linux gnueabi gcc在 Linux 中为 ARM 处理器编译 C 程序 但是 我不确定它编译的默认 ARM 模式是什么 例如 对于 C 代码 test c unsigned int main return 0x
  • 在 Linux 上的 Python 中使用受密码保护的 Excel 工作表

    问题很简单 我每周都会收到一堆受密码保护的 Excel 文件 我必须解析它们并使用 Python 将某些部分写入新文件 我得到了文件的密码 当在 Windows 上完成此操作时 处理起来很简单 我只需导入 win32com 并使用 clie
  • .NET Core 中的跨平台文件名处理

    如何处理文件名System IO以跨平台方式运行类以使其在 Windows 和 Linux 上运行 例如 我编写的代码在 Windows 上完美运行 但它不会在 Ubuntu Linux 上创建文件 var tempFilename Dat
  • 如何更改 Ubuntu 14.04 上的 php-cli 版本?

    我是 Linux 新手 在篡改时破坏了一些 php 设置 如果我执行一个包含以下内容的 php 脚本 phpinfo 它显示 php 版本为 5 6 但通过命令行 如果我运行php v它返回 7 0 版本 我想让两个版本匹配 我怎样才能修复
  • 嵌入式Linux poll()不断返回

    我有一个特别的问题 当我知道没有什么可读时 民意调查不断返回 因此设置如下 我有 2 个文件描述符 它们构成fd设置民意调查监视 一种用于引脚从高到低的变化 GPIO 另一个用于代理输入 代理输入出现问题 处理的顺序是 启动main函数 然
  • Locale.getDefault() 始终返回 en

    unix 机器上的服务器始终使用 en 作为默认区域设置 以下是区域设置输出 LANG en US LC CTYPE C LC NUMERIC C LC TIME C LC COLLATE C LC MONETARY C LC MESSAG
  • 拆分字符串以仅获取前 5 个字符

    我想去那个地点 var log src ap kernelmodule 10 001 100 但看起来我的代码必须处理 ap kernelmodule 10 002 100 ap kernelmodule 10 003 101 等 我想使用
  • 从 PL/SQL 调用 shell 脚本,但 shell 以 grid 用户而非 oracle 身份执行

    我正在尝试使用 Runtime getRuntime exec 从 Oracle 数据库内部执行 shell 脚本 在 Red Hat 5 5 上运行的 Oracle 11 2 0 4 EE CREATE OR REPLACE proced
  • Linux 中的无缓冲 I/O

    我正在写入大量的数据 这些数据数周内都不会再次读取 由于我的程序运行 机器上的可用内存量 显示为 空闲 或 顶部 很快下降 我的内存量应用程序使用量不会增加 其他进程使用的内存量也不会增加 这让我相信内存正在被文件系统缓存消耗 因为我不打算
  • 使用 grep 查找包含所有搜索字符串的行

    我有一个文件 其中包含很多与此类似的行 id 2796 some model Profile message type MODEL SAVE fields account 14 address null modification times
  • 如何使用 bash 锁定文件

    我有一个任务从远程服务器同步目录 rsync av email protected cdn cgi l email protection srv data srv data 为了使其定期运行并避免脚本 reEnter 问题 我使用 rsyn
  • 如何在 Linux 中编写文本模式 GUI? [关闭]

    Closed 这个问题正在寻求书籍 工具 软件库等的推荐 不满足堆栈溢出指南 help closed questions 目前不接受答案 当我编写脚本 程序时 我经常想弹出一个简单的文本 gui 来提示输入 我该怎么做 例如 来自 Shel
  • chown:不允许操作

    我有问题 我需要通过 php 脚本为系统中的不同用户设置文件所有者权限 所以我通过以下命令执行此操作 其中 1002 是系统的用户 ID file put contents filename content system chown 100
  • 如何在数组中存储包含双引号的命令参数?

    我有一个 Bash 脚本 它生成 存储和修改数组中的值 这些值稍后用作命令的参数 对于 MCVE 我想到了任意命令bash c echo 0 0 echo 1 1 这解释了我的问题 我将用两个参数调用我的命令 option1 without
  • 如何使用GDB修改内存内容?

    我知道我们可以使用几个命令来访问和读取内存 例如 print p x 但是如何更改任何特定位置的内存内容 在 GDB 中调试时 最简单的是设置程序变量 参见GDB 分配 http sourceware org gdb current onl
  • 添加要在给定命令中运行的 .env 变量

    我有一个 env 文件 其中包含如下变量 HELLO world SOMETHING nothing 前几天我发现了这个很棒的脚本 它将这些变量放入当前会话中 所以当我运行这样的东西时 cat env grep v xargs node t
  • arm64和armhf有什么区别?

    Raspberry Pi Type 3 具有 64 位 CPU 但其架构不是arm64 but armhf 有什么区别arm64 and armhf armhf代表 arm hard float 是给定的名称Debian 端口 https
  • Discord.net 无法在 Linux 上运行

    我正在尝试让在 Linux VPS 上运行的 Discord net 中编码的不和谐机器人 我通过单声道运行 但我不断收到此错误 Unhandled Exception System Exception Connection lost at
  • 无法使用 wget 在 CentOS 机器上安装 oracle jdk

    我想在CentOS上安装oracle java jdk 8 我无法安装 java jdk 因为当我尝试使用命令安装 java jdk 时 root ADARSH PROD1 wget no cookies no check certific
  • 两种情况或 if 哪个更快? [关闭]

    Closed 这个问题不符合堆栈溢出指南 help closed questions 目前不接受答案 我必须制作一个 非常 轻的脚本 它将接受用户的选项并调用脚本中的函数来执行一些任务 现在我可以使用 IF 和 CASE 选项 但我想知道两

随机推荐

  • Pytorch深度学习常用预训练网络模型的下载地址

    Resnet model urls 61 resnet18 https download pytorch org models resnet18 5c106cde pth resnet34 https download pytorch or
  • matlab 使用pointcloud类在geotiff文件的基础上选择点

    Import all 4 5 million points from a laz file pc 61 pointCloud 39 ALS strip laz 39 Plot all points of point cloud pc plo
  • Flutter报错:Android toolchain “but Android SDK not found at this location”

    Flutter报错 Android toolchain but Android SDK not found at this location 当前环境 macOS 10 13 6Android Studio 3 5Android Studi
  • matlab 点云滤波(中值、均值、高斯滤波)代码

    点云中值 均值 高斯滤波 介绍一下滤波函数 smoothdata 对含噪数据进行平滑处理 B 61 smoothdata method 为上述任一语法指定平滑处理方法 例如 xff0c B 61 smoothdata A 39 sgolay
  • 时间复杂度

    1 时间复杂度 在进行算法分析时 xff0c 语句总的执行次数T n xff09 是关于问题规模n的函数 xff0c 然后分析T n xff09 随n的变化 这样用大写的O来标记算法的时间复杂度 xff0c 称之为大O Order的简写 x
  • 解决电脑80端口被占用问题!亲测有用!!!

    方法1 xff1a 1 在桌面 xff0c 按下 WINDOW 43 R 快捷键 2 在弹出的命令行窗口下执行下面的命令 xff1a cmd xff0c 点击确定 3 输入net stop http xff0c 点击确定 xff0c 再次输
  • linux 下 常见 知识点

    1 etc resolv conf ping通baidu com DNS 域名解析服务器 xff01 该文件是DNS域名解析的配置文件 xff0c 它的格式很简单 xff0c 每行以一个关键字开头 xff0c 后接配置参数 resolv c
  • 学习51单片机经历

    学习51单片机的记录 文章目录 前言一 所使用的软硬件设备二 学习课程三 学习内容1 软件安装2 单片机基础3 真正开始学习 xff08 一 xff09 LED灯 总结修改日志 前言 提示 xff1a 只是记载本人学习51单片机的经历 xf
  • LED灯系列

    LED灯系列 本系列为stc89c52RC单片机开发板上LED灯相关操作思路和代码 点此链接转到专栏目录链接 http t csdn cn skf2j 文章目录 LED灯系列1 xff09 原理2 xff09 单个LED灯常亮3 xff09
  • Angular CLI的安装报错

    一 安装步骤 angular cli它是angular框架官方的一个构建工具 xff0c 当你使用 ng new xxx 项目名 创建一个项目时 xff0c 会自动生成项目结构 Angular CLI用于简单 xff0c 快速构建Angul
  • 最新Anaconda3的安装配置及使用教程

    安装记录参考链接 出现问题 xff1a 镜像地址无效 不知道 设置Anaconda镜像 那一步我哪里没有操作好 答 步骤错了 正确步骤 xff1a 1 先执行 conda config set show channel urls yes 生
  • Linux 环境变量:DISPLAY

    1 dbvis no x11 display variable was set but this program performed an operation which requires it solution export DISPLA
  • PostgreSQL入门-安装与基本使用(Ubuntu)

    PostgreSQL入门 安装与基本使用 xff08 Ubuntu xff09 PostgreSQL 是一个免费的对象 关系数据库服务器 ORDBMS xff0c 号称是 世界上最先进的开源关系型数据库 PostgreSQL 是以加州大学计
  • windows下的pycharm项目部署同步到Linux下的pycharm开发

    windows下的pycharm项目部署同步到Linux下的pycharm开发 一 运行平台二 SSH连接远程服务器 一 运行平台 项目部署系统 xff1a UOS 项目开发平台 xff1a windows10 IDE xff1a PyCh
  • redhat操作系统升级红帽6升级到红帽7linux升级

    本文档介绍了从 RHEL 6 到 RHEL 7 就地升级的步骤 支持的就地升级路径是从 RHEL 6 10 到 RHEL 7 9 如果您的 RHEL 系统满足以下条件 xff0c 则完全支持此 RHEL 6 到 RHEL 7 升级过程 xf
  • Kubernetes角色访问控制RBAC和权限规则(Role+ClusterRole)---好文

    基于角色的访问控制 xff08 Role Based Access Control 即 RBAC xff09 使用 rbac authorization k8s io API Group实现授权决策 xff0c 允许管理员通过Kuberne
  • 你知道abrt-hook-ccpp吗

    1 什么是abrt hook ccpp abrt也可以叫abrtd xff0c 展开应该是automatically bug report daemon xff0c 也就是自动错误报告守护进程 从字面意义就可以看出 xff0c 他是一个守护
  • Mobaxterm 使用图形界面

    redhat 7 yum install firefox xorg x11 xauth mesa libGLES devel x86 64 mesa dri drivers vi etc ssh sshd config X11Forward
  • Linux ❉ Chronyd时间同步服务器详解

    一 介绍 1 简介 集群中节点之间需要时间同步 xff0c Chronyd不依赖外部的时间服务NTP xff0c 在内部搭建时间服务器 Chrony是网络时间协议 xff08 NTP xff09 的一种实现 xff0c 是一个类Unix系统
  • Linux时间延迟平滑对时方案的分析-Ntp和Chrony的不同表现

    系列文章目录 文章目录 系列文章目录前言一 环境信息二 ntpd模式 2 1 版本信息2 2 配置ntpd对时源 2 2 1 配置ntpd服务配置 xff0c 启动ntpd服务2 3 2 查看对时情况2 3 配置ntpd客户端 2 3 1