由ceph需用到時間同步,重溫下ntp的相關技術。
簡介:ntp全名 network time protocol 。NTP服務器可以為其他主機提供時間校對服務。
環境准備:
兩台服務器,一台作為NTP服務器,另一台作為client端向服務器同步時間測試。
NTP服務器:192.168.0.151
client端:192.168.0.152
安裝與配置:
yum install ntpd
從配置文件的角度來講解一下ntp的配置
1 # For more information about this file, see the man pages
2 # ntp.conf(5), ntp_acc(5), ntp_auth(5), ntp_clock(5), ntp_misc(5), ntp_mon(5).
3
4 driftfile /var/lib/ntp/drift #默認即可。driftfile用來指定記錄本機與上層NTP server之間的頻率誤差。單位是百萬分之一秒。
5
6 # Permit time synchronization with our time source, but do not
7 # permit the source to query or modify the service on this system.
8 restrict default nomodify notrap nopeer noquery
#restrict用來管理權限控制。格式為 restrict [單個ip|網絡|default] parameter
parameter:
ignore:拒絕所有的ntp連接
nomodify:客戶端不能使用ntpc和ntpq這兩個程序來更改服務器的時間參數,但客戶端可以通過此主機來進行網絡校時。
noquery:客戶端不能使用ntpc和ntpq等命令來查詢時間服務器,等於不提供網絡校時服務。
notrap:不提供trap這個網絡時間登陸的功能
notrust:拒絕沒有認證的客戶端
示例:restrict 192.168.0.152 nomodify
9
10 # Permit all access over the loopback interface. This could
11 # be tightened as well, but to do so would effect some of
12 # the administrative functions.
13 restrict 127.0.0.1 #以下兩條默認,放行本機來源
14 restrict ::1
15
16 # Hosts on local network are less restricted.
17 #restrict 192.168.1.0 mask 255.255.255.0 nomodify notrap
18
19 # Use public servers from the pool.ntp.org project.
20 # Please consider joining the pool (http://www.pool.ntp.org/join.html).
21 server 0.centos.pool.ntp.org iburst #以下四條為默認,注釋掉即可
22 server 1.centos.pool.ntp.org iburst
23 server 2.centos.pool.ntp.org iburst
24 server 3.centos.pool.ntp.org iburst
25 server:用來設置上層NTP服務器,說白了就是client向誰請求NTP時間同步。
特別注意,在內網環境中由於無法連接到內網,所以沒有辦法向例如國家授時服務中心210.72.145.44同步時間
只能將內網中的某台主機設置為server,用以向其他內網服務器提供NTP服務。
server 127.127.1.0 prefer #以本機時間作為時間服務。內網中這個配置一定要加上,否則會導致NTP服務不可用
#prefer代表這台主機優先級最高。
26 #broadcast 192.168.1.255 autokey # broadcast server
27 #broadcastclient # broadcast client
28 #broadcast 224.0.1.1 autokey # multicast server
29 #multicastclient 224.0.1.1 # multicast client
30 #manycastserver 239.255.254.254 # manycast server
31 #manycastclient 239.255.254.254 autokey # manycast client
32
33 # Enable public key cryptography.
34 #crypto
35
36 includefile /etc/ntp/crypto/pw
37
38 # Key file containing the keys and key identifiers used when operating
39 # with symmetric key cryptography.
40 keys /etc/ntp/keys ##除了restrict來限制客戶端連接外,還可以通過秘鑰方式來給客戶端認證。
41
42 # Specify the key identifiers which are trusted.
43 #trustedkey 4 8 42
44
45 # Specify the key identifier to use with the ntpdc utility.
46 #requestkey 8
47
48 # Specify the key identifier to use with the ntpq utility.
49 #controlkey 8
50
51 # Enable writing of statistics records.
52 #statistics clockstats cryptostats loopstats peerstats
53
54 # Disable the monitoring facility to prevent amplification attacks using ntpdc
55 # monlist command when default restrict does not include the noquery flag. See
56 # CVE-2013-5211 for more details.
57 # Note: Monitoring will not be disabled with the limited restriction flag.
58 disable monitor
示例
服務器端需要修改/etc/ntp.conf,添加以下內容
server 127.127.1.0 prefer #設置本機為NTP服務器
restrict 192.168.0.152 #允許客戶端192.168.0.152向本機請求時間同步
restrict 192.168.0.0 mask 255.255.255.0 #允許客戶端192.168.0.0網段的所有主機向本機請求時間同步

