Introduction to Chrony
Chrony is a versatile implementation of the Network Time Protocol (NTP). It enables synchronization of system clocks with NTP servers, reference clocks (such as GPS receivers), and even menual input via wristwatch and keyboard.
Container Deployment
1) Container Image Overview
By default, this container utilizes CloudFlare's time server (time.cloudflare.com). If you prefer to use one or more alternative NTP servers, you can pass an NTP_SERVERS environment variable to the container.
env=NTP_SERVERS=""
2) Setting Up the NTP Server
container_label='timeserver'
# Update the docker image
docker pull cturra/ntp:latest
# Terminate existing container if present
if (docker ps -a --format '{{.Names}}' | grep ${container_label}); then
# If container exists, stop and remove it
docker stop ${container_label}
docker rm -f ${container_label}
fi
# Launch new container
docker run -i -t -d \
--restart=always \
--name=${container_label} \
-e TZ=Asia/Shanghai \
-p 123:123/udp \
-e NTP_SERVERS="ntp.aliyun.com" \
cturra/ntp:latest
# Optional security enhancements:
# --read-only \
# --detach \
# --tmpfs=/etc/chrony:rw,mode=1750 \
# --tmpfs=/run/chrony:rw,mode=1750 \
# --tmpfs=/var/lib/chrony:rw,mode=1750 \
3) Verifying NTP Container Status
docker exec timeserver chronyc sources
[root@centos91 ~]# docker exec timeserver chronyc sources
MS Name/IP address Stratum Poll Reach LastRx Last sample
===============================================================================
^* 203.107.6.88 2 6 17 38 +7052ms[+7052ms] +/- 35ms
4) Client Configuration
Install chronyc on the client machine:
if [ ! -f "/usr/bin/chronyc" ];
then
yum install -y chrony
fi
Start and enable the chronyd service:
systemctl enable chronyd
systemctl start chronyd
systemctl status chronyd
Check time synchronization status:
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
View default client configuration file:
[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