集群架构 - Chrony时间服务

1. 时间同步概述

1.1 NTP协议概述

网络时间协议(NTP, Network Time Protocol),是用来使网络中的各个计算机时间同步的一种协议。它的用途是把计算机的时钟同步到世界协调时UTC,其精度在局域网内可达0.1ms,在互联网上绝大多数的地方其精度可以达到1-50ms。

NTP服务器: 就是利用NTP协议提供时间同步服务的。

1.2 时间同步的重要性

  1. 集群一致性: 多服务器集群需要时间同步
  2. 日志分析: 日志时间戳需要准确
  3. 安全审计: 安全事件需要准确的时间记录
  4. 数据一致性: 分布式系统需要时间同步

1.3 时间同步工具对比

工具 说明 特点
ntpdate 传统NTP客户端 一次性同步,需要手动执行
chrony 现代时间同步工具 持续同步,自动调整
ntpd 传统NTP守护进程 功能强大但配置复杂

2. 系统时区配置

2.1 时区配置的重要性

统一调整时区为亚洲上海, 如不统一则会导致时间同步产生差异。

2.2 查看当前时区

1
2
3
4
5
6
7
8
9
10
# 查看当前时区信息
[root@liyanzhao ~]# timedatectl
Local time: Sun 2018-03-18 08:46:29 CST
Universal time: Sun 2018-03-18 00:46:29 UTC
RTC time: Sun 2018-03-18 00:46:29
Time zone: Asia/Shanghai (CST, +0800)
NTP enabled: yes
NTP synchronized: yes
RTC in local TZ: no
DST active: n/a

2.3 列出可用时区

1
2
3
4
5
6
7
8
# 列出可用时区
[root@liyanzhao ~]# timedatectl list-timezones
Africa/Abidjan
Africa/Accra
...
Asia/Shanghai
Asia/Singapore
...

2.4 设置时区

1
2
3
4
5
# 必须调整时间为上海
[root@liyanzhao ~]# timedatectl set-timezone Asia/Shanghai

# 验证时区设置
timedatectl

2.5 启用网络时间同步

1
2
3
4
5
# 使用网络时间同步(考试必考题)
[root@liyanzhao ~]# timedatectl set-ntp true

# 验证NTP状态
timedatectl

2.6 timedatectl常用命令

命令 功能 示例
timedatectl 查看时间信息 timedatectl
timedatectl list-timezones 列出时区 timedatectl list-timezones
timedatectl set-timezone 设置时区 timedatectl set-timezone Asia/Shanghai
timedatectl set-ntp 启用/禁用NTP timedatectl set-ntp true
timedatectl set-time 设置时间 timedatectl set-time "2024-02-18 12:00:00"

3. chrony配置

3.1 基础环境准备

1
2
3
4
5
6
7
8
# 停止防火墙
[root@ntp-server ~]# systemctl stop firewalld
[root@ntp-server ~]# systemctl disable firewalld

# 关闭SELinux
[root@ntp-server ~]# setenforce 0
[root@ntp-server ~]# getenforce
Disabled

3.2 安装chrony时间同步服务

1
2
# 安装chrony
[root@ntp-server ~]# yum install chrony -y

3.3 使用chrony服务同步时间

1
2
# 使用网络时间同步(考试必考题)
[root@ntp-server ~]# timedatectl set-ntp true

3.4 配置时间同步服务器

1
2
3
4
5
6
7
8
9
10
11
# 配置时间同步服务器
[root@ntp-server ~]# vim /etc/chrony.conf

# 添加时间服务器
server ntp1.aliyun.com iburst
server ntp2.aliyun.com iburst
server ntp3.aliyun.com iburst

# 允许NTP客户端访问(允许内网访问)
# Allow NTP client access from local network.
allow 192.168.69.0/24

chrony.conf主要配置项:

配置项 说明 示例
server 指定NTP服务器 server ntp1.aliyun.com iburst
allow 允许访问的网段 allow 192.168.69.0/24
local stratum 本地时间层级 local stratum 10
makestep 时间步进 makestep 1.0 3

3.5 启动chrony服务

1
2
3
4
5
6
7
8
# 设置开机自启
[root@ntp-server ~]# systemctl enable chronyd

# 启动服务
[root@ntp-server ~]# systemctl start chronyd

# 查看服务状态
systemctl status chronyd

3.6 验证服务端是否能同步时间

1
2
3
4
5
6
7
8
9
# 查看时间源
[root@ntp-server ~]# chronyc sources
210 Number of sources = 1
MS Name/IP address Stratum Poll Reach LastRx Last sample
===============================================================================
^~ localhost 2 6 377 41 -10.8s[ -10.8s] +/- 11ms

# 查看同步状态
chronyc tracking

chronyc sources输出说明:

符号 说明
^ 当前使用的时间源
* 已选择的时间源
+ 可接受的时间源
- 不可接受的时间源
? 状态未知
x 时间源被认为虚假
~ 时间源具有可变时间

