Linux:集群时间同步

在内网环境中,由于无法直接与公网校准时间,需要配置集群时间同步。选取一台服务器作为时间源,通过NTP协议进行同步。配置包括修改ntp.conf文件以允许特定网段同步,禁用外部NTP服务器,启用本地时间同步,并设置开机启动。其他服务器关闭NTP服务,通过crontab每分钟同步一次时间服务器的时间。
摘要由CSDN通过智能技术生成

集群时间同步

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

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

2)时间服务器配置(必须root用户)
(1)查看hadoop102服务状态和开机自启动状态(如果开着就关掉)
[atguigu@hadoop102 ~]$ sudo systemctl status ntpd
[atguigu@hadoop102 ~]$ sudo systemctl is-enabled ntpd

(2)修改hadoop102的ntp.conf配置文件
[atguigu@hadoop102 ~]$ sudo vim /etc/ntp.conf
修改内容如下
①修改1(授权192.168.10.0-192.168.10.255网段上的所有机器可以从这台机器上查询和同步时间)
#restrict 192.168.1.0 mask 255.255.255.0 nomodify notrap
将上面的注释去掉(并将192.168.1.0改成192.168.10.0)
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 文件
[atguigu@hadoop102 ~]$ sudo vim /etc/sysconfig/ntpd
增加内容如下(让硬件时间与系统时间一起同步)
SYNC_HWCLOCK=yes

(4)重新启动ntpd服务
[atguigu@hadoop102 ~]$ sudo systemctl start ntpd

(5)设置ntpd服务开机启动
[atguigu@hadoop102 ~]$ sudo systemctl enable ntpd

3)其他机器配置(必须root用户)
(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 ~]$ systemctl status crond

(3)在其他机器配置1分钟与时间服务器同步一次
[root@hadoop103 ~]$ sudo crontab -e
编写定时任务如下:
*/1 * * * * /usr/sbin/ntpdate hadoop102

(4)修改任意机器时间
[root@hadoop103 ~]$ date -s “2021-9-11 11:11:11”

(5)1分钟后查看机器是否与时间服务器同步
[root@hadoop103 ~]$ date

Linux 集群中,由于多台服务器之间的时间差异可能会影响到一些应用程序的正常运行,因此需要进行时间同步。下面介绍两种常用的时间同步方案: 1. NTP 时间同步方案 NTP(Network Time Protocol)是一种用于同步网络中计算机时间的协议,通过在服务器上运行 NTP 服务器,其他计算机可以通过 NTP 协议与服务器进行时间同步。 具体操作步骤如下: 1)在一个节点上安装 NTP 服务器软件: ``` yum install ntp ``` 2)编辑配置文件 /etc/ntp.conf,将服务器地址修改为本地服务器的 IP 地址: ``` server 127.127.1.0 fudge 127.127.1.0 stratum 10 server 0.cn.pool.ntp.org ``` 3)启动 NTP 服务并设置开机自启动: ``` systemctl start ntpd systemctl enable ntpd ``` 4)在其他节点上安装 NTP 客户端软件: ``` yum install ntpdate ``` 5)使用以下命令进行时间同步: ``` ntpdate -u 服务器IP地址 ``` 2. Chrony 时间同步方案 Chrony 是一种轻量级的时间同步软件,相比 NTP 更加精准和稳定。Chrony 的主要优势在于它可以快速适应网络延迟的变化,从而更加准确地进行时间同步。 具体操作步骤如下: 1)在所有节点上安装 Chrony 软件: ``` yum install chrony ``` 2)编辑配置文件 /etc/chrony.conf,将服务器地址修改为本地服务器的 IP 地址: ``` server 127.127.1.0 fudge 127.127.1.0 stratum 10 server 0.cn.pool.ntp.org ``` 3)启动 Chrony 服务并设置开机自启动: ``` systemctl start chronyd systemctl enable chronyd ``` 通过以上两种方案的操作,就可以完成 Linux 集群中的时间同步。需要注意的是,在使用 NTP 或 Chrony 进行时间同步时,要确保所有节点的时间都正确,并且所有节点都能够访问到 NTP 或 Chrony 服务器
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

程序员无羡

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值