部署时间同步服务器

1、时间同步服务

多主机协作工作时,各个主机的时间同步很重要,时间不一致会造成很多重要应用的故障,如:加密协议,日志,集群等, 利用NTP(Network Time Protocol) 协议使网络中的各个计算机时间达到同步。

  • 时间同步软件实现:
    • ntp
      • 将系统时钟和世界协调时UTC同步,精度在局域网内可达0.1ms,在互联网上绝大多数的地方精度可以达到1-50ms
    • chrony
      • 实现NTP协议的的自由软件。可使系统时钟与NTP服务器,参考时钟(例如GPS接收器)以及使用手表和键盘的手动输入进行同步。还可以作为NTPv4(RFC 5905)服务器和对等体运行,为网络中的计算机提供时间服务。设计用于在各种条件下良好运行,包括间歇性和高度拥挤的网络连接,温度变化(计算机时钟对温度敏感),以及不能连续运行或在虚拟机上运行的系统。
      • 通过Internet同步的两台机器之间的典型精度在几毫秒之内,在LAN上,精度通常为几十微秒。利用硬件时间戳或硬件参考时钟,可实现亚微秒的精度。

2、chrony

2.1、chrony介绍

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

2.2、chrony 文件组成

  • 包:chrony
  • 两个主要程序:chronyd和chronyc
    • chronyd:后台运行的守护进程,用于调整内核中运行的系统时钟和时钟服务器同步。它确定计算机增减时间的比率,并对此进行补偿
    • chronyc:命令行用户工具,用于监控性能并进行多样化的配置。它可以在chronyd实例控制的计算机上工作,也可在一台不同的远程计算机上工作
  • 服务unit 文件: /usr/lib/systemd/system/chronyd.service
  • 监听端口: 服务端: 123/udp,客户端: 323/udp
  • 配置文件: /etc/chrony.conf

2.3、配置文件chrony.conf

server                       #可用于时钟服务器,iburst 选项当服务器可达时,发送一个八
                              个数据包而不是通常的一个数据包。 包间隔通常为2秒,可加快
                              初始同步速度
pool                         #该指令的语法与server 指令的语法相似,不同之处在于它用于
                              指定NTP服务器池而不是单个NTP服务器。池名称应解析为随时
                              间可能会变化的多个地址
driftfile                    #根据实际时间计算出计算机增减时间的比率,将它记录到一个文
                              件中,会在重启后为系统时钟作出补偿
rtcsync                      #启用内核模式,系统时间每11分钟会拷贝到实时时钟(RTC)
allow / deny                 #指定一台主机、子网,或者网络以允许或拒绝访问本服务器
cmdallow / cmddeny           #可以指定哪台主机可以通过chronyd使用控制命令
bindcmdaddress               #允许chronyd监听哪个接口来接收由chronyc执行的命令
makestep                     #通常chronyd将根据需求通过减慢或加速时钟,使得系统逐步纠
                              正所有时间偏差。在某些特定情况下,系统时钟可能会漂移过
                              快,导致该调整过程消耗很长的时间来纠正系统时钟。该指令强
                              制chronyd在调整期大于某个阀值时调整系统时钟
local stratum 10             #即使server指令中时间服务器不可用,也允许将本地时间作为
                              标准时间授时给其它客户端

2.4、NTP 客户端工具

# chronyc 可以运行在交互式和非交互式两种方式,支持以下子命令
help                命令可以查看更多chronyc的交互命令
accheck             检查是否对特定主机可访问当前服务器
activity            显示有多少NTP源在线/离线
sources [-v]        显示当前时间源的同步信息
sourcestats [-v]    显示当前时间源的同步统计信息
add server          手动添加一台新的NTP服务器
clients             报告已访问本服务器的客户端列表
delete              手动移除NTP服务器或对等服务器
settime             手动设置守护进程时间
tracking            显示系统时间信息
# 范例
[root@centos79-base01 ~]# chronyc activity
200 OK
4 sources online
0 sources offline
0 sources doing burst (return to online)
0 sources doing burst (return to offline)
0 sources with unknown address

[root@centos79-base01 ~]# chronyc sources
210 Number of sources = 4
MS Name/IP address         Stratum Poll Reach LastRx Last sample
===============================================================================
^* 119.28.206.193                2   6   377    99   +703us[+2789us] +/-   32ms
^- 68-69-221-61.regn.hsdb.s>     1   6   377    34    +17ms[  +17ms] +/-  131ms
^- ntp6.flashdance.cx            2   6     7    32  +1517us[+1517us] +/-  189ms
^+ tock.ntp.infomaniak.ch        1   6   377    36  +5597us[+5597us] +/-   94ms