4. ntpdate同步

客户端要等几分钟再与新启动的ntp服务器进行时间同步,否则会提示no server suitable for synchronization found错误。

4.1 客户端安装ntpdate

1
2
# 客户端安装ntpdate命令进行同步(第一种方式)
[root@ntp-client ~]# yum install ntpdate -y

4.2 手动同步时间

1
2
3
# 手动同步时间
[root@ntp-client ~]# ntpdate 192.168.69.112
18 Mar 11:01:47 ntpdate[18913]: step time server 192.168.69.112 offset -9.805077 sec

4.3 将同步命令写入计划任务

1
2
3
4
5
# 编辑计划任务
[root@node1 ~]# crontab -e

# 每2分钟同步一次时间
*/2 * * * * /usr/sbin/ntpdate 172.16.56.11 &>/dev/null

4.4 ntpdate常用选项

选项 功能 示例
-q 查询模式,不设置时间 ntpdate -q 192.168.1.1
-d 调试模式 ntpdate -d 192.168.1.1
-s 通过syslog记录输出 ntpdate -s 192.168.1.1
-u 使用非特权端口 ntpdate -u 192.168.1.1

4.5 ntpdate注意事项

  1. 等待时间: 新启动的NTP服务器需要等待几分钟才能同步
  2. 一次性同步: ntpdate是一次性同步,不会持续调整
  3. 时间跳跃: 如果时间差异较大,会直接跳跃,可能影响应用

5. chrony同步(客户端)

客户端安装chrony服务同步内网时间服务器(第二种方式)。

5.1 环境准备

1
2
3
# 环境准备
[root@ntp-client ~]# systemctl stop firewalld
[root@ntp-client ~]# setenforce 0

5.2 安装时间同步服务

1
2
3
4
5
# 安装时间同步
[root@ntp-client ~]# yum install chrony -y

# 启用网络时间同步
[root@ntp-client ~]# timedatectl set-ntp true

5.3 配置时间同步服务器

1
2
3
4
5
6
7
8
# 配置时间同步服务器
[root@ntp-client ~]# vim /etc/chrony.conf

# 指定内部时间服务器地址
server 192.168.69.112 iburst

# 如果同步失败,可以使用外部时间服务器作为备用
# server ntp1.aliyun.com iburst

5.4 启动chrony服务

1
2
3
4
5
6
# 启动chrony服务
[root@ntp-client ~]# systemctl enable chronyd
[root@ntp-client ~]# systemctl start chronyd

# 查看服务状态
systemctl status chronyd

5.5 客户端同步验证

1
2
3
4
5
6
7
8
9
# 客户端同步验证
[root@ntp-client ~]# chronyc sources
210 Number of sources = 1
MS Name/IP address Stratum Poll Reach LastRx Last sample
===============================================================================
^? 192.168.69.112 3 6 376 206 -14.1s[ -14.1s] +/- 12.6s

# 查看同步状态
chronyc tracking

5.6 chronyc常用命令

命令 功能 示例
chronyc sources 查看时间源 chronyc sources
chronyc tracking 查看同步状态 chronyc tracking
chronyc sourcestats 查看时间源统计 chronyc sourcestats
chronyc makestep 强制同步 chronyc makestep
chronyc activity 查看活动时间源 chronyc activity

6. chrony与ntpdate对比

6.1 对比说明

特性 chrony ntpdate
同步方式 持续同步 一次性同步
时间调整 平滑调整 直接跳跃
配置复杂度 中等 简单
适用场景 生产环境 临时同步
服务状态 守护进程 命令执行

6.2 选择建议

  • 生产环境: 推荐使用chrony,持续同步,平滑调整
  • 临时同步: 可以使用ntpdate快速同步
  • 集群环境: 必须使用chrony,保证时间一致性

7. chrony配置文件详解

7.1 /etc/chrony.conf主要配置

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
# 时间服务器配置
server ntp1.aliyun.com iburst
server ntp2.aliyun.com iburst

# 允许访问的网段
allow 192.168.69.0/24

# 本地时间层级(当无法同步外部时间时使用)
local stratum 10

# 时间步进(如果时间差异超过1秒,立即调整)
makestep 1.0 3

# 日志目录
logdir /var/log/chrony

7.2 配置参数说明

参数 说明 示例
server NTP服务器地址 server ntp1.aliyun.com iburst
iburst 启动时快速同步 server ntp1.aliyun.com iburst
allow 允许访问的网段 allow 192.168.69.0/24
local stratum 本地时间层级 local stratum 10
makestep 时间步进阈值 makestep 1.0 3
logdir 日志目录 logdir /var/log/chrony

8. 时间同步实战案例

8.1 案例1: 内网时间服务器配置

服务端配置

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#!/bin/bash
# 内网时间服务器配置脚本

# 1. 安装chrony
yum install -y chrony

