1. 程式人生 > >NTP服務器時間同步

NTP服務器時間同步

位置 之前 時間同步 unity logs 選擇 --date 內容 sha


一、簡要說明
二、安裝步驟
三、配置文件
四、常用命令
五、註意事項
六、運行截圖
七、參考資料


一、簡要說明
????????? 搭建Kubernetes環境,需要幾臺、幾十臺機器配合運作,許多集群服務比如Etcd等都依賴系統的時間,如果機器的系統時間不一致,可能會出現各種問題。因此有必要統一集群內所有服務器的系統時間。

????????? NTP(Network Time Protocol)可以很方便的解決服務器之間的時間同步問題,Ubuntu系統下NTP安裝也很方便,經過測試,在Ubuntu 16.04環境下,直接安裝NTP服務,使用Ubuntu系統自帶的pool ntp.ubuntu.com 時間服務器地址池,就可以實現時間同步。用戶也可以選擇NTP官方網站推薦的pool pool.ntp.org地址池,或者選擇中國區的pool cn.pool.ntp.org地址池,都會生效的。

????????? 用戶還可以選擇集群中的1臺作為主授時服務器(NTP 服務器角色),通過配置文件中的pool 地址池與上層的服務器同步時間,集群內所有其他機器(NTP客戶端角色)的NTP配置文件中,使用Server xx.xx.xx.xx形式,明確指向主授時服務器IP地址,也可以實現為集群提供統一的時間服務。如果考慮高可靠性,還可以將多臺服務器作為集群的授時服務器。

???????? 也可以到www.ntp.org網站中查找中國區的服務器地址,直接在NTP配置文件中使用這些地址,比如 server? xx.xx.xx.xx 。

???????? 下面的例子中,我們使用到的服務器信息:
?????????? 服務器名?? IP地址???????????????? 角色

?????????? rancher2?? 192.168.3.220?? NTP 服務器
?????????? node221??? 192.168.3.221?? NTP 客戶端
????
二、安裝步驟
#在集群的所有機器上安裝NTP相關程序即可
sudo apt install ntp ntpdate ntpstat

三、配置文件
#3.1 NTP 主授時服務器(192.168.3.220)配置文件/etc/ntp.conf去除註釋後的內容:
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 客戶端服務器(192.168.3.221)配置文件/etc/ntp.conf去除註釋後的內容:
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 其他地址池和授時服務器地址參考
#3.3.1===Ubuntu 16.04系統默認的地址池===

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官網上推薦的地址池===
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===中國區地址池===
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官網上提供的中國區服務器地址===

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服務
sudo service ntp stop

#只查詢、不更新本機系統時間
sudo ntpdate -q pool.ntp.org

#使用debug(-d)模式查詢詳細更新信息
sudo ntpdate -d pool.ntp.org

#直接與pool.ntp.org中的服務器同步本機系統時間
sudo ntpdate pool.ntp.org

#查詢NTP連接上層授時服務器的狀態
nptq -p

#啟動NTP服務
sudo service ntp start

#查詢ntp運行狀態
sudo ntpstat

#查看系統時間
date
#設置系統時間的日期為2018年07月09日08點44分30秒
sudo date -s "2018/07/09 08:44:30"

#查看硬件時間
sudo hwclock? --show
#設置硬件時間
sudo hwclock --set --date="07/09/18 14:55:30"

#使用硬件時間同步系統時間
sudo hwclock --hctosys
#使用系統時間同步硬件時間
sudo hwclock --systohc


五、註意事項
????? 1、NTP服務和ntpdate命令不可同時使用。使用ntpdate之前,一定要先停止NTP服務。啟用NTP服務之前,應先使用ntpdate命令 同步一下服務器時間,或者直接使用date命令設置系統時間,以免服務器時間相差太大,NTP服務不起作用。
????? 2、註意互聯網授時服務器地址是否可用?正式使用之前,可以使用ntpdate 驗證一下,是否可以同步時間。
????? 3、除了Linux自帶的防火墻要打開UDP123端口外,網絡出口防火墻也要打開UDP123端口!這樣運行ntpdate同步時間時,才不會出現“no server suitable for synchronization found”錯誤。

六、運行截圖

圖01-Rancher2主授時服務器配置文件-與上層的中國區的服務器地址進行同步

技術分享圖片

圖02-Rancher2主授時服務器同步結果

技術分享圖片

圖03-NTP客戶端服務器node221,配置文件指向主授時服務器

技術分享圖片

圖04-NTP客戶端服務器node221時間同步結果

技術分享圖片

圖05-使用Ubuntu系統自帶的NTP地址池pool,能夠正常同步時間

技術分享圖片

圖06-使用pool.ntp.org地址池也是能正常同步時間的

技術分享圖片

圖07-使用中國區的地址池cn.pool.ntp.org 也是可以同步時間的

技術分享圖片

圖08-使用nslookup查詢cn.pool.ntp.org地址池域名的地址信息

技術分享圖片

圖09-NTP相關配置文件位置

技術分享圖片


七、參考資料

Linux NTP配置詳解 (Network Time Protocol)
https://blog.csdn.net/iloli/article/details/6431757

Linux的NTP配置總結
https://www.cnblogs.com/kerrycode/archive/2015/08/20/4744804.html

部署NTP服務器進行時間同步
https://www.cnblogs.com/linypwb/p/5532535.html

NTP服務配置
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

查找中國區授時服務器
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

關於ntp時間同步理論及配置參數
https://blog.csdn.net/qq_32748887/article/details/76690944

NTP的配置總結(整理+轉載)
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






NTP服務器時間同步