简介

时间服务NTP名称:Network Time Protocol
作用:用来给其他主机提供时间同步服务,在搭建服务器集群的时候,需要保证各个节点的时间是一致的,时间服务器不失为一个好的选择。

解释:Linux的NTP服务,NTP为NTPD和NTPdate,ntpd是服务器所属,ntpdate为客户端所属,为两个软件。【个人理解,不喜勿喷,还望共同讨论、多多指导。】
更新:2021年5月7日08时19分,ntpd也可以作为客户端校准时间所用。
解释更新:2021年5月7日08时25分:在工作中我们一般都是使用ntpdate+ntp来完成时间同步,因为单独使用ntpdate同步时间虽然简单快捷但是会导致时间不连续,而时间不连续在数据库业务中影响是很大的,单独使用ntp做时间同步时,当服务器与时间服务器相差大的时候则无法启动ntpd来同步时间。由于ntpd做时间同步时是做的顺滑同步(可以简单理解为时间走得快,以便将落后的时间赶过来),所以同步到时间服务器的的时间不是瞬间完成的,开启ntpd之后稍等三五分钟就能完成时间同步。

正文:Hadoop中NTP校准

请检查防火墙是否关闭,如未关闭请关闭

master主机操作

  • 1.安装ntp服务
  • yum install -y ntp
  • 2.修改ntp配置文件
  • vim /etc/ntp.conf
  • 2.1配置文件如下 代码中有解释,请详细查看
  • # For more information about this file, see the man pages
    # ntp.conf(5), ntp_acc(5), ntp_auth(5), ntp_clock(5), ntp_misc(5), ntp_mon(5).
    
    driftfile /var/lib/ntp/drift
    
    # Permit time synchronization with our time source, but do not
    # permit the source to query or modify the service on this system.
    restrict default nomodify notrap nopeer noquery
    
    # Permit all access over the loopback interface.  This could
    # be tightened as well, but to do so would effect some of
    # the administrative functions.
    restrict 127.0.0.1
    restrict ::1
    
    # Hosts on local network are less restricted.
    # 修改此处:这个网段必须是虚拟机的网关IP地址,你的网段不一定就是192.168.1.0,请根据你的网关IP更改
    # 解释:授权192.168.1.0-192.168.1.255网段上的所有机器可以从这台机器上查询和同步时间
    restrict 192.168.1.1 mask 255.255.255.0 nomodify notrap
    
    # 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
    # 修改此处:当该节点丢失网络连接,依然可以采用本地时间作为时间服务器为集群中的其他节点提供时间同步
    # fudge一行表示设置层级
    server 127.127.1.0
    fudge 127.127.1.0 stratum 10
    #broadcast 192.168.1.255 autokey        # broadcast server
    #broadcastclient                        # broadcast client
    #broadcast 224.0.1.1 autokey            # multicast server
    #multicastclient 224.0.1.1              # multicast client
    #manycastserver 239.255.254.254         # manycast server
    #manycastclient 239.255.254.254 autokey # manycast client
    
    # Enable public key cryptography.
    #crypto
    
    includefile /etc/ntp/crypto/pw
    
    # Key file containing the keys and key identifiers used when operating
    # with symmetric key cryptography. 
    keys /etc/ntp/keys
    
    # Specify the key identifiers which are trusted.
    #trustedkey 4 8 42
    
    # Specify the key identifier to use with the ntpdc utility.
    #requestkey 8
    
    # Specify the key identifier to use with the ntpq utility.
    

  • 3.附加项,可有可无
  • 修改/etc/sysconfig/ntpd 文件:

    vi /etc/sysconfig/ntpd

    增加内容如下(让硬件时间与系统时间一起同步)

    SYNC_HWCLOCK=yes
  • 4.重新启动ntpd服务
  • 启动ntpd服务

    systemctl start ntpd.service

    查看ntpd服务

    systemctl status ntpd.service
  • 5.设置ntpd服务开机启动
  • systemctl enable ntpd.service

    其他Slave(从节点)

    提示:请使用ROOT用户

  • 1.在其他机器配置10分钟与时间服务器同步一次
  • # 定时任务
    crontab -e

    代码如下

    */10 * * * * /usr/sbin/ntpdate master1
    注意:指令中的master1为已经在/etc/hosts文件中指定,或者请输入配置ntpd时间服务器的主机IP
    例如:

    */10 * * * * /usr/sbin/ntpdate 192.168.1.10
  • Slave(从节点)安装ntpdate
  • yum install -y ntpdate
    测试方法:
    修改从机中任意主机时间:

    date -s "2017-9-5 09:09:09"

    十分钟后查看机器是否与时间服务器同步
    提示:为节省时间,可以将命令调整为1分钟校准一次

    */1 * * * * /usr/sbin/ntpdate 192.168.1.10

    crontab -e指令详解

    
    # m       h       dom    mon  dow    command
    # 分钟    小时     某天    某月  星期几  命令
    
    0 23 * * * /root/scripts/login_day.sh
    0 23 * * 7 /root/scripts/login_week.sh
    0 0 1 * *  /root/scripts/login_month.sh
    

    NTP时间服务器制作、客户机校时、客户机自动校时