[root@centos79-base01 ~]# chronyc sources -v
210 Number of sources = 4

  .-- Source mode  '^' = server, '=' = peer, '#' = local clock.
 / .- Source state '*' = current synced, '+' = combined , '-' = not combined,
| /   '?' = unreachable, 'x' = time may be in error, '~' = time too variable.
||                                                 .- xxxx [ yyyy ] +/- zzzz
||      Reachability register (octal) -.           |  xxxx = adjusted offset,
||      Log2(Polling interval) --.      |          |  yyyy = measured offset,
||                                \     |          |  zzzz = estimated error.
||                                 |    |           \
MS Name/IP address         Stratum Poll Reach LastRx Last sample
===============================================================================
^* 119.28.206.193                2   6   375    11   +317us[+2723us] +/-   51ms
^- 68-69-221-61.regn.hsdb.s>     1   6   377    11    +25ms[  +25ms] +/-  125ms
^- ntp6.flashdance.cx            2   6     7    73   -449us[+1517us] +/-  189ms
^+ tock.ntp.infomaniak.ch        1   6   377    11   +359us[+2762us] +/-   99ms

2.5、时间工具

# timedatectl 时间和时区管理

#查看日期时间、时区及NTP状态:
timedatectl

#查看时区列表:
timedatectl list-timezones

#修改时区:
timedatectl set-timezone Asia/Shanghai

#修改时区
rm -f /etc/localtime
ln -s /usr/share/zoneinfo/Asia/Shanghai /etc/localtime

#修改日期时间:
timedatectl set-time "2017-01-23 10:30:00"

#开启NTP:
timedatectl set-ntp true/false


# ntpdate 时间同步命令,CentOS8版本此命令已淘汰

# system-config-date:图形化配置chrony服务的工具

3、部署私有的时间服务器

# 服务器端配置
[root@centos79-base01 ~]# hostname -I
192.168.1.10

[root@centos79-base01 ~]# yum install -y chrony

[root@centos79-base01 ~]# grep -v "^#\|^$" /etc/chrony.conf
server ntp.aliyun.com iburst
driftfile /var/lib/chrony/drift
makestep 1.0 3
rtcsync
allow 192.168.1.0/24                #加此行,指定允许同步的网段
local stratum 10                    #删除此行注释,当互联网无法连接,仍然可以为客户端提
                                     供时间同步服务

logdir /var/log/chrony

# 服务启动后会打开端口123/udp
[root@centos79-base01 ~]# systemctl restart chronyd
[root@centos79-base01 ~]# ss -nlut
Netid  State      Recv-Q Send-Q                  Local Address:Port                                 Peer Address:Port
udp    UNCONN     0      0                                   *:943                                             *:*
udp    UNCONN     0      0                                   *:111                                             *:*
udp    UNCONN     0      0                                   *:123                                             *:*
udp    UNCONN     0      0                           127.0.0.1:323                                             *:*
udp    UNCONN     0      0                                [::]:943                                          [::]:*
udp    UNCONN     0      0                                [::]:111                                          [::]:*
udp    UNCONN     0      0                               [::1]:323                                          [::]:*

chronyc> activity
200 OK
1 sources online              
# 客户端配置
[root@centos79-base02 ~]# hostname -I
192.168.1.11

[root@centos79-base02 ~]# yum install -y chrony

[root@centos79-base02 ~]# vim /etc/chrony.conf
server 192.168.1.10 iburst

[root@centos79-base02 ~]# systemctl restart chronyd

[root@centos79-base02 ~]# chronyc sources -v
210 Number of sources = 1

  .-- Source mode  '^' = server, '=' = peer, '#' = local clock.
 / .- Source state '*' = current synced, '+' = combined , '-' = not combined,
| /   '?' = unreachable, 'x' = time may be in error, '~' = time too variable.
||                                                 .- xxxx [ yyyy ] +/- zzzz
||      Reachability register (octal) -.           |  xxxx = adjusted offset,
||      Log2(Polling interval) --.      |          |  yyyy = measured offset,
||                                \     |          |  zzzz = estimated error.
||                                 |    |           \
MS Name/IP address         Stratum Poll Reach LastRx Last sample
===============================================================================
^* 192.168.1.10                  3   6    17    22  -6629ns[ -553us] +/-   17ms
# 服务端验证
[root@centos79-base01 ~]# chronyc clients
Hostname                      NTP   Drop Int IntL Last     Cmd   Drop Int  Last
===============================================================================
192.168.1.11                    4      0   1   -    64       0      0   -     -

© 版权声明
THE END
喜欢就支持一下吧
点赞0 分享