NTP服务器时间同步
 
一、簡要說明
 二、安裝步驟
 三、配置文件
 四、常用命令
 五、注意事項
 六、運行截圖
 七、參考資料
一、簡要說明
????????? 搭建Kubernetes環(huán)境,需要幾臺、幾十臺機器配合運作,許多集群服務(wù)比如Etcd等都依賴系統(tǒng)的時間,如果機器的系統(tǒng)時間不一致,可能會出現(xiàn)各種問題。因此有必要統(tǒng)一集群內(nèi)所有服務(wù)器的系統(tǒng)時間。
????????? NTP(Network Time Protocol)可以很方便的解決服務(wù)器之間的時間同步問題,Ubuntu系統(tǒng)下NTP安裝也很方便,經(jīng)過測試,在Ubuntu 16.04環(huán)境下,直接安裝NTP服務(wù),使用Ubuntu系統(tǒng)自帶的pool ntp.ubuntu.com 時間服務(wù)器地址池,就可以實現(xiàn)時間同步。用戶也可以選擇NTP官方網(wǎng)站推薦的pool pool.ntp.org地址池,或者選擇中國區(qū)的pool cn.pool.ntp.org地址池,都會生效的。
????????? 用戶還可以選擇集群中的1臺作為主授時服務(wù)器(NTP 服務(wù)器角色),通過配置文件中的pool 地址池與上層的服務(wù)器同步時間,集群內(nèi)所有其他機器(NTP客戶端角色)的NTP配置文件中,使用Server xx.xx.xx.xx形式,明確指向主授時服務(wù)器IP地址,也可以實現(xiàn)為集群提供統(tǒng)一的時間服務(wù)。如果考慮高可靠性,還可以將多臺服務(wù)器作為集群的授時服務(wù)器。
???????? 也可以到www.ntp.org網(wǎng)站中查找中國區(qū)的服務(wù)器地址,直接在NTP配置文件中使用這些地址,比如 server? xx.xx.xx.xx 。
???????? 下面的例子中,我們使用到的服務(wù)器信息:
?????????? 服務(wù)器名?? IP地址???????????????? 角色
?????????? rancher2?? 192.168.3.220?? NTP 服務(wù)器
?????????? node221??? 192.168.3.221?? NTP 客戶端
???? 
 二、安裝步驟
 #在集群的所有機器上安裝NTP相關(guān)程序即可
 sudo apt install ntp ntpdate ntpstat
三、配置文件
 #3.1 NTP 主授時服務(wù)器(192.168.3.220)配置文件/etc/ntp.conf去除注釋后的內(nèi)容:
 driftfile /var/lib/ntp/ntp.drift
statistics loopstats peerstats clockstats
 filegen loopstats file loopstats type day enable
 filegen peerstats file peerstats type day enable
 filegen clockstats file clockstats type day enable
server 1.networktime.org iburst
 server 2.networktime.org iburst
 server ntp.synet.edu.cn iburst
 server ntp.neu6.edu.cn iburst
 server ntp.gwadar.cn iburst
 server ntp.neu.edu.cn iburst
restrict -4 default kod notrap nomodify nopeer limited
 restrict -6 default kod notrap nomodify nopeer limited
 restrict 127.0.0.1
 restrict ::1
 restrict source notrap nomodify noquery
#3.2 NTP 客戶端服務(wù)器(192.168.3.221)配置文件/etc/ntp.conf去除注釋后的內(nèi)容:
 driftfile /var/lib/ntp/ntp.drift
statistics loopstats peerstats clockstats
 filegen loopstats file loopstats type day enable
 filegen peerstats file peerstats type day enable
 filegen clockstats file clockstats type day enable
server 192.168.3.220 iburst
restrict -4 default kod notrap nomodify nopeer limited
 restrict -6 default kod notrap nomodify nopeer limited
 restrict 127.0.0.1
 restrict ::1
 restrict source notrap nomodify noquery
#3.3 其他地址池和授時服務(wù)器地址參考
 #3.3.1===Ubuntu 16.04系統(tǒng)默認(rèn)的地址池===
 pool ntp.ubuntu.com
pool 0.ubuntu.pool.ntp.org
 pool 1.ubuntu.pool.ntp.org
 pool 2.ubuntu.pool.ntp.org
 pool 3.ubuntu.pool.ntp.org
#3.3.2===www.pool.ntp.org官網(wǎng)上推薦的地址池===
 pool pool.ntp.org
pool 0.pool.ntp.org
 pool 1.pool.ntp.org
 pool 2.pool.ntp.org
 pool 3.pool.ntp.org
#3.3.3===中國區(qū)地址池===
 pool cn.pool.ntp.org
pool 0.cn.pool.ntp.org
 pool 1.cn.pool.ntp.org
 pool 2.cn.pool.ntp.org
 pool 3.cn.pool.ntp.org
#3.3.4===www.ntp.org官網(wǎng)上提供的中國區(qū)服務(wù)器地址===
server 1.networktime.org 
 server 2.networktime.org 
 server ntp.synet.edu.cn 
 server ntp.neu6.edu.cn 
 server ntp.gwadar.cn 
 server ntp.neu.edu.cn 
 四、常用命令
#停止NTP服務(wù)
 sudo service ntp stop
