简单集群时间同步

最近集群的Hbase的其中几个节点总是连接不上,最后发现是集群之间的系统时间不同步导致的(hbase的时间戳决定节点之间的时间必须同步)。决定使用的ntp来解决集群之间的系统时间同步问题。

安装ntp

1
yum install -y ntp

ntp服务器配置

集群之间的时间同步同样采用sever/client的方式,将其中一个节点做为ntp的服务器,其余作为客户端通过ntp服务来向ntp服务器同步时间。需要选定一台作为ntp server,修该ntp的配置文件。

1
vim /etc/ntp.conf

在restrict内容下,加入一句,限制服务器的访问类型。

1
2
3
4
5
6
# 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 127.0.0.1
restrict -6 ::1
restrict 10.1.13.0 mask 255.255.255.0 nomodify

这里加入了restrict 10.1.13.0 mask 255.255.255.0 nomodify,根据子网掩码,表示访问该ntp服务器的客户端ip必须在10.1.13.1-10.1.13.254范围内,nomodify表明客户端不能更改服务端的时间参数,但是客户端可以通过服务端进行网络校时。

restrict 相关语法为:restrict IP地址 mask 子网掩码 参数

其中IP地址也可以是default ,default 就是指所有的IP

参数有以下几个:

  • ignore :关闭所有的 NTP 联机服务

  • nomodify:客户端不能更改服务端的时间参数,但是客户端可以通过服务端进行网络校时。

  • notrust :客户端除非通过认证,否则该客户端来源将被视为不信任子网

  • noquery :不提供客户端的时间查询:用户端不能使用ntpq,ntpc等命令来查询ntp服务器

  • notrap :不提供trap远端登陆:拒绝为匹配的主机提供模式 6 控制消息陷阱服务。陷阱服务是 ntpdq 控制消息协议的子系统,用于远程事件日志记录程序。

  • nopeer :用于阻止主机尝试与服务器对等,并允许欺诈性服务器控制时钟

  • kod : 访问违规时发送 KoD 包。

restrict -6 表示IPV6地址的权限设置。

接着注释掉原来的server内容,加入以下两句

1
2
3
4
5
6
7
8
# 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
server 127.127.1.0
fudge 127.127.1.0 stratum 10

server指定从哪个ntp服务器同步时间,由于这里是ntp服务器,所以只指定同步自己。

ntp客户端配置

客户端的配置只需指定向ntp服务器同步时间即可。比如上面配置的ntp服务器地址为10.1.13.111,则只需要加入server 10.1.13.111即可

1
2
3
4
5
6
7
# 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
server 10.1.13.111

启动服务

配置好了只需启动服务端和客户端的ntp服务,设置开机启动即可

1
2
service ntpd start
chkconfig ntpd on

这里的ntp服务占用UDP的123端口,当服务启动失败可以看看是否端口被占用

1
netstat -nulp | grep 123

并且要保证防护墙的UDP的123端口开放

如果不开启ntp服务自动对时的话,也可以自己设置定时任务自动对时,只启动ntp服务器,客户端可以通过crontab -e设置定时任务通过ntpdate对时。

1
2
crontab -e
* */1 * * * /usr/sbin/ntpdate 10.1.18.221 >/dev/null 2>&1

但是这种方法不推荐,最好采用上面启动ntp服务自动对时

0%