Centos7 配置Hadoop集群时间同步(ntp)
时间同步的方式:找一台机器,作为时间服务器,所有的机器与这台时间服务器时间进行定时的同步,比如每隔10分钟同步一次时间
1 修改server ntp
- 查看
ntp
是否安装[shufang@shufang101 ~]$ rpm -qa | grep ntp fontpackages-filesystem-1.44-8.el7.noarch ntpdate-4.2.6p5-25.el7.centos.2.x86_64 python-ntplib-0.3.2-1.el7.noarch # 表示已经安装,然后我们不要这个ntp #删除已安装的ntp yum erase ntp ntpdate > 删除: > ntpdate.x86_64 0:4.2.6p5-25.el7.centos.2 > 完毕! # 重新安装ntp yum -y install ntp
- 修改
ntp
配置文件
- 修改1:授权所有192.168.93.0~192.168.93.255网段上的所有机器可以从这台机器上查询和同步时间
- 修改2:集群在局域网中,不适用其他互联网上的时间
- 添加3:当该节点丢失网络连接,依然可以采用本地时间作为时间服务器为集群中的其它节点提供时间同步
vim /etc/ntp.conf
# Hosts on local network are less restricted. #restrict 192.168.1.0 mask 255.255.255.0 nomodify notrap => restrict 192.168.93.0 mask 255.255.255.0 nomodify notrap #1 表示授权其它相同网段的机器能查看并同步该服务器上的时间,这个可选可不选 # 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 => #2 表示不从其它互联网服务器同步时间 #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 #4 在文件末尾添加以下配置 server 127.127.1.0 iburst #fudge 127.127.93.0 stratum 8 #该配置影响下面的结果,可选,可以使用ntpstat命令查看,默认是7 #synchronised to NTP server (192.168.93.101) at stratum 7 #time correct to within 18 ms #polling server every 64 s
- 启动ntpd
service ntpd start 或者 systemctl start ntpd.service
查看是否启动
service ntpd status 或者 systemctl status ntpd.service
- 开启ntpd开机启动
#设置ntp服务开机自启动 chkconfig ntpd on 或者 systemctl enable ntpd.service
2 操作client ntp创建定时同步任务
在每台需要同步的客户端节点作以下操作。同时需要启动ntp,同时设置开机启动!~~~
sudo vim /etc/ntp.conf
#注释掉其他上游时间服务器
#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
#配置上游时间服务器为本地的ntpd Server服务器
server 192.168.93.101 # 这就是需要同步的服务器ip
#配置允许上游时间服务器主动修改本机的时间
restrict 192.168.93.101 nomodify notrap noquery
然后配置调度任务作业,如下。
- 在其它Client(shufang102、shufang103)机器上配置1分钟与时间服务器的时间同步一次
#此时将shufang101作为时间服务器节点,必须使用root权限进行操作 shufang@shufang102> su root crontab -e 或者 sudo crontab -e */1 * * * * /usr/sbin/ntpdate -u shufang101
- 在shufang103上同步shufang101时间
shufang@shufang103> su root crontab -e 或者 sudo crontab -e */1 * * * * /usr/sbin/ntpdate -u shufang101
- 验证是否配置成功
#在client上修改时间,过1分钟后观察,使用date命令统一查看是否生效 date -s "2020-01-01 00:00:57"