第七章 Hadoop集群时间同步

一、概述

如果服务器在公网环境(能连接外网),可以不采用集群时间同步,因为服务器会定期和公网时间进行校准;
如果服务器在内网环境,必须要配置集群时间同步,否则时间久了,会产生时间偏差,导致集群执行任务时间不同步。

二、需求

找一个机器,作为时间服务器,所有的机器与这台集群时间进行定时的同步,生产环境根据任务对时间的准确程度要求周期同步。测试环境为了尽快看到效果,采用1分钟同步一次。

三、时间服务器配置

1.ntpd服务状态和开机自启动状态

#1.查看ntpd服务状态(必须root用户)
[root@hadoop102 ~]# systemctl  status ntpd
● ntpd.service - Network Time Service
   Loaded: loaded (/usr/lib/systemd/system/ntpd.service; disabled; vendor preset: disabled)
   Active: inactive (dead)

#2.启动ntpd服务
[root@hadoop102 ~]# systemctl start ntpd

#3.设置开机自启
[root@hadoop102 ~]# systemctl is-enabled ntpd
disabled

2.修改hadoop102的ntp.conf配置文件

[root@hadoop102 ~]# vim /etc/ntp.conf
#1.授权192.168.10.0-192.168.10.255网段上的所有机器可以从这台机器上查询和同步时间
#restrict 192.168.10.0 mask 255.255.255.0 nomodify notrap
为restrict 10.0.0.0 mask 255.255.255.0 nomodify notrap

#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
为
#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

#3.当该节点丢失网络连接,依然可以采用本地时间作为时间服务器为集群中的其他节点提供时间同步
server 127.127.1.0
fudge 127.127.1.0 stratum 10

3.修改hadoop102的/etc/sysconfig/ntpd 文件

[root@hadoop102 ~]#vim /etc/sysconfig/ntpd
增加内容如下(让硬件时间与系统时间一起同步)
SYNC_HWCLOCK=yes

4.重启ntpd服务

#1.重启ntpd服务
[root@hadoop102 ~]# systemctl restart ntpd

#2.设置ntpd服务开机启动
[root@hadoop102 ~]# systemctl enable ntpd

四、其他机器配置

1.关闭ntp服务和自启动

[root@hadoop103 ~]# systemctl stop ntpd
[root@hadoop103 ~]# systemctl disable ntpd
[root@hadoop104 ~]# systemctl stop ntpd
[root@hadoop104 ~]# systemctl disable ntpd

2.在其他机器配置时间同步

[root@hadoop103 ~]# crontab -e
*/1 * * * * /usr/sbin/ntpdate hadoop102
[root@hadoop104 ~]# crontab -e
*/1 * * * * /usr/sbin/ntpdate hadoop102

3.修改任意机器时间

[root@hadoop103 ~]# date -s "2021-9-11 11:11:11"

4.1分钟后查看机器是否与时间服务器同步

[root@hadoop103 ~]# date
Thu Sep  2 15:29:49 CST 2021
posted @ 2021-09-02 15:34  年少纵马且长歌  阅读(153)  评论(0编辑  收藏  举报