NTP配置集群受控节点从管理节点同步时间

背景知识

管理节点会通过各种协议与受控节点通信,为了确保安全,受控节点接到命令后会进行一系列的校验,如果两机的时间不同步的话,会导致命令拒绝执行。NTP服务用于保证整套系统内所有主机的时间一致。
https://weread.qq.com/web/reader/bb432ae072176e64bb42f2a

在对时间的同步上,NTP采用的是:一对一连接且双方均可进行时间同步的主/被动工作模式,一对一连接且客户端由服务器端进行时间同步的客户端/服务器模式和一对多的广播/多播更新时间模式。在主/被动工作模式下,只要一方获取精准的时间源就可以对对方的时间进行更新;而在客户端/服务器模式下,只有客户端的时间被服务器端同步。在多数情况下,NTP 使用的工作模式是客户端/服务器模式。
https://weread.qq.com/web/reader/564322505b213e56438bd55

NTP有3种工作模式:客户机/服务器模式、对称模式和广播/多播模式,可根据需要进行选择。在客户机/服务器模式中,一对一连接,客户机可以被服务器同步,服务器不能被客户机同步。在对称模式中,与客户机/服务器模式基本相同,但双方均可同步对方或被对方同步,先发出申请建立连接的一方工作在主动模式下,另一方工作在被动模式下。广播/多播模式是一对多的连接,服务器主动发出时间信息,客户由此信息调整自己的时间。由于忽略网络时延,精度较低,适合用于高速局域网上。

NTP协议以UTC作为时间标准,使用层次式分布模型,时间按NTP服务器的等级传播。

网络中的设备可以扮演多重角色。例如一个第二层的设备,对于第一层来说是客户机;对于第三层可能是服务器;对于同层的设备则可以是对等机(相互用NTP进行同步)​。

局域网内的时间同步

如果只是需要在本局域网内进行系统间的时间同步,那么就可以使用局域网中任何一个系统的时钟。你需要选择局域网中的一个节点的时钟作为“权威的”的时间源,然后其他的节点就只需要与这个时间源进行时间同步即可。使用这种方式,所有的节点都会使用一个公共的系统时钟,但是不需要和局域网外的系统进行时间同步。如果一个系统在一个局域网的内部,同时又不能使用无线时钟,这种方式是最好的选择。

以上出自:https://weread.qq.com/web/reader/1683228059fad81680e6789kf09320f026af0935e4cd23d

设置本地时间源

设置本地时间源如果要以本机的系统时钟作为时间源,或者使用一个本地的时钟设置来校正时间,可以用“server 127.127.1.0”语句来指定使用本地时间源。这里的fudge语句将本地时间源的层号设置为10,由于本地时间源的层号较大,也就是说其优先级比较低,所以当有外部时钟时,通常都使用外部时间源进行同步。
https://weread.qq.com/web/reader/1683228059fad81680e6789ka3c320b026ca3c65c297000

解说:1. 集群同步时间这个场景下,我们使用:客户端/服务器模式、局域网内的时间同步;2. ntp没有客户端的做法,都是配置/etc/ntp.conf文件,如果此文件中定义了一个或多个:server <时间服务器>,则从这些server中同步时间(多个时间时,有计算策略);如果没配置,则不同步时间。

ntp服务的2个重要配置文件:/etc/ntp.conf、/etc/ntp/step-tickers。

实践操作

以一个管理节点、一个受控节点,为例。
管理节点ip:10.0.16.16,受控节点ip:10.0.4.17
2个节点的/etc/hosts文件配置:

127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6

10.0.16.16 master01
10.0.4.17  node01

master01上配置

/etc/ntp.conf文件内容:

driftfile /var/lib/ntp/drift
logfile   /var/log/ntp.log

# 重要配置;允许所有的ntp请求包进入,default表示所有IP
restrict default

restrict 127.0.0.1 
restrict -6 ::1

# 重要配置;在没有上层NTP服务器的情况下,使用本地时钟作为NTP时间源提供时间同步服务;设置本地时间源,因为此文件中没有设置:server <时间服务器>,因此不会从外部时间源同步时间。
server 127.127.1.0
fudge 127.127.1.0 stratum 10

interface ignore  wildcard

# 重要配置;ntpd服务器监听所有的IP地址
interface listen  0.0.0.0

includefile /etc/ntp/crypto/pw

keys /etc/ntp/keys

disable monitor

/etc/ntp/step-tickers文件内容(选择局域网中的管理节点的时钟作为“权威的”的时间源,然后其他的节点就只需要与这个时间源进行时间同步即可,因此管理节点在此文件中不用配置要同步的时间服务器)

# List of NTP servers used by the ntpdate service.

node01上配置

/etc/ntp.conf文件内容:

driftfile /var/lib/ntp/drift
logfile   /var/log/ntp.log
 
restrict default nomodify notrap nopeer noquery
 
restrict 127.0.0.1
restrict ::1

# 同步管理节点master01的时间
server 10.0.16.16 minpoll 3 maxpoll 4
 
includefile /etc/ntp/crypto/pw
keys /etc/ntp/keys
disable monitor

/etc/ntp/step-tickers文件内容(同步管理节点master01的时间):

cat /etc/ntp/step-tickers 
# List of NTP servers used by the ntpdate service.

10.0.16.16

ntp操作命令

service ntpd start
service ntpd stop
service ntpd restart
ntpstat命令执行:

[root@master01 ntp]# ntpstat
synchronised to local net (127.127.1.0) at stratum 11
   time correct to within 12 ms
   polling server every 64 s
[root@node01 ~]# ntpstat
synchronised to NTP server (10.0.16.16) at stratum 12
   time correct to within 13 ms
   polling server every 16 s

ntpq -p命令执行

[root@master01 ntp]# ntpq -p
     remote           refid      st t when poll reach   delay   offset  jitter
==============================================================================
*LOCAL(0)        .LOCL.          10 l   63   64  377    0.000    0.000   0.000
[root@node01 ~]# ntpq -p
     remote           refid      st t when poll reach   delay   offset  jitter
==============================================================================
*master01        LOCAL(0)        11 u   15   16  377    1.207    0.005   0.006

posted on 2024-08-17 21:13  cag2050  阅读(20)  评论(0编辑  收藏  举报

导航