版本信息:CentOS6.5,hadoop-2.6.0,jdk1.8.0_66
测试集群hosts:
192.168.1.144 master192.168.1.145 slave01
192.168.1.146 slave02
1.对master时间服务器进行操作:
检查时间服务是否安装rpm -q ntp
ntp-4.2.4p8-2.el6.x86_64 // 这表示已安装了,如果没有安装,这是空白。
如果没有安装,在root用户下
yum install ntp
按上面的安装方式在内网每台服务器上都安装好NTP软件包。
完成后,都需要配置NTP服务为自启动
chkconfig ntpd on
chkconfig --list ntpd
ntpd 0:关闭 1:关闭 2:启用 3:启用 4:启用 5:启用 6:关闭
0:off 1:off 2:on 3:on 4:on 5:on 6:off
在配置前,先使用ntpdate手动同步下时间,免得本机与外部时间服务器时间差距太大,让ntpd不能正常同步。
ntpdate -u cn.pool.ntp.org
30 Apr 23:04:54 ntpdate[2753]: step time server 202.112.29.82 offset -4.801052 sec
2.更改相关配置文件
vim /etc/ntp.conf# 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 kod nomodify notrap nopeer noquery
restrict -6 default kod 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 127.0.0.1
restrict -6 ::1
# Hosts on local network are less restricted.
# 允许内网其他机器同步时间
restrict 192.168.1.0 mask 255.255.255.0 nomodify notrap
# Use public servers from the pool.ntp.org project.
# Please consider joining the pool (http://www.pool.ntp.org/join.html).
# 中国这边最活跃的时间服务器 : http://www.pool.ntp.org/zone/cn
server 3.cn.pool.ntp.org //China — cn.pool.ntp.org
server 3.asia.pool.ntp.org
server 0.asia.pool.ntp.org
server 202.112.2.101 //上海交通大学NTP服务器
//这里可以不写这些ntp服务器,写其他的也可以,只要可用就行
#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
# allow update time by the upper server
# 允许上层时间服务器主动修改本机时间
restrict 3.cn.pool.ntp.org nomodify notrap noquery
restrict 3.asia.pool.ntp.org nomodify notrap noquery
restrict 0.asia.pool.ntp.org nomodify notrap noquery
restrict 202.112.2.101 nomodify notrap noquery
# Undisciplined Local Clock. This is a fake driver intended for backup
# and when no outside source of synchronized time is available.
# 外部时间服务器不可用时,以本地时间作为时间服务
server 127.127.1.0 # local clock
fudge 127.127.1.0 stratum 10
# 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
配置文件修改完成,保存退出,启动服务。
service ntpd start
......
启动后,一般需要5-10分钟左右的时候才能与外部时间服务器开始同步时间。可以通过命令查询NTPD服务情况。
查看服务连接和监听
netstat -tlunp | grep ntp
udp 0 0 192.168.1.144:123 0.0.0.0:* 1810/ntpd
udp 0 0 127.0.0.1:123 0.0.0.0:* 1810/ntpd
udp 0 0 0.0.0.0:123 0.0.0.0:* 1810/ntpdudp 0 0 fe80::20c:29ff:fe8d:e0e6:123 :::* 1810/ntpd
udp 0 0 ::1:123 :::* 1810/ntpd
udp 0 0 :::123 :::* 1810/ntpd
看加粗的地方,表示连接和监听已正确,采用UDP方式
3.将其他节点的时间与master进行同步
在其他每一个节点运行命令ntpdate master
这时候发现节点间的时间同步了,但ntpdate只在开机运行,我们若要设置为1小时同步一次
安装完以后使用crontab来自动更新时间:
crontab -e
添加如下行:
* */1 * * * /usr/sbin/ntpdate 192.168.64.11 //一定注意空格,空格空的不对也会报错
使用date命令发现所有节点时间已经一致。