前言:我們在搭建集群時必須保證集群中每一台主機的時間都是同步的,下面就具體講解如何進行時間同步.
一 時間服務器的作用
設備集群需要有一個統一的時間,來確保所執行的操作的時序正確。
NTP 是網絡時間協議(Network Time Protocol)的簡稱,就是通過網絡協議使計算機之間的時間同步化。
例如服務器集群進行文件創建、數據庫處理等操作的時間。
二 時區管理
1 查看當前時區
timedatectl
[root@master ~]# timedatectlLocal time: Sat 2019-02-16 23:07:46 CSTUniversal time: Sat 2019-02-16 15:07:46 UTCRTC time: Sat 2019-02-16 15:07:45Time zone: Asia/Shanghai (CST, +0800)NTP enabled: yesNTP synchronized: yesRTC in local TZ: noDST active: n/a
2 查看當前可用時區
timedatectl list-timezones
Africa/AbidjanAfrica/AccraAfrica/Addis_AbabaAfrica/AlgiersAfrica/AsmaraAfrica/Bamako…
3 設置時區
timedatectl set-timezone Asia/Shanghai
三 進行時間同步方法
3.1 最簡單的方法,讓所有集群(這裡默認集群中有三台服務器:master,slave1,slave2)中的主機跟某個時間服務器的時間同步,執行 ntpdate 時間服務器,
例如:
[root@master ~]# ntpdate http://ntp1.aliyun.com
[root@slave1 ~]# ntpdate http://ntp1.aliyun.com
[root@slave2 ~]# ntpdate http://ntp1.aliyun.com
不過,這種方法不好.ntpdate同步時間是跳躍的,這將導致文件時間標記,監控數據的紊亂.
而且ntpdate只運行一次就結束,即只同步一次.所以即使我們要寫這樣寫
vi /etc/crontab
* * * * root /usr/sbin/ntpdate ntp1.aliyun.com && /sbin/hwclock -w
3.2 配置遠程的時間服務器,再讓所有的主機跟他同步
3.3 配置本地的時間服務器,再讓所有的主機跟他同步
下面的 四 配置ntp服務中就是同時配置了遠程和本地的時間服務器,配置目的是先使用遠程時間服務器同步,如果外網發生了問題,再使用本地時間服務器.
四 配置ntp服務
最終目的:將我們的一台主機配置成ntp服務器,同網段的其他主機可以通過ntpdate -u host-addr命令以ntp服務器的時間來進行客戶端的時間同步。
4.1 服務端
4.1.1 工具安裝
我們需要先安裝ntp服務和ntpdate工具:yum -y install ntp ntpdate即使是作為服務端的主機,在必要時刻也是需要向公用的ntp服務器進行時間同步的(一般不用)。
4.1.2 進行ntp服務文件配置
[root@master ~]# vim /etc/ntp.conf
注意:
1.大家自己在配置時,只需要把我的內容替換你的內容即可
2.#授權下述網段上所有的機器允許從ntp服務器上查詢和同步時間restrict 192.168.10.0 mask 255.255.255.0 nomodify notrap
保證這裡的網段是你集群的網段即可
driftfile /var/lib/ntp/drift
# 默認情況下,NTP服務器的日誌保存在 /var/log/messages.當然我們也可以自己指定
# 自己指定日誌目錄
# 我們要確保他的屬性和SELinux環境(這兩項一般不用改)
# chown ntp:ntp /var/log/ntpd.log
# chcon -t ntpd_log_t /var/log/ntpd.log
logfile /var/log/ntpd.log
restrict default nomodify notrap nopeer noquery
# 給與本機所有權限
restrict 127.0.0.1
restrict ::1
#授權下述網段上所有的機器允許從ntp服務器上查詢和同步時間
restrict 192.168.10.0 mask 255.255.255.0 nomodify notrap
#增加下述幾個時間服務器列表,除了0.asia.pool.ntp.org還會有很多時間服務器.比如0.cn.pool.ntp.org或者time.nist.gov或者
server 0.asia.pool.ntp.org iburst
server 1.asia.pool.ntp.org iburst
server 2.asia.pool.ntp.org iburst
server 3.asia.pool.ntp.org iburst
#這兩行內容表示當外部時間不可用時,使用本地時間
server 127.127.1.0 iburst
fudge 127.127.1.0 stratum 10
#下述四行表示允許上層服務器修改本機時間
restrict 0.asia.pool.ntp.org nomodify notrap noquery
restrict 1.asia.pool.ntp.org nomodify notrap noquery
restrict 2.asia.pool.ntp.org nomodify notrap noquery
restrict 3.asia.pool.ntp.org nomodify notrap noquery
includefile /etc/ntp/crypto/pw
keys /etc/ntp/keys
disable monitor
4.1.3 設置開機自啟動服務(初始化)
使服務端服務ntp的守護進程ntpd生效
[root@master ~]# systemctl enable ntpd
Created symlink from /etc/systemd/system/multi-user.target.wants/ntpd.service to /usr/lib/systemd/system/ntpd.service.
使客戶端工具ntpdate工具生效(選做)
[root@master ~]# systemctl enable ntpdate
Created symlink from /etc/systemd/system/multi-user.target.wants/ntpdate.service to /usr/lib/systemd/system/ntpdate.service.
檢查
[root@master ~]# systemctl is-enabled ntpd
顯示: enabled
4.1.4 啟用ntp服務
[root@master ~]# systemctl start ntpd
查看ntpd進程
[root@master ~]# ps -ef | grep ntpd
ntp 1185 1 0 03:50 ? 00:00:00 /usr/sbin/ntpd -u ntp:ntp -groot 1663 1136 0 04:35 pts/2 00:00:00 grep –color=auto ntp
進程存在,說明服務已經正常啟動
4.1.5 設置防火牆,放行ntp協議請求
提前將防火牆關閉即可
4.1.6 設置硬件時間
4.1.6.1 對時間的解釋
linux的時間分為系統時間和硬件時間。
系統時間:
通常在開機時複製硬件時間,之後獨立運行並保存了時間、時區和夏令時設置。通過date命令設置。
硬件時間:
(RTC、Real-Time Clock),CMOS時間,在主板上靠電池供電,僅保存時期時間數值。通過hwclock命令設置,在這裡,我們用系統時間同步硬件時間:hwclock -w
同步前需要測試ntp上層服務器的連通性
4.1.6.2 查看ntp的情況
[root@master ~]# ntpq -p
remote refid st t when poll reach delay offset jitter
==============================================================================
+electrode.felix 89.231.96.83 2 u 56 64 1 286.235 9.133 0.971
*undefined.hostn 127.67.113.92 2 u 56 64 1 202.865 -7.224 13.201
-de-user.deepini 195.13.23.5 3 u 58 64 1 273.953 16.689 2.172
+ntp5.flashdance 192.36.143.153 2 u 55 64 1 321.711 10.639 4.733
LOCAL(0) .LOCL. 10 l 64 64 2 0.000 0.000 0.000
解釋:
remote:即NTP主機的IP或主機名稱.注意最左邊是+表示目前正在起作用的上層NTP,如果是*表示這個也連接上了,不過是作為次要聯機的NTP主機
refid:參考的上一層NTP主機的地址
st:即stratum階層
t:是連接類型. u代表單播(unicast) l代表本地(local) ,其他的還有多播(multicast),廣播(broadcast)
when:這個時間之前剛剛做過時間同步
poll:下次更新在幾秒之後
reach:已經向上層NTP服務器要求更新的次數
delay:網絡傳輸過程中的延遲時間
offset:時間補償的結果
jitter:Linux系統時間和Bios硬件時間的差異時間
4.1.6.3 執行同步
[root@master ~]# hwclock -w
4.1.6.4 測試
[root@master conf]# ntpstatsynchronised to NTP server (78.46.102.180) at stratum 3 time correct to within 189 ms polling server every 64 s
說明本地已經與時間服務器實現了同步
4.2 客戶端
4.2.1 安裝ntp服務和ntpdate工具
[root@slave1 ~]# yum -y install ntp ntpdate
4.2.2 方法一
1 配置用於同步的服務器
先執行hwclock -w 讓系統時間和bios時間同步.
[root@slave1 ~]# hwclock -w
再執行下面的命令
[root@slave1 ~]# echo “server 192.168.10.200” >/etc/ntp.conf
2 重啟服務以使配置生效,之後大概要等10分鐘左右,才會同步成功
[root@slave1 ~]# systemctl enable ntpd
[root@slave1 ~]# systemctl restart ntpd
3 這樣做的好處:
a 客戶端的ntpd服務始終運行着,定期同步時間,不用我們每次都手動同步或者寫定時器
b ntpd服務是慢慢改變時間直至標準時間
4 注意點:
最好先執行hwclock -w,否則如果bios時間和系統時間差異超過了30分鐘,就會報錯
FAQ:系統報錯-Sep 25 12:23:33 localhost kerne: set_rtc_mmss: can’t update from 3 to 22
4.2.3 方法二
1 進行客戶端與服務器端的時間同步
[root@slave1 ~]# systemctl enable ntpdate
[root@slave1 ~]# /usr/sbin/ntpdate -u 192.168.10.200
28 Mar 04:54:43 ntpdate[1727]: adjust time server 192.168.10.200 offset 0.000001 sec
2 讓系統時間和硬件時間同步
[root@slave1 ~]# hwclock -w
3 可以設置定時器,定時執行,因為ntpdate每次執行完就失效了.
[root@slave1 ~]# crontab -e
每天和主機同步一次
10 23 * * * (/usr/sbin/ntpdate -u 192.168.10.200 && /sbin/hwclock -w) &> /var/log/ntpdate.log
4 常見錯誤分析
常見錯誤分析: 客戶端 執行 ntpdate master 顯示:no server suitable for synchronization found
原因:錯誤1.Server dropped: Strata too high(在客戶端執行 ntpdate -d master可以看到,且顯示“stratum 16”。而正常情況下stratum這個值得範圍是“0~15”)
解決:
這是因為NTP server還沒有和其自身或者它的server同步上。
以下的定義是讓NTP Server和其自身保持同步,如果在/ntp.conf中定義的server都不可用時,將使用local時間作為ntp服務提供給ntp客戶端。
server 127.127.1.0
fudge 127.127.1.0
stratum 8
原創文章,作者:投稿專員,如若轉載,請註明出處:https://www.506064.com/zh-hant/n/209025.html