1. NTP 简介

网络时间协议(英语:Network Time Protocol,简称NTP)是在数据网络潜伏时间可变的计算机系统之间通过分组交换进行时钟同步的一个网络协议。自1985年以来,NTP是目前仍在使用的最古老的互联网协议之一。NTP由特拉华大学的David L. Mills设计。

NTP意图将所有参与计算机的协调世界时(UTC)时间同步到几毫秒的误差内。它使用Marzullo算法的修改版来选择准确的时间服务器,其设计旨在减轻可变网络延迟造成的影响。NTP通常可以用公共互联网保持几十毫秒的误差,并且在理想环境的局域网中可以实现超过1毫秒的精度。不对称路由和拥塞控制可能导致100毫秒(或更高)的错误。

2. 规划

使用 NTP 有很几种情况:

情况1:你只有一台服务器,能连接上互联网,那么你在 NTP 中直接配置使用互联网上的授时中心就可以。

情况2:你要为一个集群授时,确保集群中的每个节点时间尽量保持一致。那么,在安装前要在您的集群中对选出一台节点作为授时节点,其他节点作为被授节点。此时,如果你的集群如果能连接到互联网,则应该配置集群中的授时节点为互联网中的授时中心的被授节点,接收互联网授时中心的授时,在集群内部时,集群中的授时节点对其他节点进行授时,这是一个层级结构。

下面就展示了这个层级结构,其中 node1 就是集群内部的授时节点,其他都是被授节点

                 --集群内部-----
| node2
| ↗
互联网授时中心(A)→ → node1
| ↘
| node3
--------------

还有很多情况下是,node1 根本就与 A 不通网络,这种就是典型的内网环境,在这种环境下应该尽量保持集群内部的时间一致,即其他节点与 node1 保持一致。

本文主要就是描述在情况2下的安装部署。

3. 安装部署

假设有 5 个节点,主机名分别为 mdw,sdw01,sdw02,sdw03,sdw04。 那么,在这一集群中,确定 mdw 为授时节点,其他为被授节点

首先登入 mdw,执行安装:

yum -y install ntp

在 mdw 上,给其他节点也进行安装,中间会提示输入密码:

ssh root@sdw01 "yum -y install ntp"
ssh root@sdw02 "yum -y install ntp"
ssh root@sdw03 "yum -y install ntp"
ssh root@sdw04 "yum -y install ntp"

编写一个脚本get_all_host_time.sh查看所有服务器上的时间:

注意:这个脚本需要 expect 支持,因此需要安装:

yum -y install expect

nano get_all_host_time.sh

注意:登陆所有主机的用户名都是 root,密码都是 1。

