1、背景
如果服务器在公网环境(能连接外网),可以不采用集群时间同步,因为服务器会定期 和公网时间进行校准;
如果服务器在内网环境,必须要配置集群时间同步,否则时间久了,会产生时间偏差, 导致集群执行任务时间不同步。
2、设计
IP | 主机名 | 说明 |
---|---|---|
192.168.6.61 | node1 | 作为时间服务器 |
192.168.6.62 | node2 | 使用定时任务同步(node1)时间 |
192.168.6.63 | node3 | 使用定时任务同步(node1)时间 |
找一个机器(node1),作为时间服务器,其他所有的机器与这台机器(node1)时间进行定时的同步,
生产环境下 根据任务对时间的准确程度要求周期同步。
测试环境下 为了尽快看到效果,采用 1 分钟同步一 次。
3、搭建时间服务器ntp(必须是root用户)
1. 查看所有节点 ntpd 服务状态和开机自启动状态
sudo systemctl status ntpd
sudo systemctl start ntpd
sudo systemctl is-enabled ntpd
2. 如果没有安装服务器,需要先安装(每个节点都需要,同步时需要使用相关命令)
1)安装 epel-release
注:Extra Packages for Enterprise Linux 是为“红帽系”的操作系统提供额外的软件包, 适用于 RHEL、CentOS 和 Scientific Linux。相当于是一个软件仓库,大多数 rpm 包在官方 repository 中是找不到的)
yum install -y epel-release
2)安装ntpd
yum -y install ntp
3. 修改 node1 的 /etc/ntp.conf
配置文件
三点内容:
修改 1(授权 192.168.6.0-192.168.6.255 网段上的所有机器可以从这台机器上查 询和同步时间)
#restrict 192.168.1.0 mask 255.255.255.0 nomodify notrap 取消注释,并修改为如下网段类容 restrict 192.168.6.0 mask 255.255.255.0 nomodify notrap
修改 2(集群在局域网中,不使用其他互联网上的时间)
注释i以下内容: #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
4. 修改 node1 的/etc/sysconfig/ntpd
文件
增加内容如下(让硬件时间与系统时间一起同步)
SYNC_HWCLOCK=yes
5. 重新启动 ntpd 服务,并设置开启自启
sudo systemctl start ntpd
sudo systemctl enable ntpd
6. 其他机器配置(必须 root 用户)
1)关闭所有节点上 ntp 服务和自启动
sudo systemctl stop ntpd
sudo systemctl disable ntpd
2)配置 1 分钟与时间服务器同步一次的定时任务
sudo crontab -e
# 编写定时任务如下:
*/1 * * * * /usr/sbin/ntpdate node1
7. 测试:修改任意机器时间
sudo date -s "2021-9-11 11:11:11"
8)测试:1 分钟后查看机器是否与时间服务器同步
sudo date
如果没有成功,检查防火墙是否关闭