ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

Chrony时间同步服务器

2021-08-02 23:04:06  阅读:247  来源: 互联网

标签:同步 chrony ntp Chrony 时间 服务器 root


文章目录

一、时间同步基本概念

1.什么是时间同步

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

2.为什么需要时间同步

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

3. 时间同步是如何完成

假设服务器启动起来后,发现时间慢了24小时,那么他如何将自己的时间调整正确呢

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

二、 Chrony时间服务

1 Chrony介绍

chrony 是基于 ntp 协议的实现时间同步服务,它既可以当做服务端,也可以充当客户端;

  1. chrony 是 NTP 的替代品,能更精确的时间和更快的速度同步时钟;
  2. chrony 占用系统资源少,只有被唤起时才占用少部分CPU,chrony兼容ntpdate;
  3. chrony 允许本地网络其他主机像本地某台主机进行时间同步;

2 为何需要Chrony

所有服务器直接同步公网上的时间不就可以了吗,为何需要自己搭建一台时间服务器呢?
如果每台服务器都去同步公网时间服务器,且服务器较多,会带来如下问题:

  1. 造成网络延迟
  2. 浪费带宽

解决方案:搭建内网时间服务器,来同步公网时间,然后所有服务器来与这台服务器进行时间同步

  1. 减小服务器之间的误差,提升同步速度
  2. 减少网络带宽损耗
    在这里插入图片描述

3. Chrony安装

[root@manager ~]# yum install chrony -y

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

4. Chrony服务端

[root@manager ~]# 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 0.centos.pool.ntp.org iburst
server 1.centos.pool.ntp.org iburst
server 2.centos.pool.ntp.org iburst
server 3.centos.pool.ntp.org 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.
#如果系统时钟的偏移量大于1秒,则允许系统时钟在前三次更新中步进
makestep 1.0 3

# Enable kernel synchronization of the real-time clock (RTC).
#启用实时时钟(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.
#指定包含 NTP 身份验证密钥的文件
#keyfile /etc/chrony.keys

# Specify directory for log files.
#指定日志文件
logdir /var/log/chrony

# Select which information is logged.
#选择日志文件要记录的信息
#log measurements statistics tracking

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

[root@manager ~]# 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 断网继续同步开启

2.重启 Chrony 服务

root@manager ~]# systemctl enable chronyd #开机自启
[root@manager ~]# systemctl restart chronyd

5. Chrony客户端

1.客户端使用 ntpdate 或 chronyc 命令的方式进行手动同步

# ntpdate
[root@nfs ~]# yum install ntpdate -y
[root@nfs ~]# ntpdate 172.16.1.62

# chronyc
[root@nfs ~]# chronyc -a makestep 200 OK

这两种方式需要配合定时任务使用,比较鸡肋,不推荐使用。

2.客户端使用 chrony 守护进程方式进行时间自动化同步
在客户端同样需要安装chrony,假设现在nfs服务器要同步manager服务器的时间,manager服务端ip地址为172.16.1.62

[root@nfs ~]# yum install chrony -y

1.修改客户端配置文件,添加服务端ip

[root@nfs ~]# 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 172.16.1.62 iburst

2.重启客户端chrony

[root@nfs ~]# systemctl restart chronyd

3.查看时间同步是否正常

[root@nfs ~]# chronyc sources
210 Number of sources = 1
MS Name/IP address         Stratum Poll Reach LastRx Last sample               
===============================================================================
^* 172.16.1.62                   3   6   177    18  -1310us[-1840us] +/-   19ms
[root@nfs ~]# 


或者
[root@nfs ~]# 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               
===============================================================================
^* 172.16.1.62                   3   6   377    29   -147us[ -868us] +/-   20ms
[root@nfs ~]# 

标签:同步,chrony,ntp,Chrony,时间,服务器,root
来源: https://blog.csdn.net/m0_46090675/article/details/119334040

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有