1. 程式人生 > >容器學習筆記之CentOS7叢集使用Chrony實現時間同步

容器學習筆記之CentOS7叢集使用Chrony實現時間同步

 

0x00 概述

容器叢集對時間同步要求高,實際使用環境中必須確保叢集中所有系統時間保持一致,openstack官方也推薦使用chrony代替ntp做時間同步。

Chrony是一個開源的自由軟體,像CentOS 7或基於RHEL 7作業系統,已經是預設服務,預設配置檔案在 /etc/chrony.conf 它能保持系統時間與時間伺服器(NTP)同步,讓時間始終保持同步。相對於NTP時間同步軟體,佔據很大優勢。

需要注意的是,配置完/etc/chrony.conf後,需重啟chrony服務,否則可能會不生效。

Master 192.168.26.135
Node 192.168.26.136

 

0x01 安裝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

 

0x02 檢查設定時區

# 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

 

0x03 Master節點配置chrony

此處選用的是阿里雲的ntp服務server,地址為

ntp1.aliyun.com

當前是Master,ip地址為192.168.26.135,網段是192.168.26.0/24,配置詳情如下,紅色為更改部分

# 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 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.26.0/24

# Serve time even if not synchronized to a time source.
#local stratum 10
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

 

0x04 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

 

 0x05 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

 

 0x06 chronyc命令引數說明

# chronyc -a makestep 檢視ntp詳細的同步狀態

# chronyc sourcestats    #檢視同步狀態

 

0x07 chrony的優勢

chrony的優勢更快的同步只需要數分鐘而非數小時時間,從而最大程度減少了時間和頻率誤差,這對於並非全天 24 小時執行的臺式計算機或系統而言非常有用。

能夠更好地響應時鐘頻率的快速變化,這對於具備不穩定時鐘的虛擬機器或導致時鐘頻率發生變化的節能技術而言非常有用。

在初始同步後,它不會停止時鐘,以防對需要系統時間保持單調的應用程式造成影響。

在應對臨時非對稱延遲時(例如,在大規模下載造成連結飽和時)提供了更好的穩定性。

無需對伺服器進行定期輪詢,因此具備間歇性網路連線的系統仍然可以快速同步時鐘。

 參考