同步集群时间

首先确保集群已经正常使用(搭建集群完毕)

集群时间同步

时间同步的方式:找一个机器,作为时间服务器,所有的机器与这台集群时间进行定时的同步,比如,每隔十分钟,同步一次时间。

第一步 配置小弟同步老大的操作

  配置时间同步实操:

    1.时间服务器配置(必须 root 用户)

      检查 ntp 是否安装

      [root@hadoop102 桌面]# rpm -qa|grep ntp

       ntp-4.2.6p5-10.el6.centos.x86_64

      fontpackages-filesystem-1.41-1.1.el6.noarch

      ntpdate-4.2.6p5-10.el6.centos.x86_64

      修改 ntp 配置文件

      [root@hadoop102 桌面]# vi /etc/ntp.conf

    修改内容如下 

      修改 1

        #restrict 192.168.1.0 mask 255.255.255.0 nomodify notrap 为

        restrict 192.168.1.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

        修改/etc/sysconfig/ntpd 文件

      [root@hadoop102 桌面]# vim /etc/sysconfig/ntpd

      增加内容如下

        SYNC_HWCLOCK=yes

      重新启动 ntpd

        [root@hadoop102 桌面]# service ntpd status

      ntpd 已停

        [root@hadoop102 桌面]# service ntpd start

        正在启动 ntpd:           [确定]

        执行:开机自启

        [root@hadoop102 桌面]# chkconfig ntpd on

        其他机器配置(必须 root 用户)

        在其他机器配置 10 分钟与时间服务器同步一次

        [root@hadoop103 hadoop-2.7.2]# crontab -e

      编写脚本

        */10 * * * * /usr/sbin/ntpdate hadoop102

      修改任意机器时间 

       [root@hadoop103 hadoop]# date -s "2017-9-11 11:11:11"

     十分钟后查看机器是否与时间服务器同步

       [root@hadoop103 hadoop]# date

        发现可以同步

第二步 老大同步网络时间

    如果老大时间跟网络不一致,下面的小弟时间全部都乱套了 下面再老大机器上同步网络

    同步时间需要用到

      ntpdate time.nuri.net

    但是上面的第一步配置 ntpd start 查看ntp的状态

      [root@hadoop02 bin]# service ntpd status
      ntpd (pid  3030) is running...

    正在运行中,如果ntpd正在运行中使用上面的时间同步命令会失败

      20 Sep 09:17:07 ntpdate[3076]: the NTP socket is in use, exiting

    报错,说ntp正在运行,但是他也不能关,如果关了,下面的小弟时间都同步不上老大了,怎么办呢

    下一个shell脚本,先把ntp关了,更新同步时间的操作,然后再开启就行了 本人命名syncTime.sh内容如下

      #!/bin/bash
      service ntpd stop
      echo "停止 ntp. . . . . 。"
      ntpdate time.nuri.net
      service ntpd start
      echo "开启 ntp......"

    把他加到定时任务里面crontab -e,经过测试无效

      */1 * * * *  /usr/local/bin/syncTime.sh   这个命令就是一分钟更新以下网络时间,看不懂的话可以查询crontab的相关资料

    至于为什么无效,还不清楚,但是在定时器里面写如下代码

      */1 * * * *  echo "sdafsdf" >> /usr/local/bin/aa.sh

    给aa.sh追加sdsas文本内容,1分钟追加一次,这个有效。。。

    真正解决办法:编辑/etc/crontab 这个文件

    最后一行添加定时任务  在这个文件执行文件需要添加root  指定是root用户

 

测试,把虚拟机挂起或者关机3分钟,然后开启,会跟网络时间差3分钟,等待1分钟,date查看时间 OK

这样就配置完成了

原文地址:https://www.cnblogs.com/gentle-awen/p/10154262.html