NTP 集群简略部署指南

by 无若

1. NTP 简介

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

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

2. 规划

使用 NTP 有很几种情况:

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

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

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

  1. --集群内部-----
  2. | node2
  3. |
  4. 互联网授时中心(A)→ node1
  5. |
  6. | node3
  7. --------------

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

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

3. 安装部署

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

首先登入 mdw,执行安装:

  1. yum -y install ntp

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

  1. ssh root@sdw01 "yum -y install ntp"
  2. ssh root@sdw02 "yum -y install ntp"
  3. ssh root@sdw03 "yum -y install ntp"
  4. ssh root@sdw04 "yum -y install ntp"

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

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

  1. yum -y install expect

nano get_all_host_time.sh

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

  1. #!/bin/bash
  2. USER="root"
  3. PASS="1"
  4.  
  5. all_hosts="mdw sdw01 sdw02 sdw03 sdw04"
  6. for element in $all_hosts
  7. do
  8. HOST=$element
  9. EX_RUN=$(expect -c "
  10. spawn ssh $USER@$HOST date \+\%Y\%m\%d\-\%H\%M\%S
  11. expect \"password:\"
  12. send \"$PASS\r\"
  13. interact
  14. ")
  15. echo "$EX_RUN" &
  16. done

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

  1. [root@mdw ~]# source get_all_host_time.sh
  2. spawn ssh root@mdw date +%Y%m%d-%H%M%S
  3. root@mdw's password:
  4. 20170524-155628
  5. spawn ssh root@sdw01 date +%Y%m%d-%H%M%S
  6. root@sdw01's password:
  7. 20170524-154539
  8. spawn ssh root@sdw02 date +%Y%m%d-%H%M%S
  9. root@sdw02's password:
  10. 20170524-155629
  11. spawn ssh root@sdw03 date +%Y%m%d-%H%M%S
  12. root@sdw03's password:
  13. 20170524-154339
  14. spawn ssh root@sdw04 date +%Y%m%d-%H%M%S
  15. root@sdw04's password:
  16. 20170524-155629

4. 配置授时节点

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

先设置区域及时区

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

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

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

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

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

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

  1. hwclock -w

先对配置文件备份2份

  1. cp /etc/ntp.conf /etc/back_ntp.conf
  2. 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 中代表本机:

  1. #server 0.centos.pool.ntp.org iburst
  2. #server 1.centos.pool.ntp.org iburst
  3. #server 2.centos.pool.ntp.org iburst
  4. #server 3.centos.pool.ntp.org iburst
  5. server 0.cn.pool.ntp.org iburst
  6. server 1.cn.pool.ntp.org iburst
  7. server 2.cn.pool.ntp.org iburst
  8. server 3.cn.pool.ntp.org iburst
  9. server 127.127.1.0
  10. fudge 127.127.1.0 stratum 10

如果没有外网就是这样:

  1. #server 0.centos.pool.ntp.org iburst
  2. #server 1.centos.pool.ntp.org iburst
  3. #server 2.centos.pool.ntp.org iburst
  4. #server 3.centos.pool.ntp.org iburst
  5. #server 0.cn.pool.ntp.org iburst
  6. server 127.127.1.0
  7. fudge 127.127.1.0 stratum 10

启动 ntp

  1. service ntpd start

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

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

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

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

4. 配置被授节点

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

  1. [root@mdw ~]# ssh root@sdw01 'ntpdate mdw'
  2. root@sdw01's password:
  3. 24 May 17:25:32 ntpdate[20159]: step time server 10.20.17.140 offset 204.898966 sec
  4. [root@mdw ~]# ssh root@sdw02 'ntpdate mdw'
  5. root@sdw02's password:
  6. 24 May 17:26:12 ntpdate[20885]: adjust time server 10.20.17.140 offset 0.092156 sec
  7. [root@mdw ~]# ssh root@sdw03 'ntpdate mdw'
  8. root@sdw03's password:
  9. 24 May 17:26:24 ntpdate[20493]: step time server 10.20.17.140 offset 341.003116 sec
  10. [root@mdw ~]# ssh root@sdw04 'ntpdate mdw'
  11. root@sdw04's password:
  12. 24 May 17:26:37 ntpdate[20496]: adjust time server 10.20.17.140 offset 0.093937 sec
  13. [root@mdw ~]#

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

nano /home/ntp.conf

  1. #server 0.centos.pool.ntp.org iburst
  2. #server 1.centos.pool.ntp.org iburst
  3. #server 2.centos.pool.ntp.org iburst
  4. #server 3.centos.pool.ntp.org iburst
  5.  
  6. server mdw iburst

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

  1. [root@mdw ~]# scp /home/ntp.conf root@sdw01:/etc/ntp.conf
  2. root@sdw01's password:
  3. ntp.conf 100% 2023 2.0KB/s 00:00
  4. [root@mdw ~]# scp /home/ntp.conf root@sdw02:/etc/ntp.conf
  5. root@sdw02's password:
  6. ntp.conf 100% 2023 2.0KB/s 00:00
  7. [root@mdw ~]# scp /home/ntp.conf root@sdw03:/etc/ntp.conf
  8. root@sdw03's password:
  9. ntp.conf 100% 2023 2.0KB/s 00:00
  10. [root@mdw ~]# scp /home/ntp.conf root@sdw04:/etc/ntp.conf
  11. root@sdw04's password:
  12. ntp.conf 100% 2023 2.0KB/s 00:00
  13. [root@mdw ~]#

启动被授节点的ntp服务:

  1. [root@mdw ~]# ssh root@sdw01 'service ntpd restart'
  2. root@sdw01's password:
  3. Redirecting to /bin/systemctl restart ntpd.service
  4. [root@mdw ~]# ssh root@sdw02 'service ntpd restart'
  5. root@sdw02's password:
  6. Redirecting to /bin/systemctl restart ntpd.service
  7. [root@mdw ~]# ssh root@sdw03 'service ntpd restart'
  8. root@sdw03's password:
  9. Redirecting to /bin/systemctl restart ntpd.service
  10. [root@mdw ~]# ssh root@sdw04 'service ntpd restart'
  11. root@sdw04's password:
  12. Redirecting to /bin/systemctl restart ntpd.service

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

  1. [root@mdw ~]# ssh root@sdw01 'ntpq -p'
  2. root@sdw01's password:
  3. remote refid st t when poll reach delay offset jitter
  4. ==============================================================================
  5. *mdw LOCAL(0) 11 u 15 64 3 0.256 0.105 0.190
  6. [root@mdw ~]# ssh root@sdw02 'ntpq -p'
  7. root@sdw02's password:
  8. remote refid st t when poll reach delay offset jitter
  9. ==============================================================================
  10. *mdw LOCAL(0) 11 u 17 64 3 0.152 -27.998 7.016
  11. [root@mdw ~]# ssh root@sdw03 'ntpq -p'
  12. root@sdw03's password:
  13. remote refid st t when poll reach delay offset jitter
  14. ==============================================================================
  15. *mdw LOCAL(0) 11 u 15 64 3 0.221 0.084 0.079
  16. [root@mdw ~]# ssh root@sdw04 'ntpq -p'
  17. root@sdw04's password:
  18. remote refid st t when poll reach delay offset jitter
  19. ==============================================================================
  20. *mdw LOCAL(0) 11 u 15 64 3 0.239 -29.479 2.525
  21. [root@mdw ~]#

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

  1. [root@mdw ~]# chkconfig ntpd on
  2. Note: Forwarding request to 'systemctl enable ntpd.service'.
  3. Created symlink from /etc/systemd/system/multi-user.target.wants/ntpd.service to /usr/lib/systemd/system/ntpd.service.
  4.  
  5. [root@mdw ~]# ssh root@sdw01 'chkconfig ntpd on'
  6. root@sdw01's password:
  7. Note: Forwarding request to 'systemctl enable ntpd.service'.
  8. Created symlink from /etc/systemd/system/multi-user.target.wants/ntpd.service to /usr/lib/systemd/system/ntpd.service.
  9. [root@mdw ~]# ssh root@sdw02 'chkconfig ntpd on'
  10. root@sdw02's password:
  11. Note: Forwarding request to 'systemctl enable ntpd.service'.
  12. Created symlink from /etc/systemd/system/multi-user.target.wants/ntpd.service to /usr/lib/systemd/system/ntpd.service.
  13. [root@mdw ~]# ssh root@sdw03 'chkconfig ntpd on'
  14. root@sdw03's password:
  15. Note: Forwarding request to 'systemctl enable ntpd.service'.
  16. Created symlink from /etc/systemd/system/multi-user.target.wants/ntpd.service to /usr/lib/systemd/system/ntpd.service.
  17. [root@mdw ~]# ssh root@sdw04 'chkconfig ntpd on'
  18. root@sdw04's password:
  19. Note: Forwarding request to 'systemctl enable ntpd.service'.
  20. Created symlink from /etc/systemd/system/multi-user.target.wants/ntpd.service to /usr/lib/systemd/system/ntpd.service.

再次使用脚本查看集群时间,注意由于脚本执行有时差,所以会出现相差正负1秒的情况,此集群(10Mb/s的局域网)只有5台服务器,超过1秒就是不正常的了(注意:如果集群内服务器过多,此脚本出现误差会变大):

  1. [root@mdw ~]# ./get_all_host_time.sh
  2. spawn ssh root@mdw date +%Y%m%d-%H%M%S
  3. root@mdw's password:
  4. 20170524-173747
  5. spawn ssh root@sdw01 date +%Y%m%d-%H%M%S
  6. root@sdw01's password:
  7. 20170524-173747
  8. spawn ssh root@sdw02 date +%Y%m%d-%H%M%S
  9. root@sdw02's password:
  10. 20170524-173748
  11. spawn ssh root@sdw03 date +%Y%m%d-%H%M%S
  12. root@sdw03's password:
  13. 20170524-173748
  14. spawn ssh root@sdw04 date +%Y%m%d-%H%M%S
  15. root@sdw04's password:
  16. 20170524-173748

NTP 集群简略部署指南的更多相关文章

  1. Storm入门教程 第三章Storm集群安装部署步骤、storm开发环境

    一. Storm集群组件 Storm集群中包含两类节点:主控节点(Master Node)和工作节点(Work Node).其分别对应的角色如下: 主控节点(Master Node)上运行一个被称为N ...

  2. HBase 1.2.6 完全分布式集群安装部署详细过程

    Apache HBase 是一个高可靠性.高性能.面向列.可伸缩的分布式存储系统,是NoSQL数据库,基于Google Bigtable思想的开源实现,可在廉价的PC Server上搭建大规模结构化存 ...

  3. 2 Hadoop集群安装部署准备

    2 Hadoop集群安装部署准备 集群安装前需要考虑的几点硬件选型--CPU.内存.磁盘.网卡等--什么配置?需要多少? 网络规划--1 GB? 10 GB?--网络拓扑? 操作系统选型及基础环境-- ...

  4. Storm集群安装部署步骤【详细版】

    作者: 大圆那些事 | 文章可以转载,请以超链接形式标明文章原始出处和作者信息 网址: http://www.cnblogs.com/panfeng412/archive/2012/11/30/how ...

  5. JStorm集群的部署

    JStorm是一个类似Hadoop MapReduce的系统,不同的是JStorm是一套基于流水线的消息处理机制,是阿里基于Storm优化的版本,和Storm一样是一个分布式实时计算的系统,从开发角度 ...

  6. Redis集群的部署

    Redis集群分为主节点Master和从节点Slave,主节点只有1个,而从节点可以有多个,这样从节点和主节点可以进行数据的传输,Redis集群的性能将比单机环境更高,接下来是配置的过程 首先配置Ma ...

  7. Redis 集群搭建详细指南

    先有鸡还是先有蛋? 最近有朋友问了一个问题,说毕业后去大城市还是小城市?去大公司还是小公司?我的回答都是大城市!大公司! 为什么这么说呢,你想一下,无论女孩男孩找朋友都喜欢找个子高胸大的.同样的道理嘛 ...

  8. Storm集群安装部署步骤

    本文以Twitter Storm官方Wiki为基础,详细描述如何快速搭建一个Storm集群,其中,项目实践中遇到的问题及经验总结,在相应章节以"注意事项"的形式给出. 1. Sto ...

  9. HBase集群安装部署

    0x01 软件环境 OS: CentOS6.5 x64 java: jdk1.8.0_111 hadoop: hadoop-2.5.2 hbase: hbase-0.98.24 0x02 集群概况 I ...

随机推荐

  1. CSS3转换(transform)基本用法介绍

    一个炫酷的网页离不开css的transform.transition.animation三个属性,之前一直没有涉及到这块内容,刚好最近要做一个相关东西,趁此机会好好学一学这三个属性. 一.功能 实现元 ...

  2. 垃圾处理器-CMS

    一.简介 CMS垃圾收集器是一款用于老年代的,使用复制-清除-整理算法的垃圾收集器. 二.GC阶段 1.初始化标记(STW) 暂停应用程序线程,遍历 GC ROOTS 直接可达的对象并将其压入标记栈( ...

  3. 9.11、mysql增量备份和增量恢复介绍

    1.增量备份: 增量数据是从上次全量备份之后,更新的新数据,对于mysql来说,binlog日志就是mysql的增量数据: (1)按天进行备份: 周一00点全量备份 周二00点全量备份 ...... ...

  4. 从DVWA靶场学代码审计

    DVWA是较为经典的一个传统漏洞的靶场 内置了low,medium,hight,impossible四个安全级别供安全人员去研究相关漏洞.今天就来对impossible这个级别进行代码审计,从中学习一 ...

  5. Docker:Linux离线安装docker-compose

    1)首先访问 docker-compose 的 GitHub 版本发布页面 https://github.com/docker/compose/releases 2)由于服务器是 CentOS 系统, ...

  6. 关于scrollview的无限滚动效果实现

    起因及需求:做过阅读器的朋友应该知道,一般的阅读器都会有仿真.平移等特效.最近赶上真空期,项目不忙,有点时间,于是想抓起来,总结点干货. 仿真翻页及平滑翻页的基本实现: 仿真翻页,使用系统自带的UIP ...

  7. LeetCode 778. Swim in Rising Water

    题目链接:https://leetcode.com/problems/swim-in-rising-water/ 题意:已知一个n*n的网格,初始时的位置为(0,0),目标位置为(n-1,n-1),且 ...

  8. Jupyter使用快捷键

    命令行模式(按 Esc 生效) F: 查找并且替换 Ctrl-Shift-F: 打开命令配置 Ctrl-Shift-P: 打开命令配置 Enter: 进入编辑模式 P: 打开命令配置 Shift-En ...

  9. Requests方法 -- Http协议的短链接与长连接介绍

    转载于简书: 作者:熊师傅链接:https://www.jianshu.com/p/3fc3646fad80 1.以前的误解 很久之前就听说过长连接的说法,而且还知道HTTP1.0协议不支持长连接,从 ...

  10. Java-数组有关

    1.复制数组 复制数组主要有三类方法: 1.使用循环语句逐个赋值数组元素 2.使用System类中的静态方法arraycopy 3.使用clone方法复制数组 对于2,详述如下: arraycopy( ...