一、架构
1. 架构图
2. 解释
从集群中挑选出一台服务器作为时间服务器,其他机器的时间都和该时间服务器的时间进行同步。
二、集群时间同步的配置方法
1. 时间服务器配置
(1) 在时间服务器上切换用户至root用户
su - root
(2) 查看本机是否已经安装ntp服务,如果没有,则安装ntp服务
# 查看本机是否已经安装了ntp服务
rpm -qa | grep -i -E "ntp"
# 如果执行上面的命令后,发现没有安装ntp服务,使用该命令安装
# yum –y install ntp
(3) 设置ntp服务为开启自启
# 设置ntp服务开机自启
systemctl enable ntpd
(4) 确保ntp服务处于关闭状态,修改它的ntp.conf配置文件
# 关闭ntp服务
systemctl stop ntpd
# 确认ntp服务当前未运行
systemctl status ntpd
# 使用文本编辑器打开ntp服务的配置文件,进行修改
sudo vim /etc/ntp.conf
# 第一处修改,配置集群内网段上的所有服务器都可以从本机查询和同步时间
# 第二个网络号根据自己集群所处的网络号来写
:%s/#restrict 192.168.1.0 mask 255.255.255.0 nomodify notrap/restrict 192.168.159.0 mask 255.255.255.0 nomodify notrap/g
# 第二处修改,不使用互联网上的时间,使用局域网的时间
:%s/server 0.centos.pool.ntp.org iburst/#server 0.centos.pool.ntp.org iburst/g
:%s/server 1.centos.pool.ntp.org iburst/#server 1.centos.pool.ntp.org iburst/g
:%s/server 2.centos.pool.ntp.org iburst/#server 2.centos.pool.ntp.org iburst/g
:%s/server 3.centos.pool.ntp.org iburst/#server 3.centos.pool.ntp.org iburst/g
# 添加内容,如果该节点丢失了网络连接,就使用本地时间作为同步时间使用
server 127.127.1.0
fudge 127.127.1.0 stratum 10
# 退出保存
(5) ntp服务处于关闭的情况下,继续配置另一个配置文件:ntpd
# 使用文本编辑器打开配置文件,进行配置
vim /etc/sysconfig/ntpd
# 添加内容如下
SYNC_HWCLOCK=yes
# 保存退出
(6) 开启ntp服务,退出root用户
# 开启ntp服务
systemctl start ntpd
# 退出root用户
exit
2. 其他服务器配置
(1) 在服务器上切换用户至root用户
su - root
(2) 查看本机是否已经安装ntp服务,如果没有,则安装ntp服务
# 查看本机是否已经安装了ntp服务
rpm -qa | grep -i -E "ntp"
# 如果执行上面的命令后,发现没有安装ntp服务,使用该命令安装
# yum –y install ntp
(3) 设置ntp服务为开启禁启
# 设置ntp服务开机自启
systemctl disable ntpd
(4) 确保ntp服务处于关闭状态
# 关闭ntp服务
systemctl stop ntpd
# 确认ntp服务当前未运行
systemctl status ntpd
(5) 使用crontab编写定时任务
# 启动crontab服务
systemctl start crond
# 编写定时任务
crontab -e
# 每隔十分钟进行时间同步,主机名为时间服务器的主机名,内容为:
*/10 * * * * sudo /usr/sbin/ntpdate hadoop101
(6) 退出root用户
exit