1. 程式人生 > >Linux集群的NTP服務器時間同步

Linux集群的NTP服務器時間同步

inux art .org str 使用 rontab 定時任務 emc 用戶

我們搭建集群環境的時候,時間必須是要統一的,才能保證集群數據的一致性。

一般操作是直接使用NTP,跟默認的時間服務器同步,但是最好還是讓所有節點跟集群中的某臺作為時間服務器的節點同步。

步驟:(節點有NameNode1,NameNode2,DataNode1,DataNode2,DataNode3)

  1. 選擇一臺服務器作為時間服務器。(DataNode1做為時間服務器)
  2. 使用root用戶,查看服務器是否安裝ntp服務
    rpm -qa|grep ntp

    沒有安裝的話使用yum install進行安裝。

  3. 修改文件 /etc/ntp.conf,一共修改三處內容:
  4. ①將#去掉。並且將網段修改正確。
    restrict 192.168.0.0 mask 255.255.255.0 nomodify notrap
  5. ②將以下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
  6. ③最後添加倆句話:

    server 127.127.1.0 #local clock
    fudge  127.127.1.0 stratum 10
  7. 編輯etc/sysconfig/ntpd文件
    # Drop root to id ‘ntp:ntp‘ by default.
    SYNC_HWCLOCK=yes
    OPTIONS="-u ntp:ntp -p /var/run/ntpd.pid -g"
  8. 啟動ntpd服務,並且設置開機啟動

    systemctl start ntpd.service
    systemctl enable ntpd.service
  9. 每個需要同步的子節點進行確認有沒有ntp。如果沒有的話就安裝下。
  10. 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之間同步是沒問題的。

Linux集群的NTP服務器時間同步