systemctl start ntpd
systemctl enable ntpd
客戶端需要修改/etc/ntp.conf,添加以下內容
server 192.168.0.151 #指名上層NTP服務器
restrict 192.168.0.151 #放行192.168.0.151

驗證
首先驗證服務是否啟動成功(服務端和客戶端都啟用ntpd服務)
啟動服務 :service ntpd start
查看服務是否啟動:netstat -tunlp |grep ntp
ntp默認監聽於UDP的123端口
其次驗證NTP是否正常工作
此處需要使用192.168.0.152這台Client服務器,在此主機上可以使用命令ntpstat或ntpq -p這兩個命令。
ntpstat:這條命令可以查看我們的客戶端(此處為192.168.0.152)是否與server(192.168.0.151) 連接成功。

上圖的大致意思是:本機已經和192.168.0.151這台位於第七層的NTP服務器同步時間,時間精確到57ms以內。每個64s去同步一次時間。
ntpq -p:此命令可以列出當前主機的NTP和上層NTP的狀態。
客戶端顯示如下

服務端顯示如下

remote:上層NTP服務器的IP或者主機名。主要最左邊的*
*:代表目前正在使用中的上層NTP
+:已經連接成功,且可以作為下一個提供時間服務的候選人。
refid:它指的是給遠程服務器(192.168.0.151)提供時間同步的服務器,本機上級的上級NTP服務器。
為什么顯示為LOCAL(0)?
由於此處為內網環境,無法去外網公用的主機同步時間,因此我們在192.168.0.151上設置的是192.168.0.151自身作為時間服務器同時,使用server 127.127.1.0 prefer指名他的上級NTP為自身。
如果可以服務端可以聯網,客戶端refid就不一定LOCAL(0),而是外網NTP服務器

服務端查看到的外網NTP服務器

st:就是stratum層級。 類似於DNS,NTP是層級結構,有頂端的服務器,最多有15層。 為了減緩負荷和網絡堵塞,原則上應該避免直接連接到級別為1的服務器。
when:幾秒之前同步過時間
poll:在過多長時間去同步時間。
reach:已經同步時間的次數
delay:網絡傳輸過程中的延遲,單位是10的-6次方秒。
offset:時間修正值,這是個最關鍵的值, 它告訴了我們本地機和服務器之間的時間差別.。單位是10的-3次方。
jitter:linux系統時間(軟件時間)與BIOS硬件時間的差異時間,單位是10的-6次方秒。可以使用 hwclock -w將系統時間寫入BIOS.
排錯1:
客戶端不是將內網NTP作為上層NTP服務器(通常是這台客戶端也是可以上外網的)

注釋掉默認的4台ntp服務器即可

排錯2:客戶端無法同步unsynchronised
[root@ceph-osd4 ~]# ntpstat
unsynchronised
polling server every 8 s
解決方法:
手動同步某一台機器的時間
依次執行下面命令實現:
systemctl stop ntpd # 先停止服務,否則ntp socket會被占用
ntpdate 192.168.0.151 # 手動執行同步
systemctl start ntpd # 繼續啟動服務
systemctl stop ntpd
ntpdate 192.168.0.151
systemctl start ntpd
實測同步時間:
ntpd啟動過程需要4分鍾左右!
[root@ceph-mon2 ~]# ntpstat
unsynchronised
time server re-starting
polling server every 8 s
在ntp server上重新啟動ntp服務后,ntp server自身或者與其server同步的客戶端需要一個時間段,這個過程約4分鍾
reach這個值是在啟動ntp server服務后,就從0開始不斷增加,當增加到17的時候,netstat就變成同步狀態。
從0到17中間進行了4次的變更(0-->1-->3-->7-->17),每一次變更是poll的值(秒數)
除了0到1只有不到1秒,1-->3-->7-->17每次都是64秒,所以同步總時間是1+64秒*3 約200秒的時間。




參考和引用文章
https://www.cnblogs.com/wxxjianchi/p/10531582.html>