#!/bin/bash
USER="root"
PASS="1" all_hosts="mdw sdw01 sdw02 sdw03 sdw04"
for element in $all_hosts
do
HOST=$element
EX_RUN=$(expect -c "
spawn ssh $USER@$HOST date \+\%Y\%m\%d\-\%H\%M\%S
expect \"password:\"
send \"$PASS\r\"
interact
")
echo "$EX_RUN" &
done

编写完成后,执行,发现各个节点的时间差异是较大的:

[root@mdw ~]# source get_all_host_time.sh
spawn ssh root@mdw date +%Y%m%d-%H%M%S
root@mdw's password:
20170524-155628
spawn ssh root@sdw01 date +%Y%m%d-%H%M%S
root@sdw01's password:
20170524-154539
spawn ssh root@sdw02 date +%Y%m%d-%H%M%S
root@sdw02's password:
20170524-155629
spawn ssh root@sdw03 date +%Y%m%d-%H%M%S
root@sdw03's password:
20170524-154339
spawn ssh root@sdw04 date +%Y%m%d-%H%M%S
root@sdw04's password:
20170524-155629

4. 配置授时节点

首先在主节点 mdw 上进行配置,mdw 要被设置成授时节点

先设置区域及时区

localectl set-locale LANG=en_US.utf8     #设置系统语言及区域
timedatectl set-timezone Asia/Shanghai #设置时区

有外网条件的可以先更新下 mdw 上的时间,使用的是 cn.pool.ntp.org 上的时间:

[root@mdw ~]# ntpdate cn.pool.ntp.org
24 May 16:31:20 ntpdate[3126]: adjust time server 85.199.214.101 offset -0.008138 sec

没有外网条件的,就看看你的手表,直接设置一下吧:

date -s "2017-05-24 16:38:00"

在设置时间后记得,写一下时间到 BIOS:

hwclock -w

先对配置文件备份2份

cp /etc/ntp.conf /etc/back_ntp.conf
cp /etc/ntp.conf /home/ntp.conf

再对 授时节点 mdw 的 ntp 的配置文件进行修改 nano /etc/ntp.conf

注释掉以下几个授时服务域名,配置能连通的授时域名 cn.pool.ntp.org,如果不能连通外网,此项就不要设置;

注意:配置 cn.pool.ntp.org 的时候,上面带的数字代表几个都是可选的NTP授时服务点,NTP会自己选择一个近的。(In most cases it's best to use pool.ntp.org to find an NTP server (or 0.pool.ntp.org, 1.pool.ntp.org, etc if you need multiple server names). The system will try finding the closest available servers for you.)

将本机作为授时节点,配置 server 127.127.1.0 和 fudge 127.127.1.0 stratum 10,注意不要修改 IP,这在 NTP 中代表本机:

#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 iburst
server 1.cn.pool.ntp.org iburst
server 2.cn.pool.ntp.org iburst
server 3.cn.pool.ntp.org iburst
server 127.127.1.0
fudge 127.127.1.0 stratum 10

如果没有外网就是这样:

#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 iburst
server 127.127.1.0
fudge 127.127.1.0 stratum 10

启动 ntp

service ntpd start

查看 ntp 情况,可以看到在 offset 和 jitter 上是有对应的值的,在 remote 中有*开头的是ntp当前选中的授时服务点:

[root@mdw ~]# ntpq -p
remote refid st t when poll reach delay offset jitter
==============================================================================
-ntp2.itcomplian 5.103.128.88 3 u 6 64 1 351.985 39.559 1.853
+mail.python.org 131.188.3.221 2 u 5 64 1 200.830 -50.568 11.188
*li734-36.member 10.84.87.146 2 u 4 64 1 68.791 -43.469 1.006
+61-216-153-106. 211.22.103.158 3 u 3 64 1 60.575 -27.762 1.399
LOCAL(0) .LOCL. 10 l - 64 0 0.000 0.000 0.000

当然如果不配置 server x.cn.pool.ntp.org iburst,查看 ntp 情况,是这样的,ntp直接选中的是 LOCAL:

[root@mdw ~]# ntpq -p
remote refid st t when poll reach delay offset jitter
==============================================================================
*LOCAL(0) .LOCL. 10 l 3 64 1 0.000 0.000 0.000

4. 配置被授节点

先使用 ntpdate 让 被授节点 更新时间:

[root@mdw ~]# ssh root@sdw01 'ntpdate mdw'
root@sdw01's password:
24 May 17:25:32 ntpdate[20159]: step time server 10.20.17.140 offset 204.898966 sec
[root@mdw ~]# ssh root@sdw02 'ntpdate mdw'
root@sdw02's password:
24 May 17:26:12 ntpdate[20885]: adjust time server 10.20.17.140 offset 0.092156 sec
[root@mdw ~]# ssh root@sdw03 'ntpdate mdw'
root@sdw03's password:
24 May 17:26:24 ntpdate[20493]: step time server 10.20.17.140 offset 341.003116 sec
[root@mdw ~]# ssh root@sdw04 'ntpdate mdw'
root@sdw04's password:
24 May 17:26:37 ntpdate[20496]: adjust time server 10.20.17.140 offset 0.093937 sec
[root@mdw ~]#

还是登入 mdw 节点,编辑原来的备份好的配置文件

nano /home/ntp.conf

#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 mdw iburst

把此配置文件传至其他被授节点

[root@mdw ~]# scp /home/ntp.conf root@sdw01:/etc/ntp.conf
root@sdw01's password:
ntp.conf 100% 2023 2.0KB/s 00:00
[root@mdw ~]# scp /home/ntp.conf root@sdw02:/etc/ntp.conf
root@sdw02's password:
ntp.conf 100% 2023 2.0KB/s 00:00
[root@mdw ~]# scp /home/ntp.conf root@sdw03:/etc/ntp.conf
root@sdw03's password:
ntp.conf 100% 2023 2.0KB/s 00:00
[root@mdw ~]# scp /home/ntp.conf root@sdw04:/etc/ntp.conf
root@sdw04's password:
ntp.conf 100% 2023 2.0KB/s 00:00
[root@mdw ~]#

启动被授节点的ntp服务:

[root@mdw ~]# ssh root@sdw01 'service ntpd restart'
root@sdw01's password:
Redirecting to /bin/systemctl restart ntpd.service
[root@mdw ~]# ssh root@sdw02 'service ntpd restart'
root@sdw02's password:
Redirecting to /bin/systemctl restart ntpd.service
[root@mdw ~]# ssh root@sdw03 'service ntpd restart'
root@sdw03's password:
Redirecting to /bin/systemctl restart ntpd.service
[root@mdw ~]# ssh root@sdw04 'service ntpd restart'
root@sdw04's password:
Redirecting to /bin/systemctl restart ntpd.service

启动被授节点的ntp服务情况,看到 offset 和 jitter 中有值,则说明正常:

[root@mdw ~]# ssh root@sdw01 'ntpq -p'
root@sdw01's password:
remote refid st t when poll reach delay offset jitter
==============================================================================
*mdw LOCAL(0) 11 u 15 64 3 0.256 0.105 0.190
[root@mdw ~]# ssh root@sdw02 'ntpq -p'
root@sdw02's password:
remote refid st t when poll reach delay offset jitter
==============================================================================
*mdw LOCAL(0) 11 u 17 64 3 0.152 -27.998 7.016
[root@mdw ~]# ssh root@sdw03 'ntpq -p'
root@sdw03's password:
remote refid st t when poll reach delay offset jitter
==============================================================================
*mdw LOCAL(0) 11 u 15 64 3 0.221 0.084 0.079
[root@mdw ~]# ssh root@sdw04 'ntpq -p'
root@sdw04's password:
remote refid st t when poll reach delay offset jitter
==============================================================================
*mdw LOCAL(0) 11 u 15 64 3 0.239 -29.479 2.525
[root@mdw ~]#

将所有节点的 ntp 服务都设置成开机启动,这里先对本机(mdw)进行设置:

[root@mdw ~]# chkconfig ntpd on
Note: Forwarding request to 'systemctl enable ntpd.service'.
Created symlink from /etc/systemd/system/multi-user.target.wants/ntpd.service to /usr/lib/systemd/system/ntpd.service. [root@mdw ~]# ssh root@sdw01 'chkconfig ntpd on'
root@sdw01's password:
Note: Forwarding request to 'systemctl enable ntpd.service'.
Created symlink from /etc/systemd/system/multi-user.target.wants/ntpd.service to /usr/lib/systemd/system/ntpd.service.
[root@mdw ~]# ssh root@sdw02 'chkconfig ntpd on'
root@sdw02's password:
Note: Forwarding request to 'systemctl enable ntpd.service'.
Created symlink from /etc/systemd/system/multi-user.target.wants/ntpd.service to /usr/lib/systemd/system/ntpd.service.
[root@mdw ~]# ssh root@sdw03 'chkconfig ntpd on'
root@sdw03's password:
Note: Forwarding request to 'systemctl enable ntpd.service'.
Created symlink from /etc/systemd/system/multi-user.target.wants/ntpd.service to /usr/lib/systemd/system/ntpd.service.
[root@mdw ~]# ssh root@sdw04 'chkconfig ntpd on'
root@sdw04's password:
Note: Forwarding request to 'systemctl enable ntpd.service'.
Created symlink from /etc/systemd/system/multi-user.target.wants/ntpd.service to /usr/lib/systemd/system/ntpd.service.

centos7不同于以往linux的操作,对于开机自启动的调整,命令有所不同。    chkconfig --list查看非系统内置服务的自启动状态
[root@vd13crmtb01 ~]# chkconfig --list

Note: This output shows SysV services only and does not include native
      systemd services. SysV configuration data might be overridden by native
      systemd configuration.

If you want to list systemd services use 'systemctl list-unit-files'.
      To see services enabled on particular target use
      'systemctl list-dependencies [target]'.

iprdump         0:off   1:off   2:on    3:on    4:on    5:on    6:off
iprinit         0:off   1:off   2:on    3:on    4:on    5:on    6:off
iprupdate       0:off   1:off   2:on    3:on    4:on    5:on    6:off
netconsole      0:off   1:off   2:off   3:off   4:off   5:off   6:off
network         0:off   1:off   2:on    3:on    4:on    5:on    6:off
vmware-tools    0:off   1:off   2:on    3:on    4:on    5:on    6:off

根据提示,要查看系统内置的服务自启动状态需要使用命令systemctl list-unit-files,它会列出系统所有内置服务的自启动状态。
    所以调整ntp的自启动需要在这里调整。

[root@vd13crmtb01 ~]# systemctl enable ntpd.service         //开机自启动
    调整之后重启系统,可是发现ntp并没有启动起来

[root@vd13crmtb01 ~]# pgrep ntpd                            //无法查看到相关进程
[root@vd13crmtb01 ~]# systemctl status ntpd
ntpd.service - Network Time Service
   Loaded: loaded (/usr/lib/systemd/system/ntpd.service; enabled)
   Active: inactive (dead)                                  //显示开机自启动了,但是状态是inactive

通过查询发现有一个chronyd服务设置为开机自启动,这个服务导致ntp无法开启自启动

[root@vd13crmtb01 ~]# systemctl status chronyd
chronyd.service - NTP client/server
   Loaded: loaded (/usr/lib/systemd/system/chronyd.service; enabled)
   Active: inactive (dead) since Tue 2014-11-11 08:28:14 CST; 1min 45s ago
Main PID: 3033 (code=exited, status=0/SUCCESS)
   CGroup: /system.slice/chronyd.service

Nov 11 08:06:31 vd13crmtb01.zj.chinamobile.com systemd[1]: Starting NTP clien...
Nov 11 08:06:31 vd13crmtb01.zj.chinamobile.com chronyd[3033]: chronyd version...
Nov 11 08:06:31 vd13crmtb01.zj.chinamobile.com chronyd[3033]: Linux kernel ma...
Nov 11 08:06:31 vd13crmtb01.zj.chinamobile.com chronyd[3033]: hz=100 shift_hz...
Nov 11 08:06:31 vd13crmtb01.zj.chinamobile.com systemd[1]: Started NTP client...
Nov 11 08:28:14 vd13crmtb01.zj.chinamobile.com systemd[1]: Stopping NTP clien...
Nov 11 08:28:14 vd13crmtb01.zj.chinamobile.com systemd[1]: Stopped NTP client...
Hint: Some lines were ellipsized, use -l to show in full.

通过禁止chronyd服务开机自启动之后再将系统重启

[root@vd13crmtb01 ~]# systemctl disable chronyd.service 
rm '/etc/systemd/system/multi-user.target.wants/chronyd.service'

[root@vd13crmtb01 ~]# pgrep ntpd
2981
[root@vd13crmtb01 ~]# ntpq -p
     remote           refid      st t when poll reach   delay   offset  jitter
==============================================================================
10.70.213.132   10.70.91.148     3 u   21   64    1    1.252    0.029   0.000
*10.70.213.133   10.70.91.148     3 u   21   64    1    1.139   -0.191   0.000

此时,NTP的服务已经开机自启动了,完成!

原文链接:

https://www.cnblogs.com/gleaners/p/6905281.html

http://www.iyunv.com/thread-28141-1-1.html

ntp集群时间同步的更多相关文章

  1. 配置NTP集群时间同步(二)

    [root@hadoop1 bin]# rpm -qa|grep ntp 没有的话用yum -y install ntp安装(要记着每台机器上都要安装) [root@hadoop1 bin]# vi ...

  2. RAC集群时间同步服务

    集群时间同步服务在集群中的两个 Oracle RAC 节点上执行以下集群时间同步服务配置.Oracle Clusterware 11g 第 2 版及更高版本要求在部署了 Oracle RAC 的集群的 ...

  3. CentOS7 设置集群时间同步

    1. 安装ntp时间同步工具 yum -y install ntp ntpdate #安装ntpdate时间同步工具 ntpdate cn.pool.ntp.org #设置时间同步 hwclock - ...

  4. CDH集群安装配置(三)- 集群时间同步(主节点)和 免密码登录

    集群时间同步(主节点) 1. 查看是否安装ntp服务,如果没有安装 rpm -qa |grep ntpd查看命令 yum install ntp安装命令 2. 修改配置 vi /etc/ntp.con ...

  5. Ambari 集群时间同步配置教程

    本文原始地址:https://sitoi.cn/posts/27560.html 步骤 在时间同步主节点创建 ntp.conf 文件 在时间同步从节点上创建 ntp.conf 文件 修改所有节点时区 ...

  6. NTP 集群简略部署指南

    NTP 集群简略部署指南 by 无若 1. NTP 简介 网络时间协议(英语:Network Time Protocol,简称NTP)是在数据网络潜伏时间可变的计算机系统之间通过分组交换进行时钟同步的 ...

  7. Hadoop入门 集群时间同步

    集群时间同步 如果服务器在公网环境(能连接外网),可以不采用集群时间同步.因为服务器会定期和公网时间进行校准. 如果服务器在内网环境,必须要配置集群时间同步,否则时间久了,会产生时间偏差,导致集群执行 ...

  8. linux集群时间同步

    说明:由于hadoop集群对时间要求很高,所以集群内主机要经常同步.本文档适合ubuntu.redhat系列. 注:很多内容是在网上摘录,然后试验后总结,如有疑问可留言探讨. 1.设置主机时间准确(任 ...

  9. Hadoop集群时间同步

    0x01 ntp安装 检查时间服务器是否安装 # rpm -q ntp ntp-4.2.4p8-2.el6.x86_64 // 这表示已安装了,如果没有安装,这是空白. //如果没有安装,我们按照下 ...

随机推荐

  1. Android 架构 3.实现

    以实现最小化可用产品(MVP)的目标,用最简单的方式来搭建架构和实现代码.IDE采用Android Studio,Demo实现的功能为用户注册.登录和展示一个券列表,数据采用我们现有项目的测试数据,接 ...

  2. Flex State

    在Flex 程序中,引入了状态设计的概念.在一个程序中,按照功能的需求,将界面切分成相对独立的部分.运行过程中,随着用户交互,界面在各个部分之间切换.比如在购物车程序中,登录界面.选购商品界面.购物车 ...

  3. iOS开发笔记_5.线程,HTTP请求,定时器

    说起线程,不会陌生了,操作系统课程里已经详细介绍了这个东东,这里就不解释了,想要了解的问问百度或者翻翻书. 线程的创建 总结了昨天的学习,有下面几种创建的方式. //第一种 NSThread *t = ...

  4. 【java】乱码处理+编码转化+判断字符串编码方式

    之前有一篇是修改IDE的编码,服务器的编码等处理乱码,但是在所有环境因素上,保证了编码方式之后,也会有前台传递给后台[get方式提交]传递给后台的编码方式是非UTF-8的,也会有例如FTP服务器的编码 ...

  5. mongodb_服务端安装及连接

    安装环境:Windows7  64位    附件内容: 1.mongodb Windows7 补丁:451413_intl_x64_zip.exe 2.mongodb Windows 安装程序:mon ...

  6. Xamarin.Forms 调用 腾讯地图SDK

    Xamarin.Forms研究了好一段时间了,最近一直在学习中,想尝试一下调用其他的SDK,就如腾讯地图SDK(申请容易). 完成此次项目得感谢以下链接: http://www.cnblogs.com ...

  7. leetcode笔记:Word Ladder

    一. 题目描写叙述 Given two words (start and end), and a dictionary, find the length of shortest transformat ...

  8. python字符串转日期

    需要两步 为了从字符串中提取时间,并进行比较,因此有了这个问题,如何将字符串转换成datetime类型 1.字符串与time类型的转换 >>> import time>> ...

  9. Chrome 37 Beta: 使用Windows的DirectWrite和支持<dialog>元素

    今天的Chrome Beta 发布版包含了许多新的开发者特性.这些特性帮助你制作更丰富的.更迅速的和更引人入胜的网页内容以及应用程序,尤其是移动设备上.除非特别指出,否则下面描述的变化对Android ...

  10. java之 ------ 图形界面(三)

    import java.awt.*; import java.awt.event.*; import javax.swing.*; import javax.swing.border.TitledBo ...