最近有點忙,在學習kubernetes,有一段時間沒更新了。之前感覺ntp時間同步不重要,最近接觸集群比較多,發現非常重要,而且自己走了很多坑,記錄一下。
一、環境介紹
集群3台機器:
1台機器為server節點,配置ntp-server
其余兩台為client,同步server節點的時間
如果有多台,參考client配置就好了
這里有無外網都不影響:
有外網--添加外部的阿里雲或者國家授時中心
無外網--同步本地時間,只有集群時間一樣的,環境運行就沒問題
博主這里用虛擬機演示,有外網
ntp-server-ip-192.168.1.130
ntp-client-ip-192.168.1.131
ntp-client-ip-192.168.1.132
阿里時鍾服務器:203.107.6.88
國家授時服務器:114.118.7.161
二、NTP搭建操作
1.所有機器卸載NTP,統一安裝ntp 和 ntpdate
這里建議把提前源都統一,保證ntp版本一致,或者下rpm包來安裝
#檢查是否安裝
NTP
rpm -qa | grep ntp
#已安裝刪除
ntp
yum –y remove ntpdate-4.2.6p5-22.el7.x86_64
#安裝
ntp
yum -y install ntp ntpdate
#設置時區上海
timedatectl set-timezone Asia/Shanghai
2.主節點配置ntp服務器,修改配置文件
有外網,優先同步一個外網的時間;
如果外網掛了,這里還配置了一個環回地址,保證本地時間
主節點:修改配置文件 vim /etc/ntp.conf # the administrative functions. restrict 192.168.1.130 nomodify notrap nopeer noquery 修改為ntp-server的IP restrict 203.107.6.88 <==放行 阿里雲ntp.aliyun.com 進入本 NTP 的服務器 restrict 114.118.7.161 <==放行 國家授時中心ntp.ntsc.ac.cn進入本 NTP 的服務器 restrict 127.0.0.1 restrict ::1
# Hosts on local network are less restricted. restrict 192.168.1.0 mask 255.255.255.0 nomodify notrap #填寫當前網段的,如果跨網段了,博主還沒遇到過,試試多加幾個網端或者設置成網關的IP
# Use public servers from the pool.ntp.org project. # Please consider joining the pool (http://www.pool.ntp.org/join.html).
#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 203.107.6.88 prefer <==以這部主機為最優先的server server 114.118.7.161
server 127.127.1.0 指定為本機環回地址 Fudge 127.127.1.0 stratum 10 指定為本機環回地址
3.配置ntp客戶端,修改配置文件
其他節點:ntp客戶端配置 vi /etc/ntp.conf
#添加一行為網端的 restrict 192.168.1.0 mask 255.255.255.0 nomodify notrap #填寫當前網段的,如果跨網段了,博主還沒遇到過,試試多加幾個網端或者設置成網關的IP
#注釋server部分,添加服務端地址 server 192.168.1.130 Fudge 192.168.1.130 stratum 10
4.所有啟動服務並同步硬件時間,所有主機都需要設置並統一
這里注意順序,先配置好server節點,搞定server節點后,大概幾分鍾,#啟動服務並開機啟動
systemctl restart ntpd && systemctl enable ntpd
systemctl restart ntpdate && systemctl enable ntpdate
#主節點手動同步阿里時鍾IP,同步快一點 ntpdate -u 203.107.6.88
#查看是否成功,多等一會,5到10分鍾,不行就手動同步一下
ntpq -p && ntpstat
client節點
systemctl restart ntpd && systemctl enable ntpd
systemctl restart ntpdate && systemctl enable ntpdate
#ip填寫為server端的 ntpdate -u 192.168.1.130
5.其他補充命令
同步硬件時間 sed -i "2a\SYNC_HWCLOCK=yes" /etc/sysconfig/ntpdate hwclock -w
ntpstat 檢查通信,正常可以看到對方IP ntpq -p 查看與上層的狀態 timedatectl 查看服務是否啟動
注意:
如果開機啟動不成功的,請把chronyd關閉,它與NTP都是同步時間,會沖突
systemctl stop chronyd.service
systemctl disable chronyd.service
三、簡單總結下
1.重點就是ntp.conf的配置,只有這個配置好了,就沒得問題,網上多查查或者參考我這個配置
2.容易出錯的大坑就是同步時間報錯什么 unsynchronised time server re-starting等。
不要慌,重新檢查配置文件,然后先搞定server端,待server同步ok了,在修改client配置文件,同步server
3.還有一個坑就是延時,這個同步時間不是命令輸入完成就ok了,非要等幾分鍾才行,只有耐心等待
附:參考鏈接
https://www.cnblogs.com/sonwnja/p/6767936.html