目录
3.配置内网NTP-Server(192.168.190.200)
4.配置内网NTP-Clients(192.168.190.201)
----本文参考自各个相关博文(文末的链接),最后整理出操作笔记。
--《百度百科》
NTP是网络时间协议(Network Time Protocol),它是用来同步网络中各个计算机的时间的协议。
一、NTP时间不同步可能引起的问题:
1.集群自动关闭;
2.HBase中出现错误警告:
2.1.ERROR:org.apache.hadoop.hbase.PleaseHoldException: Master is initializing;//正在初始化
2.2.ERROR:Can't get master address from Zookeeper; znode data == null;
*注:系统启动后NTP服务同步主从节点大概需要15分钟,所以期间HBase中任然报错,需要等待初始化完成。
二、配置NTP时间同步
目标环境,3台Linux CentOS7 虚拟机master/slave1/slave2, 一台作为NTPD服务与外部公共NTP服务同步时间,同时作为内网的NTPD服务器,其他机器与这台服务做时间同步。
服务器IP | 角色 | 说明 | 同步方式 |
192.168.190.200 : master | NTPD服务 | 1、负责与外部公共NTPD服务同步标准时间 2、作为内外网络的NTPD服务 | NTPD服务平滑同步 |
192.168.190.201 : slave1 | 内网NTP客户端 | 内网设备与192.168.190.200同步时间 | NTPD服务平滑同步 |
…… | 内网NTP客户端 | 内网设备与192.168.190.200同步时间 | NTPD服务平滑同步 |
1.NTP时间同步方式选择
ntpd与ntpdate服务的区别:
ntpd服务:(平滑型)一点一点的校准本地与外部时间服务器的时间;
用于大跨度时间校准后的微小校准。
ntpdate服务:(瞬间型)瞬间将本地时间调整为外部时间服务器的时间;
用于一开始的大跨度的时间调整。
----应用场景举例:引用自《ntp服务及时间同步问题》
NTP同步方式在linux下一般两种:使用ntpdate命令直接同步和使用NTPD服务平滑同步。有什么区别呢,简单说下,免得时间长了,概念又模糊。
现有一台设备,系统时间是 13:00 , 真实的当前时间(在空中,也许卫星上,这里假设是在准备同步的上级目标NTP服务器)是: 12:30 。如果我们使用ntpdate同步(ntpdate -u 目标NTP服务器IP),操作系统的时间立即更新为12:30,假如,我们的系统有一个定时应用,是在每天12:40运行,那么实际今天这个的任务已经运行过了(当前时间是13:00嘛),现在被ntpdate修改为12:30,那么意味作10分钟后,又会执行一次任务,这就糟糕了,这个任务只能执行一次的嘛!!我想你(其实是我)已经懂了ntpdate时间同步的隐患,当然这个例子有些极端,但的确是有风险的,生产环境我不打算这么干,还是稳妥点好。所以解决该问题的办法就是时间平滑更改,不会让一个时间点在一天内经历两次,这就是NTPD服务方式平滑同步时间,它每次同步时间的偏移量不会太陡,是慢慢来的(问:怎么来,没有细究,只晓得一次一点的同步,完全同步好需要较长时间,所以一般开启NTPD服务同步前先用ntpdate先手动同步一次)。
2.安装NTP
root用户执行(所有节点)
1.检查是否安装NTP,如果没有则没有显示
$ rpm -q ntp
ntp-4.2.4p8-2.el6.x86_64
2.如果没有安装,yum方式安装
$ yum -y install ntp
3.完成后,启动NTPD服务,并配置NTPD服务为开机自启动
$ service ntpd start -- 启动NTPD服务
$ service ntpd status -- 查看NTPD服务状态
$ service ntpd stop -- 停止NTPD服务
$ systemctl enable ntpd.service -- 设为开机自启
查看自启动项 ntpd状态
$ systemctl list-unit-files | grep ntpd
ntpd.service enable -- 开机自启(ntpd服务-平滑调整时间)
ntpdate.service disable -- 开机不自启(ntpdate服务-瞬间调整时间)
*注:关闭 chronyd 服务的开机自启行为:
CentOS7默认安装了chronyd的时间同步服务,
设置NTPD为开机自启后,需要把chronyd服务先设为开机不自启,
否则NTPD开机无法自启
查看自启动项 chronyd 状态
$ systemctl list-unit-files | grep chronyd
chronyd.service enable
$ systemctl disable chronyd.service
3.配置内网NTP-Server(192.168.190.200)
3.1.修改 /etc/ntp.conf 文件,主要的几个修改地方,如下:
192.168.190.200: master节点root用户操作
1.注释掉原有的四个server,如下:
#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
2.在其上增加aliyun的server
server 203.107.6.88 # aliyun clock
3.增加aliyun server放行
restrict 203.107.6.88
4.增加内网server放行,不允许其修改服务器时间参数
(单个IP放行)restrict 192.168.190.201 nomodify
(网段放行)restrict 192.168.190.0 mask 255.255.255.0 nomodify
5.增加外部服务时间不可获取时,使用本地时间
server 127.127.1.0 # local clock
fudge 127.127.1.0 stratum 10
*注:配置文件中,写在靠前的 server 优先级高于 靠后的 server
所以把 server 203.107.6.88 写在
server 127.127.1.0 之前
意为:本机优先与 203.107.6.88 同步
3.2.修改后的结果:
# For more information about this file, see the man pages
# ntp.conf(5), ntp_acc(5), ntp_auth(5), ntp_clock(5), ntp_misc(5), ntp_mon(5).
driftfile /var/lib/ntp/drift
# Permit time synchronization with our time source, but do not
# permit the source to query or modify the service on this system.
restrict default nomodify notrap nopeer noquery
# Permit all access over the loopback interface. This could
# be tightened as well, but to do so would effect some of
# the administrative functions.
restrict 203.107.6.88
restrict 127.0.0.1
restrict ::1
# Hosts on local network are less restricted.
#restrict 192.168.1.0 mask 255.255.255.0 nomodify notrap
restrict 192.168.190.0 mask 255.255.255.0 nomodify
# Use public servers from the pool.ntp.org project.
# Please consider joining the pool (http://www.pool.ntp.org/join.html).
server 203.107.6.88 # aliyun clock
#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
server 127.127.1.0 # local clock
fudge 127.127.1.0 stratum 10
#broadcast 192.168.1.255 autokey # broadcast server
#broadcastclient # broadcast client
#broadcast 224.0.1.1 autokey # multicast server
#multicastclient 224.0.1.1 # multicast client
#manycastserver 239.255.254.254 # manycast server
#manycastclient 239.255.254.254 autokey # manycast client
# Enable public key cryptography.
#crypto
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.3.启动NTPD服务,等待同步(15分钟以内)
master主节点root用户执行
1.修改完配置文件后,停止NTPD服务
$ service ntpd stop
2.同步本机与外部时间服务器 203.107.6.88 (aliyun clock)时间
(以免时间差距太大,使ntp不能正常同步校准)
$ ntpdate 203.107.6.88 -- ntpd服务启动后不能执行ntpdate服务命令
12 May 13:56:44 ntpdate[1657]: adjust time server 203.107.6.88 offset 0.500967 sec
3.启动并查看NTP服务是否开始同步,通常需要在ntpd启动后15分钟内才能开始同步
$ service ntpd start
$ ntpstat
synchronised to NTP server (203.107.6.88) at stratum 3 -- 已经开始同步,层数3
time correct to within 41 ms -- 时间校准到41ms以内
polling server every 128 s -- 每隔128s轮询一次服务器
4.查看当前NTP服务器与上层NTP服务器的状态
$ ntpq -p
如下图:
参数解释:
remote:是上层 NTP 服务器的 IP 或主主机名
注意最左邊的符号:
1.如果有『 * 』代表目前正在作用当中的上层 NTP
2.如果是『 + 』代表也有连上线,而且可作为下一个提供时间更新的候选者。
refid:是 remote 服务器参考的上一层 NTP 服务器的地址
st:是 stratum 层级
when:几秒钟前曾经做过时间同步化更新的动作;
poll:下一次更新在几秒钟之后;
reach:已经向上层 NTP 服务器要求更新的次数
delay:网络传输过程中延迟的时间,单位为 10^(-3) 秒
offset:时间补偿的结果,单位为 10^(-3) 秒
jitter:Linux 系统时间与 BIOS 硬件时间的差距时间,单位为 10^(-3) 秒。
注:配置参数和命令简单说明请参考:http://linux.vbird.org/linux_server/0440ntp.php#server_ntp.conf
4.配置内网NTP-Clients(192.168.190.201)
4.1.修改 /etc/ntp.conf 文件,主要的几个修改地方,如下:
192.168.190.201: slave1节点root用户操作
1.注释掉原有的四个server,如下:
#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
2.在其上增加主节点IP
server 192.168.190.200 # master lock
3.增加master server放行
restrict 192.168.190.200
4.增加外部服务时间不可获取时,使用本地时间
server 127.127.1.0 # local clock
fudge 127.127.1.0 stratum 10 # 不可以超过15层
4.2.启动NTPD服务,等待同步(15分钟以内)
slave1从节点root用户执行
1.修改完配置文件后,停止NTPD服务
$ service ntpd stop
2.同步本机与主节点时间服务器 192.168.190.200 (master clock)时间
(以免时间差距太大,使ntp不能正常同步校准)
$ ntpdate 192.168.190.200
12 May 13:56:44 ntpdate[1657]: adjust time server 192.168.190.200 offset 0.500967 sec
3.启动并查看NTP服务是否开始同步,通常需要在ntpd启动后15分钟内才能开始同步
$ service ntpd start
$ ntpstat
synchronised to NTP server (192.168.190.200) at stratum 4 -- 已经开始同步,层数4
---比上层master的NTPD服务(为3)低一层,最高层为1
time correct to within 41 ms -- 时间校准到41ms以内
polling server every 128 s -- 每隔128s轮询一次服务器
4.查看当前NTP服务器与上层NTP服务器的状态
$ ntpq -p
如下图:
其中master即是:192.168.190.200
此图的 * 号在LOCAL(0)上,+ 号在master上。为NTPD服务刚开启6分钟时的状态:
此图的 * 号在master上。为NTPD服务刚开启12分钟后的状态:
4.3.如果有更多的从节点,依照“配置内网NTP-Clients(192.168.190.201)”的方式一样配置
......
三、参考资料
四、授时域名池