Linux集群时间同步

Linux集群时间同步

所谓集群时间同步,简单点来说,就是选定一台服务器作为时间服务器,集群上的其它所有机器都与这台时间服务器进行定时同步。

准备工作

关闭各节点防火墙

1
2
3
4
5
6
## 查看防火墙状态
[root@node01 ~]# systemctl status firewalld
## 关闭防火墙
[root@node01 ~]# systemctl stop firewalld
##禁止防火墙开机启动
[root@node01 ~]# systemctl disable firewalld

检查各节点是否安装ntp

1
2
3
4
5
6
7
## 给所有需要做时间同步的服务器安装ntp,这里以node01为例
[root@node01 ~]# rpm -qa|grep ntp
[root@node01 ~]# yum -y install ntp
## 安装之后再次查看
[root@node01 ~]# rpm -qa|grep ntp
ntpdate-4.2.6p5-28.el7.centos.x86_64
ntp-4.2.6p5-28.el7.centos.x86_64

集群时间同步

选定时间服务器

选定一个服务器作为时间服务器,这里选择node01作为时间服务器。

修改/etc/ntp.conf配置文件

文件位置为/etc/ntp.conf

  1. 改动一:授权该网段上的所有机器都可以从时间服务器查询和同步时间
    以本集群为例,node01的IP地址为192.168.10.10,node02的IP地址为192.168.10.11。
    这里我们授权192.168.10.0-192.168.10.255网段上的所有机器都可以从时间服务器查询和同步时间。这里的网段以实际情况为准。

    1
    2
    3
    4
    5
    6
    7
    ##改动前
    # Hosts on local network are less restricted.
    #restrict 192.168.1.0 mask 255.255.255.0 nomodify notrap
    ##改动后
    # Hosts on local network are less restricted.
    #restrict 192.168.1.0 mask 255.255.255.0 nomodify notrap
    restrict 192.168.10.0 mask 255.255.255.0 nomodify notrap
  2. 改动二:集群在局域网中,不使用其它互联网上的时间

    1
    2
    3
    4
    5
    6
    7
    ## 直接将互联网上的时间配置注释掉即可。改动后
    # Use public servers from the pool.ntp.org project.
    # Please consider joining the pool (http://www.pool.ntp.org/join.html).
    # 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. 改动三:当该节点丢失网络连接,依然可以采用本地时间作为时间服务器为集群中的其它节点提供时间同步
    在实际使用中,可能会出现节点网络连接丢失的情况,这个时候我们可以以本地时间作为时间服务。
    这里直接在配置文件末尾添加下面的配置即可。

    1
    2
    3
    ## 这里stratum后面的10表示等级
    server 127.127.1.0
    fudge 127.127.1.0 stratum 10

修改/etc/sysconfig/ntpd配置文件

1
2
## 在文件末尾新增下面的配置,这里是为了让硬件时间与系统时间一起同步。
SYNC_HWCLOCK=yes

重新启动ntpd服务,并设置ntpd服务开机启动

注意:只在时间服务器上启动ntpd服务,在其它节点不要启动ntpd服务。

1
2
3
4
5
6
## 查看服务ntpd状态
[root@node01 ~]# systemctl status ntpd.service
## 打开服务
[root@node01 ~]# systemctl start ntpd.service
## 设置服务开机启动
[root@node01 ~]# systemctl enable ntpd.service

其它节点设置定时任务

1
2
3
4
5
6
7
## 编辑定时任务,以每1分钟进行一次时间同步操作为例
[root@node02 ~]# crontab -e
## 定时任务内容,已配置主机名与IP地址对应关系,或者直接使用IP地址
[root@node02 ~]# crontab -l
*/1 * * * * /usr/sbin/ntpdate node01
## 重启服务
[root@node02 ~]# systemctl restart crond.service

简单测试

修改node02,等待一分钟之后,查看时间是否同步。

1
2
3
4
5
[root@node02 ~]# date -s '2011-01-01 11:11:11'
2011年 01月 01日 星期六 11:11:11 CST
## 等待一分钟之后再次查看时间
[root@node02 ~]# date
2018年 08月 18日 星期六 12:32:39 CST
打赏
  • 版权声明: 本博客所有文章除特别声明外,著作权归作者所有。转载请注明出处!
  • Copyrights © 2015-2023 henrrywan

请我喝杯咖啡吧~

支付宝
微信