我們搭建集群環境的時候,時間必須是要統一的,才能保證集群數據的一致性。
一般操作是直接使用NTP,跟默認的時間服務器同步,但是最好還是讓所有節點跟集群中的某台作為時間服務器的節點同步。
步驟:(節點有NameNode1,NameNode2,DataNode1,DataNode2,DataNode3)
- 選擇一台服務器作為時間服務器。(DataNode1做為時間服務器)
- 使用root用戶,查看服務器是否安裝ntp服務
rpm -qa|grep ntp
沒有安裝的話使用yum install進行安裝。
- 修改文件 /etc/ntp.conf,一共修改三處內容:
- ①將#去掉。並且將網段修改正確。
restrict 192.168.0.0 mask 255.255.255.0 nomodify notrap
- ②將以下4個server進行#注釋掉
#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 127.127.1.0 #local clock fudge 127.127.1.0 stratum 10
- 編輯etc/sysconfig/ntpd文件
# Drop root to id 'ntp:ntp' by default. SYNC_HWCLOCK=yes OPTIONS="-u ntp:ntp -p /var/run/ntpd.pid -g"
-
啟動ntpd服務,並且設置開機啟動
systemctl start ntpd.service systemctl enable ntpd.service
- 每個需要同步的子節點進行確認有沒有ntp。如果沒有的話就安裝下。
- root賬戶下在每個需要同步的子節點編寫crontab任務(crontab -e)。這個任務的意義就是每10分鍾和master01同步下服務器時間。
0-59/10 * * * * /usr/sbin/ntpdate master01.timer.cn
附:
1.作為時間服務器的節點的時間校准可以是人工指定(不與其他集群協作的情況下),也可以是跟其他組織的時間服務器同步
這里只說第二種情況。首先要確認當前服務器的時區
將時區設為上海
rm -rf /etc/localtime ln -s /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
然后通過定時任務,與阿里雲的時間服務器 ntp1.aliyun.com 同步(方法同上面第10步,ntp1~ntp7)
2.不同操作系統之間也是可以做時間同步的。linux與windows之間同步是沒問題的。