docker 搭建时间服务器ntp

一、chrony介绍

chrony是网络时间协议 (NTP) 的通用实现。它可以将系统时钟与 NTP 服务器、参考时钟(例如 GPS 接收器)以及使用手表和键盘的手动输入同步。

二、容器部署

1)容器镜像介绍

默认情况下,此容器使用 CloudFlare 的时间服务器 (time.cloudflare.com)。如果您想使用一个或多个不同的 NTP 服务器,您可以向该容器传递一个NTP_SERVERS 环境变量。

env=NTP_SERVERS=""

2)部署ntp服务器

docker_name='ntp'

# 更新docker
docker pull cturra/ntp:latest

# 停用docker
if (docker ps -a --format '{{.Names}}' | grep ${docker_name}); then
    # 如果容器存在,则停止并删除
	docker stop ${docker_name}
	docker rm -f ${docker_name}
fi

# 重新启动
docker run -i -t -d \
--restart=always \
--name=${docker_name} \
-e TZ=Asia/Shanghai \
-p 123:123/udp \
-e NTP_SERVERS="ntp.aliyun.com" \
cturra/ntp:latest

# --read-only \
# --detach \
# --tmpfs=/etc/chrony:rw,mode=1750 \
# --tmpfs=/run/chrony:rw,mode=1750 \
# --tmpfs=/var/lib/chrony:rw,mode=1750 \

3)查看NTP容器状态

docker exec ntp chronyc sources
[root@centos91 ~]# docker exec ntp chronyc sources
MS Name/IP address         Stratum Poll Reach LastRx Last sample
===============================================================================
^* 203.107.6.88                  2   6    17    38  +7052ms[+7052ms] +/-   35ms

4)修改客户端

安装 chronyc

if [ ! -f "/usr/bin/chronyc" ];
then
    yum install -y chrony
fi

客户端查看chronyd服务状态

systemctl enable chronyd
systemctl start chronyd

systemctl status chronyd

查看时间同步状态

chronyc sources
[root@centos91 ~]# chronyc sources
210 Number of sources = 4
MS Name/IP address         Stratum Poll Reach LastRx Last sample
===============================================================================
^+ 139.199.215.251               2   6    17    33  +1577us[+2840us] +/-   68ms
^+ electrode.felixc.at           2   6    17    33  -7996us[-6733us] +/-   92ms
^* 124.65.131.109                4   6    35    31    +12ms[  +13ms] +/-   64ms
^+ ntp8.flashdance.cx            2   6    17    32    -16ms[  -14ms] +/-  131ms

查看客户端配置文件(默认)

[root@centos91 ~]# cat /etc/chrony.conf | grep iburst
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
posted @ 2025-09-04 10:44  youemby  阅读(280)  评论(0)    收藏  举报