Centos NTP时间服务器部署及时间同步

一、前言

1、NTP简介

NTP全称为(Network Time Protocol)即网络时间协议。是用来使计算机时间同步的一种协议。它可以使计算机对服务器或时钟源做同步,可以提供高精度的时间校正(LAN 上与标准时间小于1毫秒,WAN上几十毫秒),而且可以由加密确认的方式防止恶意的协议攻击。

2、使用场景

时间保持同步对于服务器集群来说尤为重要,比如说电商的秒杀,以及火车票的抢购等等,如果服务器时间不同步,那么不同的用户可能不是在同一时间点进行抢购的,就会出现不公平的问题。
对于集群化部署的应用,例如数据库集群,只有时间同步了,同一时间到达不同数据库节点的数据才会有相同的时间戳。集群时间的一致性影响了分布式系统的一致性。

二、准备工作

1、服务器规划

IP节点应用
192.168.242.51NTP服务端
192.168.242.52NTP客户端

2、软件环境说明

版本
Linux ServerCentos 7.7
NTP4.2.6

3、端口号

nntp            119/tcp         readnews untp   # USENET News Transfer Protocol
nntp            119/udp         readnews untp   # USENET News Transfer Protocol
ntp             123/tcp
ntp             123/udp 

三、部署服务端

1、安装NTP

yum -y install ntp-4.2.6p5-29.el7.centos.2.x86_64   #默认会安装 ntp,ntpdate

2、修改服务端配置

vim /etc/ntp.conf

#我们每一个system clock的频率都有小小的误差,这个就是为什么机器运行一段时间后会不精确. NTP会自动来监测>我们时钟的误差值并予以调整.但问题是这是一个冗长的过程,所以它会把记录下来的误差先写入driftfile.这样即使
你重新开机以后之前的计算结果也就不会丢失了
driftfile /var/lib/ntp/drift

# restrict IP地址 mask 子网掩码 参数
# 其中 IP 可以是IP地址,也可以是 default ,default 就是指所有的IP
# 参数有以下几个:
# ignore :关闭所有的 NTP 联机服务
# nomodify:客户端不能更改服务端的时间参数,但是客户端可以通过服务端进行网络校时。
# notrust :客户端除非通过认证,否则该客户端来源将被视为不信任子网
# noquery :不提供客户端的时间查询
# notrap:陷阱服务是 ntpdq 控制消息协议的子系统,用于远程事件日志记录程序。
# restrict -6 表示IPV6地址的权限设置
# nopeer :用于阻止主机尝试与服务器对等,并允许欺诈性服务器控制时钟

#restrict   default  nomodify notrap nopeer noquery

#restrict ::1
#拒绝所有NTP联机服务
#restrict default ignore
#该网段不能更改服务器时间参数,但可以校准时钟,远程时间日志记录程序
#restrict 192.168.0.0 mask 255.255.0.0 nomodify notrap
# Hosts on local network are less restricted.
#restrict 192.168.1.0 mask 255.255.255.0 nomodify notrap

# iburst: 如果一个时间服务器不可用,会发送多个包确认
server ntp1.aliyun.com iburst  #设置阿里云时间同步服务器
server ntp2.aliyun.com iburst

#server 1.centos.pool.ntp.org iburst
#server 2.centos.pool.ntp.org iburst

includefile /etc/ntp/crypto/pw

# Key file containing the keys and key identifiers used when operating
# with symmetric key cryptography. 

keys /etc/ntp/keys

# Specify the key identifiers which are trusted.
#trustedkey 4 8 42

# Specify the key identifier to use with the ntpdc utility.
#requestkey 8

# Specify the key identifier to use with the ntpq utility.
#controlkey 8

# Enable writing of statistics records.
#statistics clockstats cryptostats loopstats peerstats

# Disable the monitoring facility to prevent amplification attacks using ntpdc
# monlist command when default restrict does not include the noquery flag. See
# CVE-2013-5211 for more details.
# Note: Monitoring will not be disabled with the limited restriction flag.
disable monitor

3、同步时间到硬件

vim /etc/sysconfig/ntpd

#同步硬件时钟
SYNC_HWCLOCK=yes

4、启动服务,设置开机自启

systemctl start ntpd
systemctl enable ntpd

四、部署客户端

1、安装NTP

yum -y install ntp-4.2.6p5-29.el7.centos.2.x86_64   #默认会安装 ntp,ntpdate

2、修改客户端配置

driftfile /var/lib/ntp/drift
restrict default nomodify notrap nopeer noquery
restrict 127.0.0.1
restrict ::1
server 192.168.242.51  #时间服务器地址28 
restrict 192.168.242.51 nomodify notrap noquery  #允许上层时间服务器修改本机时间
server 127.0.0.1
fudge 127.0.0.1 stratum 10
includefile /etc/ntp/crypto/pw
keys /etc/ntp/keys
disable monitor

3、启动服务,设置开机自启

systemctl start ntpd
systemctl enable ntpd

3、同步时间

3.1 自动同步
# 先设置一个错误时间
[root@k8s-node1 ~]# date -s "2021-9-4 19:20"
Sat Sep  4 19:20:00 CST 2021

#poll 就是多少秒同步一次,当when等于poll的时候就会进行同步
#[root@k8s-node1 ~]# watch ntpq -p
Every 2.0s: ntpq -p                                                           Sat Sep  4 21:10:39 2021

     remote           refid      st t when poll reach   delay   offset  jitter
==============================================================================
 k8s-master1     203.107.6.88     3 u   31   64    3    0.330    0.008   0.015
 localhost       .STEP.          16 l    -   64    0    0.000    0.000   0.000

3.2 立即同步时间
#需要先停掉ntpd服务
[root@k8s-node1 ~]# systemctl stop ntpd
#同步时间
[root@k8s-node1 ~]# ntpdate 192.168.242.51
 4 Sep 21:15:08 ntpdate[22379]: adjust time server 192.168.242.51 offset 0.002881 sec
3.3 通过crontab定时任务+ntpdate同步
[root@k8s-node1 ~]# crontab -e -u root
#每十分钟同步一次
*/10 * * * * /usr/sbin/ntpdate 192.168.242.51

常见报错

1、no server suitable for synchronization found

可能是网络不通,或者时间没服务器没有授权 ntp.conf文件中 restrict,默认是允许所有

2、 the NTP socket is in use, exiting

因是由于 xntpd 已经绑定到了该 Socket。运行 ntpdate 时,它会首先进行广播,然后侦听端口 123。如果 xntpd 正在运行,而有一个进程已经在侦听该端口了,则会使 ntpdate 无法在上面运行。运行下列命令,即可找出 xntpd 的 PID

[root@k8s-master1 ~]# ps -ef|grep ntpd
ntp       12010      1  0 23:51 ?        00:00:00 /usr/sbin/ntpd -u ntp:ntp -g
root      12015  11908  0 23:54 pts/1    00:00:00 grep --color=auto ntpd
[root@k8s-master1 ~]# ntpd
ntpd     ntpdate  ntpdc    
[root@k8s-master1 ~]# ntpdate ntp.aliyun.com
 2 Sep 23:55:08 ntpdate[12016]: the NTP socket is in use, exiting
[root@k8s-master1 ~]# kill 12010
[root@k8s-master1 ~]# ntpdate ntp.aliyun.com
 2 Sep 23:55:31 ntpdate[12019]: adjust time server 203.107.6.88 offset 0.004193 sec
  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值