持续创作,加速成长!这是我参与「掘金日新计划 · 10 月更文挑战」的第20天,点击查看活动详情
目录
1.时间同步基本概念
1.1.什么是时间同步
时间同步,就是将本地时间与互联网时间进行校对,为系统提供一个统一时间的过程;
由于本地时间的计时速率、运行环境不一致性;所有本地时钟可能在某一刻被校准了,一段时间后,这些本地时钟也会出现 不一致。为了本地时钟再次达到相同的时间值,需要再次进行时间同步的操作
1.2.为什么需要时间同步
在运维工作的场景当中,存在这众多主机协同完成不同的任务;
1.3.时间同步是如果完成
ntp时间服务(centos6) chrony时间服务器:是ntp的替代品,能更精确、更快的同步,传统ntp需要几个小时,而chrony仅需要数秒种活毫秒即可 完成时间同步
2.chrony时间服务
2.1.chrony介绍
chrony是基于ntp协议的实现时间同步服务,它既可以当做服务端,也可以充当客户端 1.chrony是ntp的替代品,能更精确的时间和更快的速度同步时钟 2.chrony占用系统资源少,只有被唤起时才占用少部分cpu,chrony兼容ntpdate 3.chrony允许本地网络其他主机像本地进行时间同步
2.2.为什么我们要使用chrony
所有服务器直接同步公网上的时间不就可以了吗,为什么需要自己搭建一台时间服务器呢 1.如果每台服务器都去同步公网时间服务器,且服务器较多,会造成延迟,浪费带宽
所有我们使用chrony,搭建内网时间服务器,来同步公网时间,然后所有服务器来与这台服务器进行时间同步 这样也可以减少服务器之间的误差,提升同步速度,减少网络带宽损耗
3.chrony服务端
3.1.安装chrony服务
[root@192.168.146.101~]# yum -y install chrony
主配置文件:/etc/chrony.conf
客户端程序:/usr/bin/chronyc
服务器程序:/usr/sbin/chronyd
3.2.修改chrony配置文件
[root@192.168.146.101~]# vim /etc/chrony.conf
server ntp.aliyun.org iburst
allow 192.168.0.0/16
local stratum 10
3.3.启动chrony
[root@192.168.146.101~]# systemctl start chronyd
3.2.配置文件详解
[root@192.168.146.101~]# 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指令启用硬件时间戳 #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
4.chrony客户端
4.1.客户端使用ntpdate活chronyc命令的方式进行手动同步
ntpdate
[root@k8smaster1 ~]# yum -y install ntpdate
[root@k8smaster1 ~]# ntpdate 192.168.146.101
chronyc
[root@k8smaster1 ~]# chronyc -a makestep
200 OK
4.2.使用chrony守护进程方式进行时间自动化同步
[root@k8smaster1 ~]# vim /etc/chrony.conf
server 192.168.146.101 iburst
4.3.重启
[root@k8smaster1 ~]# systemctl restart chronyd
4.4.查看时间同步是否正常
[root@k8smaster1 ~]# chronyc sources
[root@k8smaster1 ~]# chronyc sources -v