[Linux系列]Chrony时间同步服务器

背景

         前面在做LNMP和LNMT架构的实验时,虚拟机待机一晚上之后,时间还停留在前一天,从而导致apt命令无法安装应用。又让我想起了前段时间在做某国产xc项目的时候,就出现过内网xc主机,关机一段时间之后,时间不对,从而导致部分应用无法访问的问题。今天刚好有时间,就来说说Chrony时间同步吧!!

 时间同步

         我们知道一台电脑主机,每次开机时间都是正常的,那是因为我们电脑上配置了时间同步的服务器地址,联网之后,会通过互联网上的时间服务器来校验我们本地的时间。当主机处于内网的时候,我们系统时间是通过bios的时间来保证时间一致的,bios上有个纽扣电池,当电池没电之后,且又无法同步互联网时间的话,就会导致电脑的系统时间不对的情况。

1.    什么是时间同步?

         时间同步,就是将本地时间与互联网时间进行校对,为系统提供一个统一时间的过程。
由于本地时间的计时速率、运行环境不一致性;所有本地时钟即使在某一刻被校准了 ,一段时间后,这些本地时钟也会出现不一致。为了本地时钟再次达到相同的时间值,所以需要进行时间同步的操作。

2.     为什么要时间同步? 

         在运维工作的场景当中,存在着众多主机协同完成不同的任务,比如 LNMP 架构,它们可以分别部署在三台不同的主机上;那么这三台主机在工作时,由于分别位于不同的主机之上,它们需要根据文件或者数据流所生成的时间,来决定响应给客户端的结果该如何进行展示;此时就需要统一网络中的主机时间一致。但这个时间一致并不是说一定得是正确的,如果现在当前时间是下午3点,但是这三台主机的时间精确一致是昨天凌晨6点,这也没有什么问题。
但对于有些场景时间不正确也不行,比如https应用;客户端与服务端通讯时,如果客户端时间是准确的,而服务端时间来自昨天,或者来自未来的响应,则会提示存在风险,而不予接受。

3.    常用的时间同步服务器

 1) NTP时间服务:让时间校对像手表一样波动的快一点,而不是像date命令直接跳跃过去:其他服务器一分钟60s,而ntp一分钟30s,来实现时间的校对;问题:为了赶上慢的24小时,可能需要消耗非常长的时间来进行校对
2)Chrony时间服务:Chrony是NTP的替代品,能更精确、更快的同步时钟,传统ntp需要几小时,而chrony仅需要数秒种或数毫秒即可完成时间同步;调整时间的速度就像波动表针的速度一样快;

4.    公网时间服务器地址

阿里云NTP服务器:ntp1.aliyun.com;ntp2.aliyun.com;ntp3.aliyun.com;ntp4.aliyun.com

腾讯云NTP服务器:time1.cloud.tencent.com ;time2.cloud.tencent.com ;
time3.cloud.tencent.com

使用方法:ntpdate    ntp1.aliyun.com 

Chrony 

 1.    chrony简介

1)chrony 是基于 ntp 协议的实现时间同步服务,它既可以当做服务端,也可以充当客户端;
2)chrony 是 NTP 的替代品,能更精确的时间和更快的速度同步时钟;
3)chrony 占用系统资源少,只有被唤起时才占用少部分CPU,chrony兼容ntpdate;
4)chrony 允许本地网络其他主机像本地某台主机进行时间同步;

chrony官网:https://chrony.tuxfamily.org
chrony官方文档:https://chrony.tuxfamily.org/documentation.html

 2.     内网激活服务器架构 

 3.     Chrony服务端安装

 3.1        apt命令安装

root@web1:~# apt install chrony
root@web1:~#
root@web1:~#

主配置文件:/etc/chrony.conf
客户端程序:/usr/bin/chronyc
服务端程序:/usr/sbin/chronyd

3.2        配置Chrony服务器


root@web1:/etc/chrony#vim /etc/chrony.conf
###使用同步的远程时钟源,理论上可以同步无限个
pool ntp.ubuntu.com        iburst maxsources 4
pool 0.ubuntu.pool.ntp.org iburst maxsources 1
pool 1.ubuntu.pool.ntp.org iburst maxsources 1
pool 2.ubuntu.pool.ntp.org iburst maxsources 2

# Use time sources from DHCP.
sourcedir /run/chrony-dhcp

# Use NTP sources found in /etc/chrony/sources.d.
sourcedir /etc/chrony/sources.d

# This directive specify the location of the file containing ID/key pairs for
# NTP authentication.
keyfile /etc/chrony/chrony.keys

# This directive specify the file into which chronyd will store the rate
# information.
driftfile /var/lib/chrony/chrony.drift

# Save NTS keys and cookies.
ntsdumpdir /var/lib/chrony


## 日志文件目录
logdir /var/log/chrony

# Stop bad estimates upsetting machine clock.
maxupdateskew 100.0

## #启用实时时钟(RTC)的内核同步
rtcsync

# Step the system clock instead of slewing it if the adjustment is larger than
# one second, but only in the first three clock updates.
## #如果系统时钟的偏移量大于1秒,则允许系统时钟在前三次更新中步进
makestep 1 3

# Get TAI-UTC offset and leap seconds from the system tz database.
# This directive must be commented out when using time sources serving
# leap-smeared time.
leapsectz right/UTC

 Chrony服务端配置,修改 /etc/chrony.conf 文件三处,设定外部时间服务器、允许内网同步此服务端、设置断网继续同步即可

root@web1:/etc/chrony#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 ntp.aliyun.com iburst  #1 指定三台阿里云时间同步服务器
server ntp1.aliyun.com iburst
server ntp2.aliyun.com iburst

# Allow NTP client access from local network.
allow 172.16.1.0/24    #2 允许172.16.1.0网段同步此服务器

# Serve time even if not synchronized to a time source.
local stratum 10   #3 断网继续同步开启

4.   Chrony客户端安装

 安装chrony

root@web2:#  apt install chrony
root@web2:# 

配置服务端地址

root@web2:/etc/chrony/conf.d# touch  test1.conf
root@web2:/etc/chrony/conf.d# cat test1.conf 
server 192.168.65.130 iburst

重启服务

root@web2:/etc/chrony/conf.d# systemctl restart chronyd

开始同步

root@web2:~# chronyc sources
MS Name/IP address         Stratum Poll Reach LastRx Last sample               
===============================================================================
^* 192.168.65.130                3   6   177    61   +164us[ +269us] +/-   42ms
root@web2:~# 

或者


root@web2:~# chronyc sources -v

  .-- Source mode  '^' = server, '=' = peer, '#' = local clock.
 / .- Source state '*' = current best, '+' = combined, '-' = not combined,
| /             'x' = may be in error, '~' = too variable, '?' = unusable.
||                                                 .- 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.65.130                3   6   377    16   +428us[+1449us] +/-   33ms
root@web2:~# 

猜你喜欢

转载自blog.csdn.net/qq_43714097/article/details/126624827