概述
集羣對時間同步要求高,實際使用環境中必須確保集羣中所有系統時間保持一致
Chrony是一個開源的自由軟件,像CentOS 7或基於RHEL 7操作系統,已經是默認服務,默認配置文件在 /etc/chrony.conf 它能保持系統時間與時間服務器(NTP)同步,讓時間始終保持同步。相對於NTP時間同步軟件,佔據很大優勢。
需要注意的是,配置完/etc/chrony.conf後,需重啓chrony服務,否則可能會不生效
主機名稱 | IP地址 |
Master | 192.168.1.10 |
Node1 | 192.168.1.20 |
Node2 | 192.168.1.30 |
1、安裝chrony
CentOS7中已經默認安裝了chrony,其配置文件路徑在
/etc/chrony.conf
如果系統內沒有chrony,請按照如下進行安裝,啓動並檢查相關狀態
yum install chrony -y
systemctl enable chronyd.service
systemctl restart chronyd.service
systemctl status chronyd.service
在防火牆內放行,因NTP使用123/UDP端口協議,所以允許NTP服務即可。(如果已關閉防火牆請無視)
firewall-cmd --add-service=ntp --permanent
firewall-cmd --reload
2、檢查設置時區
# timedatectl
Local time: Fri 2018-2-29 13:31:04 CST
Universal time: Fri 2018-2-29 05:31:04 UTC
RTC time: Fri 2018-2-29 08:17:20
Time zone: Asia/Shanghai (CST, +0800)
NTP enabled: yes
NTP synchronized: yes
RTC in local TZ: no
DST active: n/a
#如果你當前的時區不正確,請按照以下操作設置。
#查看所有可用的時區:
# timedatectl list-timezones
#篩選式查看在亞洲S開的上海可用時區:
# timedatectl list-timezones | grep -E "Asia/S.*"
Asia/Sakhalin
Asia/Samarkand
Asia/Seoul
Asia/Shanghai
Asia/Singapore
Asia/Srednekolymsk
#設置當前系統爲Asia/Shanghai上海時區:
# timedatectl set-timezone Asia/Shanghai
#設置完時區後,強制同步下系統時鐘:
# chronyc -a makestep
200 OK
3、Master節點配置chrony
此處選用的是阿里雲的ntp服務server,地址爲
ntp1.aliyun.com
當前是Master,ip地址爲192.168.1.10,網段是192.168.1.0/24,配置詳情如下,紅色爲更改部分
vim /etc/chrony.conf
server ntp1.aliyun.com iburst
# Record the rate at which the system clock gains/losses time.
driftfile /var/lib/chrony/drift
# Allow the system clock to be stepped in the first three updates
# if its offset is larger than 1 second.
makestep 1.0 3
# Enable kernel synchronization of the real-time clock (RTC).
rtcsync
# Enable hardware timestamping on all interfaces that support it.
#hwtimestamp *
# Increase the minimum number of selectable sources required to adjust
# the system clock.
#minsources 2
# Allow NTP client access from local network.
#allow 192.168.0.0/16
allow 192.168.1.0/24
# Serve time even if not synchronized to a time source.
local stratum 10
# Specify file containing keys for NTP authentication.
#keyfile /etc/chrony.keys
# Specify directory for log files.
logdir /var/log/chrony
# Select which information is logged.
#log measurements statistics tracking
重啓
# systemctl restart chronyd.service
# systemctl status chronyd.service
強制同步下系統時鐘:
# chronyc -a makestep
查看時間同步源狀態:
# chronyc sourcestats
查看時間同步源:
# chronyc sources -v
4、Node節點配置chrony
node節點(192.168.26.136)只需要註釋掉原來的ip,新增Master主機的IP地址即可(記得重啓chrony服務)
# systemctl restart chronyd.service
# systemctl status chronyd.service
強制同步下系統時鐘:
# chronyc -a makestep
查看時間同步源狀態:
# chronyc sourcestats
查看時間同步源:
# chronyc sources -v
Node2也是以上步驟
5、 chrony.conf文件詳解
以下是系統默認配置文件的說明(CentOS7),瞭解一下各個配置是做什麼。
# 使用pool.ntp.org項目中的公共服務器。以server開,理論上你想添加多少時間服務器都可以。
# 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
# 根據實際時間計算出服務器增減時間的比率,然後記錄到一個文件中,在系統重啓後爲系統做出最佳時間補償調整。
driftfile /var/lib/chrony/drift
# chronyd根據需求減慢或加速時間調整,
# 在某些情況下系統時鐘可能漂移過快,導致時間調整用時過長。
# 該指令強制chronyd調整時期,大於某個閥值時步進調整系統時鐘。
# 只有在因chronyd啓動時間超過指定的限制時(可使用負值來禁用限制)沒有更多時鐘更新時才生效。
makestep 1.0 3
# 將啓用一個內核模式,在該模式中,系統時間每11分鐘會拷貝到實時時鐘(RTC)。
rtcsync
# Enable hardware timestamping on all interfaces that support it.
# 通過使用hwtimestamp指令啓用硬件時間戳
#hwtimestamp eth0
#hwtimestamp eth1
#hwtimestamp *
# Increase the minimum number of selectable sources required to adjust
# the system clock.
#minsources 2
# 指定一臺主機、子網,或者網絡以允許或拒絕NTP連接到扮演時鐘服務器的機器
#allow 192.168.0.0/16
#deny 192.168/16
# Serve time even if not synchronized to a time source.
local stratum 10
# 指定包含NTP驗證密鑰的文件。
#keyfile /etc/chrony.keys
# 指定日誌文件的目錄。
logdir /var/log/chrony
# Select which information is logged.
#log measurements statistics tracking
6、chrony的優勢
chrony的優勢更快的同步只需要數分鐘而非數小時時間,從而最大程度減少了時間和頻率誤差,這對於並非全天 24 小時運行的臺式計算機或系統而言非常有用。
能夠更好地響應時鐘頻率的快速變化,這對於具備不穩定時鐘的虛擬機或導致時鐘頻率發生變化的節能技術而言非常有用。
在初始同步後,它不會停止時鐘,以防對需要系統時間保持單調的應用程序造成影響。
在應對臨時非對稱延遲時(例如,在大規模下載造成鏈接飽和時)提供了更好的穩定性。
無需對服務器進行定期輪詢,因此具備間歇性網絡連接的系統仍然可以快速同步時鐘。