内容简介:本文风哥教程参考Linux官方文档、Red Hat Enterprise Linux官方文档、Ansible Automation Platform官方文档、Docker官方文档、Kubernetes官方文档和Podman官方文档等内容,详细介绍了相关技术的配置和使用方法。
风哥提示:
>本文档介绍企业级时间同步服务部署综合实战案例。
Part01-Chrony时间服务器
1.1 Chrony服务配置
[root@fgedu-ntp ~]# yum install -y chrony
# 配置Chrony服务器
[root@fgedu-ntp ~]# cat > /etc/chrony.conf << 'EOF'
# 使用国内NTP服务器
server ntp.aliyun.com iburst
server ntp.tencent.com iburst
server time.windows.更多视频教程www.fgedu.net.cncom iburst
# 允许本地网络同步
allow 192.16学习交流加群风哥QQ1132571748.0.0/16
allow 10.0.0.0/8
# 本地时钟作为后备
local stratum 10
# 记录时钟漂移
driftfile /var/lib/chrony/drift
# 日志配置
logdir /var/log/chrony
log measurements statistics tracking
# 系统时钟设置
makestep 1.0 3
rtcsync
EOF
# 启动Chrony
[root@fgedu-ntp ~]# systemctl enable chronyd --now
# 查看同步状态
[root@fgedu-ntp ~]# chronyc sources
210 Number of sources = 3
MS Name/IP address Stratum Poll Reach LastRx Last sample
===============================================================================
^* 203.107.6.88 2 6 37 15 -23us[ -23us] +/- 23ms
^+ 139.199.215.251 2 6 37 16 -123us[ -123us] +/- 35ms
^- 52.231.114.183 3 6 37 17 +456us[ +456us] +/- 58ms
[root@fgedu-ntp ~]# chronyc tracking
Reference ID : CB6B0658 (203.107.6.88)
Stratum : 3
Ref time (UTC) : Sat Apr 04 15:00:00 2026
System time : 0.000000123 seconds fast of NTP time
Last offset : -0.000023456 seconds
RMS offset : 0.000123456 seconds
Frequency : 12.345 ppm fast
Residual freq : -0.001 ppm
Skew : 0.123 ppm
Root delay : 0.023456 seconds
Root dispersion : 0.001234 seconds
Update interval : 64.2 seconds
Leap status : Normal
Part02-客户端配置
2.1 客户端时间同步
[root@fgedu-client ~]# cat > /etc/chrony.conf << 'EOF' # 使用内部NTP服务器 server 192.168.1.10 iburst server 192.168.1.11 iburst # 记录时钟漂移 driftfile /var/lib/chrony/drift # 系统时钟设置 makestep 1.0 3 rtcsync # 日志配置 logdir /var/log/chrony EOF [root@fgedu-client ~]# systemctl restart chronyd # 手动同步时间 [root@fgedu-client ~]# chronyc makestep 200 OK # 查看同步状态 [root@fgedu-client ~]# chronyc sources 210 Number of sources = 2 MS Name/IP address Stratum Poll Reach LastRx Last sample =============================================================================== ^* 192.168.1.10 3 6 37 15 -12us[ -12us] +/- 23ms ^+ 192.168.1.11 3 6 37 16 -45us[ -45us] +/- 35ms # 验证时间 [root@fgedu-client ~]# timedatectl Local time: Sat 2026-04-04 23:00:00 CST Universal time: Sat 2026-04-04 15:00:00 UTC RTC time: Sat 2026-04-04 15:00:00 Time zone: Asia/Shanghai (CST, +0800) System clock synchronized: yes NTP service: active RTC in local TZ: no
Part03-PTP精确时间
3.1 PTP配置
[root@fgedu-ptp ~]# yum install -y linuxptp
# 配置PTP主时钟
[root@fgedu-ptp ~]# cat > /etc/ptp4l.conf << 'EOF'
[global]
# 网络接口
tx_timestamp_timeout 10
logMinMeanPdelayReqInterval -3
logSyncInterval -3
# 时钟精度
clockClass 248
clockAccuracy 0xFE
offsetScaledLogVariance 0xFFFF
# 优先级
priority1 128
priority2 128
# 其他设置
domainNumber 0
firstStepThreshold 0.00002
maxStepThreshold 0.00002
EOF
# 启动PTP主时钟
[root@fgedu-ptp ~]# ptp4l -i eth0 -m -S &
# 配置PTP从时钟
[root@fgedu-ptp-client ~]# cat > /etc/ptp4l.conf << 'EOF'
[global]
tx_timestamp_timeout 10
logMinMeanPdelayReqInterval -3
logSyncInterval -3
domainNumber 0
EOF
[root@fgedu-ptp-client ~]# ptp4l -i eth0 -m -S -s &
# 配置phc2sys同步系统时钟
[root@fgedu-ptp-client ~]# phc2sys -s eth0 -w -m &
# 查看PTP状态
[root@fgedu-ptp ~]# ptp4l -i eth0 -m -S 2>&1 | head -20
ptp4l[123.456]: selected /dev/ptp0 as PTP clock
ptp4l[123.456]: port 1: INITIALIZING to LISTENING on INIT_COMPLETE
ptp4l[123.456]: port 0: INITIALIZING to LISTENING on INIT_COMPLETE
ptp4l[123.456]: port 1: LISTENING to MASTER on ANNOUNCE_RECEIPT_TIMEOUT_EXPIRES
ptp4l[123.456]: port 1: new foreign master 123456.abc.def-123456
ptp4l[123.456]: selected best master clock 123456.abc.def-123456
ptp4l[123.456]: port 1: MASTER to SLAVE on RS_SLAVE
Part04-时间监控
4.1 时间服务监控
[root@fgedu-ntp ~]# cat > /usr/local/bin/time-monitor.sh << 'EOF' #!/bin/bash # time-monitor.sh # from:www.itpux.com.qq113257174.wx:itpux-com # web: http://www.fgedu.net.cn echo "=== 时间服务监控 ===" echo "监控时间: $(date)" echo "" echo "1. 系统时间" timedatectl echo "" echo "2. Chrony状态" systemctl is-active chronyd echo "" echo "3. 同步源状态" chronyc sources echo "" echo "4. 同步统计" chronyc sourcestats echo "" echo "5. 客户端连接" chronyc clients echo "" echo "6. 时间偏差检查" offset=$(chronyc tracking | grep "Last offset" | awk '{print $4}') echo "时间偏差: $offset 秒" # 检查偏差是否过大 if (( $(echo "$offset > 0.1″ | bc -l) )); then
echo “警告: 时间偏差过大!”
fi
echo “”
echo “=== 监控完成 ===”
EOF
[root@fgedu-ntp ~]# chmod +x /usr/local/bin/time-monitor.sh
# 配置Prometheus监控
[root@fgedu-ntp ~]# cat > /usr/loca学习交流加群风哥微信: itpux-coml/bin/chrony-exporter.sh << 'EOF'
#!/bin/bash
# chrony-exporter.sh
# from:www.itpux.com.qq113257174.wx:itpux-com
# web: http://www.fgedu.net.cn
echo "# HELP chrony_tracking Tracking stats"
echo "# TYPE chrony_tracking gauge"
tracking=$(chronyc tracking 2>/dev/null)
echo “chrony_tracking_stratum{server=\”$(hostname)\”} $(echo “$tracking” | grep Stratum | awk ‘{print $3}’)”
echo “chrony_tracking_frequency{server=\”$(hostname)\”} $(echo “$tracking” | grep Frequency | awk ‘{print $3}’)”
echo “chrony_tracking_root_delay{server=\”$(hostname)\”} $(echo “$tracking” | grep “Root delay” | awk ‘{print $4}’)”
echo “chrony_tracking_root_dispersion{server=\”$(hostname)\”} $(echo “$tracking” | grep “Root dispersion” | awk ‘{print $4}’)”
EOF
# 配置Node Exporter textfile收集器
[root@fgedu-ntp ~]# mkdir -p /var/lib/node_exporter/textfile_collector
[root@fgedu-ntp ~]# echo “* * * * * root /usr/local/bin/chrony-exporter.sh > /var/lib/node_exporter/textfile_collector/chrony.prom” >> /etc/crontab
- 配置冗余NTP服务器
- 使用Chrony替代ntpd
- 监控时间偏差
- 配置时区统一
- 定期检查时间同步状态
本文由风哥教程整理发布,仅用于学习测试使用,转载注明出处:http://www.fgedu.net.cn/10327.html
