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的不同表现 的相关文章

  • 在 execl 在输出中不可见之前打印

    include
  • 无法在 Linux 的 NetBeans 中编译 C++ 和 OpenGL (GLFW) 的简单源代码

    我开始学习 OpenGL glfw 我从教程中复制源代码并尝试编译它 但出现了错误 我想我已经正确安装了所有头文件 glm glfw 等 这是我的来源 我没有在头文件中使用这些字符 include iostream include stdi
  • Xvfb 冻结初始化 GLX 扩展

    我正在尝试运行无头 Xvfb 服务器来捕获 Amazon EC2 micro 上的屏幕截图 但它在 GLX 上陷入了困境 我使用此脚本安装了 GLX Xvfb 和所有库 https gist github com joekiller 414
  • 如何从脚本编辑 /etc/sudoers?

    我需要编辑 etc sudoers从脚本中添加 删除白名单中的内容 假设我有一个可以处理普通文件的命令 我如何将其应用到 etc sudoers 我可以复制并修改它 然后有visudo用修改后的副本替换原始版本 通过提供我自己的脚本 EDI
  • 如何通过ssh获取远程命令的退出代码

    我正在通过 ssh 从远程计算机运行脚本 ssh some cmd my script 现在 我想在本地计算机上存储 shell 脚本的退出状态 我该怎么做 假设没有任何问题ssh其本身 其退出状态是在远程主机上执行的最后一个命令的退出状态
  • bash 将输出重定向到文件,但结果不完整

    重定向命令输出的问题已经被问过很多次了 但是我有一个奇怪的行为 我使用的是 bash shell debian 版本 4 3 30 1 release 并尝试将输出重定向到文件 但并非所有内容都记录在文件中 我尝试运行的 bin 文件是 l
  • Ruby:在 Ubuntu 上安装 rmagick

    我正在尝试在 Ubuntu 10 04 上安装 RMagick 看起来here https stackoverflow com questions 1482823 is there an easy way to install rmagic
  • Linux shell 从用户输入中获取设备 ID

    我正在为一个程序编写安装脚本 该程序需要在其配置中使用 lsusb 的设备 ID 因此我正在考虑执行以下操作 usblist lsusb put the list into a array for each line use the arr
  • Crontab 每 5 分钟一次 [关闭]

    Closed 这个问题是无关 help closed questions 目前不接受答案 我如何告诉 crontab 每 5 分钟运行一次 但从每小时的第二分钟开始 换句话说 我想在以下时间执行我的脚本minute 5 2 例如 我的脚本应
  • LINUX:如何锁定内存中进程的页面

    我有一个 LINUX 服务器 运行一个具有大量内存占用的进程 某种数据库引擎 该进程分配的内存太大 需要将其中一部分换出 换出 我想做的是将所有其他进程 或正在运行的进程的子集 的内存页面锁定在内存中 以便只有数据库进程的页面被换出 例如
  • 如何从 C++ 程序中重新启动 Linux?

    我有一个 Qt 4 GUI 我需要在下拉菜单中提供一个选项 允许用户选择重新启动计算机 我意识到这对于以其他方式重新启动计算机的能力来说似乎是多余的 但选择需要保留在那里 我尝试使用 system 来调用以下内容 suid root she
  • 为 Linux 编译 Objective-C 应用程序(API 覆盖范围)

    我可能在这里问一些奇怪的问题 但我不确定从哪里开始 问题是我正在考虑使用 Obj C 和 Foundation 类在 Mac 上编写一个命令行工具 但存在一个非常大的风险 那就是我希望能够为不同的 Linux 发行版编译它 以便将来作为服务
  • Linux无法删除文件

    当我找到文件时 我在删除它们时遇到问题 任务 必须找到带有空格的文件并将其删除 我的尝试 rm find L root grep i 但我有错误 rm cannot remove root test No such file or dire
  • grep 排除文件的数组参数

    我想从我的文件中排除一些文件grep命令 为此我使用参数 exclude excluded file ext 为了更容易阅读 我想使用包含排除文件的 bash 数组 EXCLUDED FILES excluded file ext 然后将
  • 在 docker 中重定向命令输出

    我想为我的服务器做一些简单的日志记录 它是一个在 Docker 容器中运行的小型 Flask 应用程序 这是 Dockerfile Dockerfile FROM dreen flask MAINTAINER dreen WORKDIR s
  • ioctl 命令的用户权限检查

    我正在实现 char 驱动程序 Linux 并且我的驱动程序中有某些 IOCTL 命令仅需要由 ADMIN 执行 我的问题是如何在 ioctl 命令实现下检查用户权限并限制非特权用户访问 IOCTL 您可以使用bool capable in
  • 批量删除文件名中包含 BASH 中特殊字符的子字符串

    我的目录中有一个文件列表 opencv calib3d so2410 so opencv contrib so2410 so opencv core so2410 so opencv features2d so2410 so opencv
  • 执行命令而不将其保留在历史记录中[关闭]

    Closed 这个问题不符合堆栈溢出指南 help closed questions 目前不接受答案 在进行软件开发时 经常需要在命令行命令中包含机密信息 典型示例是将项目部署到服务器的凭据设置为环境变量 当我不想将某些命令存储在命令历史记
  • 有没有一种快速方法可以从 Jar/war 中删除文件,而无需提取 jar 并重新创建它?

    所以我需要从 jar war 文件中删除一个文件 我希望有类似 jar d myjar jar file I donot need txt 的内容 但现在我能看到从 Linux 命令行执行此操作的唯一方法 不使用 WinRAR Winzip
  • 如何使用 JSch 将多行命令输出存储到变量中

    所以 我有一段很好的代码 我很难理解 它允许我向我的服务器发送命令 并获得一行响应 该代码有效 但我想从服务器返回多行 主要类是 JSch jSch new JSch MyUserInfo ui new MyUserInfo String

随机推荐

  • 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