# 2. 配置chrony
cat > /etc/chrony.conf <<EOF
# 使用阿里云NTP服务器
server ntp1.aliyun.com iburst
server ntp2.aliyun.com iburst
server ntp3.aliyun.com iburst

# 允许内网访问
allow 192.168.69.0/24

# 本地时间层级
local stratum 10

# 时间步进
makestep 1.0 3
EOF

# 3. 启动服务
systemctl enable chronyd
systemctl start chronyd

# 4. 验证
chronyc sources

客户端配置

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#!/bin/bash
# 客户端时间同步配置脚本

# 1. 安装chrony
yum install -y chrony

# 2. 配置chrony
cat > /etc/chrony.conf <<EOF
# 使用内网时间服务器
server 192.168.69.112 iburst

# 备用外部服务器
server ntp1.aliyun.com iburst
EOF

# 3. 启动服务
systemctl enable chronyd
systemctl start chronyd

# 4. 验证
chronyc sources
chronyc tracking

8.2 案例2: 多级时间同步架构

1
2
3
4
5
6
7
8
外部NTP服务器 (阿里云)

内网时间服务器 (192.168.69.112)

应用服务器集群
├── 服务器1
├── 服务器2
└── 服务器3

8.3 案例3: 时间同步监控脚本

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#!/bin/bash
# 时间同步监控脚本

NTP_SERVER="192.168.69.112"
THRESHOLD=1.0

# 检查时间差异
OFFSET=$(chronyc tracking | grep "System time" | awk '{print $4}')

# 如果时间差异超过阈值,发送告警
if [ $(echo "$OFFSET > $THRESHOLD" | bc) -eq 1 ]; then
echo "警告: 时间同步差异过大 (${OFFSET}秒)" | \
mail -s "时间同步告警" admin@example.com
fi

9. 时间同步故障排查

9.1 常见问题

问题1: 无法同步时间

1
2
3
4
5
6
7
8
9
10
11
# 检查chrony服务状态
systemctl status chronyd

# 检查时间源
chronyc sources

# 检查网络连通性
ping ntp1.aliyun.com

# 检查防火墙
systemctl status firewalld

问题2: 时间差异过大

1
2
3
4
5
# 强制同步
chronyc makestep

# 或使用ntpdate快速同步
ntpdate 192.168.69.112

问题3: 客户端无法连接服务端

1
2
3
4
5
6
7
8
# 检查服务端allow配置
grep allow /etc/chrony.conf

# 检查防火墙规则
iptables -L -n

# 检查chrony端口(UDP 123)
netstat -unlp | grep 123

9.2 排查步骤

  1. 检查服务状态: systemctl status chronyd
  2. 检查时间源: chronyc sources
  3. 检查同步状态: chronyc tracking
  4. 检查网络: ping ntp_server
  5. 检查防火墙: systemctl status firewalld
  6. 查看日志: journalctl -u chronyd

10. 时间同步最佳实践

10.1 配置建议

  1. 统一时区: 所有服务器使用相同的时区
  2. 内网时间服务器: 生产环境使用内网时间服务器
  3. 多级架构: 大型集群使用多级时间同步架构
  4. 监控告警: 监控时间同步状态,设置告警

10.2 安全建议

  1. 访问控制: 使用allow限制访问范围
  2. 防火墙: 合理配置防火墙规则
  3. 日志记录: 记录时间同步日志

10.3 性能优化

  1. iburst选项: 启动时快速同步
  2. 多时间源: 配置多个时间源提高可靠性
  3. 本地层级: 配置本地时间层级作为备用

11. 命令总结

11.1 时区管理命令

命令 功能 示例
timedatectl 查看时间信息 timedatectl
timedatectl list-timezones 列出时区 timedatectl list-timezones
timedatectl set-timezone 设置时区 timedatectl set-timezone Asia/Shanghai
timedatectl set-ntp 启用NTP timedatectl set-ntp true

11.2 chrony管理命令

命令 功能 示例
systemctl start chronyd 启动服务 systemctl start chronyd
systemctl status chronyd 查看状态 systemctl status chronyd
chronyc sources 查看时间源 chronyc sources
chronyc tracking 查看同步状态 chronyc tracking
chronyc makestep 强制同步 chronyc makestep

11.3 ntpdate命令

命令 功能 示例
ntpdate 同步时间 ntpdate 192.168.1.1
ntpdate -q 查询模式 ntpdate -q 192.168.1.1
ntpdate -d 调试模式 ntpdate -d 192.168.1.1

12. 常用NTP服务器

12.1 国内NTP服务器

服务器 地址
阿里云 ntp1.aliyun.com
腾讯云 ntp.tencent.com
国家授时中心 cn.pool.ntp.org

12.2 国际NTP服务器

服务器 地址
NTP Pool pool.ntp.org
Google time.google.com
Microsoft time.windows.com

实战优化