如果服务器在 公网环境下,可以来连接外网,服务器会定期和公网时间校准,不需要时间同步
如果是在内网环境下,必须配置集群时间同步,否则时间久了,会产生时间偏差,导致集群执行任务的时间不同步
需求:
找一台机器作为时间机器,所有的机器与这台机器时间进行定时同步,生产环境根据任务时间对时间的准确程度要求周期同步。测试环境为了尽快看到效果,采用1分钟同步一次
(1) 查看所有节点ntpd服务状态和开机自启动状态
[jiaran@hadoop102 ~]$ sudo systemctl status ntpd
[jiaran@hadoop102 ~]$ sudo systemctl start ntpd
[jiaran@hadoop102 ~]$ sudo systemctl is-enabled ntpd
(2) 修改hadoop102的ntp.conf配置文件
[jiaran@hadoop102 ~]$ sudo vim /etc/ntp.conf
修改内容:
授权192.168.10.0-192.168.10.255网段上的所有机器可以从这台机器上查询和同步时间)
#restrict 192.168.10.0 mask 255.255.255.0 nomodify notrap
改为restrict 192.168.10.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文件
[jiaran@hadoop102 ~]$ sudo vim /etc/sysconfig/ntpd
增加内容如下(让硬件时间与系统时间一起同步)
SYNC_HWCLOCK=yes
(4)重启ntpd服务
[jiaran@hadoop102 ~]$ sudo systemctl start ntpd
(5)设置ntpd服务开机自启动
[jiaran@hadoop102 ~]$ sudo systemctl enable ntpd
(6)其他机器上的配置
1 关闭所有节点的ntp服务和自启动
[jiaran@hadoop103 ~]$ sudo systemctl stop ntpd
[jiaran@hadoop103 ~]$ sudo systemctl disable ntpd
[jiaran@hadoop104 ~]$ sudo systemctl stop ntpd
[jiaran@hadoop104 ~]$ sudo systemctl disable ntpd
2在其他机器配置1分钟与时间服务器同步一次
[jiaran@hadoop103 ~]$ sudo crontab -e
编写定时任务如下:
*/1 * * * * /usr/sbin/ntpdate hadoop102
3 修改任意机器时间
[jiaran@hadoop103 ~]$ sudo date -s "2021-9-11 11:11:11"
4 1分钟后查看机器是否与时间服务器同步
[jiaran@hadoop103 ~]$ sudo date