#只查詢、不更新本機系統(tǒng)時間
 sudo ntpdate -q pool.ntp.org
#使用debug(-d)模式查詢詳細(xì)更新信息
 sudo ntpdate -d pool.ntp.org
#直接與pool.ntp.org中的服務(wù)器同步本機系統(tǒng)時間
 sudo ntpdate pool.ntp.org
#查詢NTP連接上層授時服務(wù)器的狀態(tài)
 nptq -p
#啟動NTP服務(wù)
 sudo service ntp start
#查詢ntp運行狀態(tài)
 sudo ntpstat
#查看系統(tǒng)時間
 date
 #設(shè)置系統(tǒng)時間的日期為2018年07月09日08點44分30秒
 sudo date -s "2018/07/09 08:44:30"
#查看硬件時間
 sudo hwclock? --show
 #設(shè)置硬件時間
 sudo hwclock --set --date="07/09/18 14:55:30" 
#使用硬件時間同步系統(tǒng)時間
 sudo hwclock --hctosys
 #使用系統(tǒng)時間同步硬件時間
 sudo hwclock --systohc
 五、注意事項
????? 1、NTP服務(wù)和ntpdate命令不可同時使用。使用ntpdate之前,一定要先停止NTP服務(wù)。啟用NTP服務(wù)之前,應(yīng)先使用ntpdate命令 同步一下服務(wù)器時間,或者直接使用date命令設(shè)置系統(tǒng)時間,以免服務(wù)器時間相差太大,NTP服務(wù)不起作用。
????? 2、注意互聯(lián)網(wǎng)授時服務(wù)器地址是否可用?正式使用之前,可以使用ntpdate 驗證一下,是否可以同步時間。
????? 3、除了Linux自帶的防火墻要打開UDP123端口外,網(wǎng)絡(luò)出口防火墻也要打開UDP123端口!這樣運行ntpdate同步時間時,才不會出現(xiàn)“no server suitable for synchronization found”錯誤。
六、運行截圖
圖01-Rancher2主授時服務(wù)器配置文件-與上層的中國區(qū)的服務(wù)器地址進(jìn)行同步
圖02-Rancher2主授時服務(wù)器同步結(jié)果
圖03-NTP客戶端服務(wù)器node221,配置文件指向主授時服務(wù)器
圖04-NTP客戶端服務(wù)器node221時間同步結(jié)果
圖05-使用Ubuntu系統(tǒng)自帶的NTP地址池pool,能夠正常同步時間
圖06-使用pool.ntp.org地址池也是能正常同步時間的
圖07-使用中國區(qū)的地址池cn.pool.ntp.org 也是可以同步時間的
圖08-使用nslookup查詢cn.pool.ntp.org地址池域名的地址信息
圖09-NTP相關(guān)配置文件位置
七、參考資料
Linux NTP配置詳解 (Network Time Protocol)
https://blog.csdn.net/iloli/article/details/6431757
Linux的NTP配置總結(jié)
https://www.cnblogs.com/kerrycode/archive/2015/08/20/4744804.html
部署NTP服務(wù)器進(jìn)行時間同步
https://www.cnblogs.com/linypwb/p/5532535.html
NTP服務(wù)配置 
http://blog.sina.com.cn/s/blog_4612ed51010124e2.html
NTP時間同步問題
https://blog.csdn.net/sinat_36384705/article/details/73826408
How to Configure NTP for Use in the NTP Pool Project on Ubuntu 16.04
https://www.digitalocean.com/community/tutorials/how-to-configure-ntp-for-use-in-the-ntp-pool-project-on-ubuntu-16-04
查找中國區(qū)授時服務(wù)器
http://support.ntp.org/bin/view/Servers/StratumOneTimeServers
 http://support.ntp.org/bin/view/Servers/PublicTimeServer000934
 http://support.ntp.org/bin/view/Servers/PublicTimeServer001036
 http://support.ntp.org/bin/view/Servers/PublicTimeServer000893
 http://support.ntp.org/bin/view/Servers/StratumTwoTimeServers
 http://support.ntp.org/bin/view/Servers/PublicTimeServer001465
 http://support.ntp.org/bin/view/Servers/PublicTimeServer000794
 http://support.ntp.org/bin/view/Servers/PublicTimeServer001466
 http://support.ntp.org/bin/view/Servers/PublicTimeServer000781
 http://support.ntp.org/bin/view/Servers/PublicTimeServer000782
 http://support.ntp.org/bin/view/Servers/PublicTimeServer001237
How do I use pool.ntp.org
http://www.pool.ntp.org/zh/use.html
關(guān)于ntp時間同步理論及配置參數(shù)
https://blog.csdn.net/qq_32748887/article/details/76690944
NTP的配置總結(jié)(整理+轉(zhuǎn)載)
https://blog.csdn.net/gycool21/article/details/51746174
解決ntp的錯誤 no server suitable for synchronization found
http://www.blogjava.net/spray/archive/2008/07/10/213964.html
轉(zhuǎn)載于:https://www.cnblogs.com/rancher-maomao/p/9309990.html
總結(jié)
以上是生活随笔為你收集整理的NTP服务器时间同步的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
 
                            
                        - 上一篇: @SentinelResource注解实
- 下一篇: 22图的遍历
