NTP设置集群内机器时间同步

为什么要时间同步?

​ 如果集群中时间相差很大,会出现很多无法解决的问题;几乎所有的集群软件,工作的一个前提条件就是时间是同步的;而业务系统中,常常会记录时间戳,如果集群不同步,这些记录就是脏数据。

相关知识:

#查看或设置当前系统的时间
date
# 按照指定格式显示日期时间
date '+%Y-%m-%d %H:%M:%S' 
#手工临时同步系统时间
ntpdate
#作为守护进程,按照一定的算法进行时间同步,即使你启动了该进程,ntpd也不会立刻进行时间同步
ntpd
#修改默认时区
cp  /usr/share/zoneinfo/Asia/Shanghai  /etc/localtime

ntp相关操作:

#查看ntp状态
$ sudo service ntpd status
ntpd (pid  1565) is running...
#关闭ntp
$ sudo service ntpd stop
Shutting down ntpd:                            [  OK  ]
#启动ntp
$ sudo service ntpd start
Starting ntpd:                      		   [  OK  ]
#重启ntp
$ sudo service ntpd restart
#查看ntp位置
$ which ntpdate
/usr/sbin/ntpdate
#检查ntp端口是否已经开启(root用户下)
netstat -unlnp | grep ntpd
#查看网络中的NTP服务器,显示客户端和每个服务器的关系(root用户下)
ntpq -p
#查看时间服务器的ntp服务状态:服务启动后需要等5-10分钟才能看到正常信息(root用户下)
ntpstat

选择集群内一台机器作为时间服务器,其他机器从该时间服务器更新时间,同时时间服务器向外网时间服务器同步时间。

  • 查看ntp服务是否已经存在,若不存在,给所有需要同步时间的服务器安装ntp服务yum install ntp -y;

    $ sudo rpm -qa|grep ntp
    fontpackages-filesystem-1.41-1.1.el6.noarch
    ntpdate-4.2.4p8-3.el6.centos.x86_64
    ntp-4.2.4p8-3.el6.centos.x86_64
    
    
  • 所有需要同步的机器(包括选择的时间服务器)设置ntp开机自启动,但不需要启动服务

    $ sudo chkconfig ntpd on
    
  • 时间服务器配置

    • 修改时间服务器的配置文件/etc/ntp.conf
    $ vi /etc/ntp.conf
    #ntp.conf
    #释放注释,允许192.168.1.0-192.168.1.255网段的机器同步此服务器
    restrict 192.168.1.0 mask 255.255.255.0 nomodify notrap
    #注释掉默认的外网时间服务器
    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.cn.pool.ntp.org
    server 1.cn.pool.ntp.org
    server 2.cn.pool.ntp.org
    server 3.cn.pool.ntp.org
    
    # 添加允许上层时间服务器主动修改本机时间
    restrict 0.cn.pool.ntp.org nomodify notrap noquery
    restrict 1.cn.pool.ntp.org nomodify notrap noquery
    restrict 2.cn.pool.ntp.org nomodify notrap noquery
    restrict 3.cn.pool.ntp.org nomodify notrap noquery
    #释放注释 外部时间服务器不可用时,以本地时间作为时间服务
    server 127.127.1.0 #local clock
    fudge 127.127.1.0 stratum 10
    #把系统时间写入主板,这样,即使服务器关机或断电,时间也会更新
    SYNC_HWCLOCK=yes
    
    
    • 在启动ntpd服务之前,修改时间服务器时间

    当中国国家授时中心服务器与时间服务器之间的时间误差过大时(可能是1000秒),时间服务器去同步时间可能对系统和应用带来不可预知的问题,时间服务器将停止时间同步!所以如果发现时间服务器启动之后时间并不进行同步时,应该考虑到可能是时间差过大引起的,此时需要先手动进行时间同步!

    #设置当前日期
    $ sudo date -s 1995-11-22
    #设置当前时间
    $ sudo date -s 20:50:30
    # 在root用户下
    date -s "1995-11-22 20:50:30"
    
    • 启动时间服务器的ntpd服务
  • 配置集群内其他机器向时间服务器同步时间

    • 修改配置文件

      # 默认的服务器列表注释掉
      # 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
      
      # 从node01中同步时间
      server 时间服务器主机名或ip
      
      # 允许时间服务器修改本地时间
      restrict 时间服务器主机名或ip nomodify notrap noquery
      
      # 如果时间服务器不可用,用本地的时间服务
      server 127.127.1.0
      fudge 127.127.1.0 stratum 10
      
      # 同步时间后写到硬件中
      SYNC_HWCLOCK=yes
      
    • 每台机器在启动ntpd服务之前,手动同步时间(#ntpstat在时间服务器服务状态正常的前提下)

      #root用户下
      ntpdate -u 时间服务器主机名或ip
      
    • 启动每台机器的ntpd服务

    • 等待5-10分钟后,查看每台机器的状态(#ntpq -pntpstat

  • 定时同步主服务器时间

    #root用户下
    crontab -e
    #每隔十分钟从 时间服务器同步一次
    */10 * * * *  /usr/sbin/ntpdate 时间服务器主机名
    

    亲身实践总结,但也同样感谢网络上其他博主的文章!!

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值