(3)时间同步Chrony

提到分布式环境,就不得不考虑机器之间时间同步问题。今天我们就来串讲下基于Chrony的时间同步。

chrony是网络时间协议(NTP)的通用实现。它可以将系统时钟与NTP服务器进行同步。它还可以充当一个NTPv4(RFC 5905)服务器,用来向网络中的其他计算机提供时间服务。
在精度方面,通过Internet同步的两台计算机之间的典型精度在几毫秒内;在LAN上,精度通常为数十微秒。使用硬件时间戳或硬件参考时钟,可能会达到亚微秒的精度

对Chrony初步了解后,下面我们来介绍该如何使用它。

安装

先准备2台服务器

  • 192.168.1.68
  • 192.168.1.69
    68,69上均安装Chrony,区别是,68从公网ntp服务端同步,69从68上同步
yum -y install chrony
#安装后会/etc下生成2个文件,一个是配置相关,一个是做ntp认证的
ls /etc/ | grep chrony
chrony.conf
chrony.keys
配置chrony.conf
  • 192.168.1.68
vim /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 ntp1.aliyun.com iburst minpoll 4 maxpoll 10
server ntp2.aliyun.com iburst minpoll 4 maxpoll 10
server ntp3.aliyun.com iburst minpoll 4 maxpoll 10
server ntp4.aliyun.com iburst minpoll 4 maxpoll 10
server ntp5.aliyun.com iburst minpoll 4 maxpoll 10
server ntp6.aliyun.com iburst minpoll 4 maxpoll 10
server ntp7.aliyun.com iburst minpoll 4 maxpoll 10
# 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.1.0/24

# 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

  • server 指定可以用作时间源的NTP服务器。指令后紧跟服务器名称或其IP地址;参考阿里云的NTP服务
  • iburst 使用此选项,发送到服务器的前四个请求之间的间隔将为2秒或更短,而不是minpoll选项指定的间隔,这使chronyd在启动后不久即可进行时钟的第一次更新
  • minpoll 此选项指定发送到服务器的请求之间的最小间隔,以2的幂为单位,以秒为单位。例如,minpoll 5表示轮询间隔不应降到32秒以下。默认值为6(64秒),最小值为-6(1/64秒),最长为24(6个月)。请注意,间隔短于6(64秒)的间隔通常不应与Internet上的公共服务器一起使用,因为它可能被视为滥用。亚秒间隔将启用仅当服务器可访问且往返延迟小于10毫秒时,即服务器应位于本地网络中
  • maxpoll 此选项指定发送到服务器的请求之间的最大间隔(以2的幂为单位,以秒为单位)。例如,maxpoll 9表示轮询间隔应保持在9或以下(512秒)。默认值为10(1024秒),最小为-6(1/64秒),最大为24(6个月)
  • allow allow指令用于指定允许NTP客户端作为NTP服务器访问计算机的特定子网。默认设置是不允许任何客户端访问,即chronyd纯粹作为NTP客户端运行
  • 其他配置默认即可,如需修改请参照文档。使用命令man chrony.conf即可查看详细说明文档
  • 192.168.1.69
vim /etc/chrony.conf
#与68配置的差异在于server,如下:
server 192.168.1.68 iburst
#其他配置省略,使用默认即可
...
...
启动
  • 设置开机自启动
systemctl enable chronyd
  • 启动
systemctl start chronyd
测试
  • 先将系统时间设置为一个错误的时间
date -s 16:25:22
  • 重启chronyd,查看时间,因为chrony的前四个请求可以很快的进行时间同步,便于我们验证
#重启
systemctl restart chronyd
#重启后,隔个几秒用date命令查看下时间是否已同步
date
chronyc

chronyc可以用来跟踪时间同步状态、查看当前同步源、添加移除NTP Server等,具体可以自行看help说明


image.png
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

友情链接更多精彩内容