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 就是集群內部的授時節點
,其他都是被授節點
。
--集群內部----- | 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.
再次使用腳本查看集群時間,注意由於腳本執行有時差,所以會出現相差正負1秒的情況,此集群(10Mb/s的局域網)只有5台服務器,超過1秒就是不正常的了(注意:如果集群內服務器過多,此腳本出現誤差會變大):
[root@mdw ~]# ./get_all_host_time.sh spawn ssh root@mdw date +%Y%m%d-%H%M%S root@mdw's password: 20170524-173747 spawn ssh root@sdw01 date +%Y%m%d-%H%M%S root@sdw01's password: 20170524-173747 spawn ssh root@sdw02 date +%Y%m%d-%H%M%S root@sdw02's password: 20170524-173748 spawn ssh root@sdw03 date +%Y%m%d-%H%M%S root@sdw03's password: 20170524-173748 spawn ssh root@sdw04 date +%Y%m%d-%H%M%S root@sdw04's password: 20170524-173748