同步集群时间

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

集群时间同步shell

时间同步的方式:找一个机器,做为时间服务器,全部的机器与这台集群时间进行定时的同步,好比,每隔十分钟,同步一次时间。vim

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

  配置时间同步实操:centos

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

      检查 ntp 是否安装服务器

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

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

      fontpackages-filesystem-1.41-1.1.el6.noarchoop

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

      修改 ntp 配置文件.net

      [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

这样就配置完成了

相关文章
相关标签/搜索