云计算、AI、云原生、大数据等一站式技术学习平台

网站首页 > 教程文章 正文

NTP时间同步,NTP服务配置流程,docker部署配置NTP服务

jxf315 2024-12-10 16:19:53 教程文章 51 ℃

一、整体流程拓扑

二、Docker自建NTP服务流程

  1. 下载ntp镜像
docker pull cturra/ntp:latest
  1. 创建及启动容器

注意几项配置

  • 配置公网NTP地址,例如:下面使用的是阿里云的公网NTP服务地址
  • 配置ntp服务端口,注意:是udp端口
sudo docker run -e TZ=Asia/Shanghai -d --restart=always \
-p 123:123/udp \
--name ntp \
--env NTP_SERVERS=ntp1.aliyun.com,ntp2.aliyun.com,ntp3.aliyun.com,ntp4.aliyun.com \
--env LOG_LEVEL=0 \
-v /etc/timezone:/etc/timezone:ro \
-v /etc/localtime:/etc/localtime:ro \
--log-opt max-size=10m \
--log-opt max-file=1 \
cturra/ntp:latest
  1. 验证NTP服务状态
  • 查看ntp服务启动日志:docker logs -f ntp
[root@drccentosserver01 ~]# docker logs -f ntp
2022-11-03T07:30:21Z chronyd version 4.1 starting (+CMDMON +NTP +REFCLOCK +RTC +PRIVDROP -SCFILTER +SIGND +ASYNCDNS +NTS +SECHASH +IPV6 -DEBUG)
2022-11-03T07:30:21Z Disabled control of system clock
2022-11-03T07:30:21Z Could not read valid frequency and skew from driftfile /var/lib/chrony/chrony.drift
2022-11-03T07:30:25Z Selected source 120.25.115.20 (ntp1.aliyun.com)
2022-11-03T07:30:27Z Selected source 203.107.6.88 (ntp2.aliyun.com)
  • 验证每个ntp源的配置状态:docker exec ntp chronyc sources
[root@drccentosserver01 ~]# docker exec ntp chronyc sources
MS Name/IP address         Stratum Poll Reach LastRx Last sample               
===============================================================================
^+ 120.25.115.20                 2  10   353   33m  -2895us[-3307us] +/-   22ms
^* 203.107.6.88                  2   9   377   476   +519us[ +520us] +/-   14ms
  • 查看配置的每个ntp源的收集测量的统计信息:docker exec ntp chronyc sourcestats
[root@drccentosserver01 ~]# docker exec ntp chronyc sourcestats
Name/IP Address            NP  NR  Span  Frequency  Freq Skew  Offset  Std Dev
==============================================================================
120.25.115.20              28  16  105m     -0.027      0.929   -970us  1910us
203.107.6.88               20  12   94m     +0.008      0.535   +560us   966us

三、应用服务器配置

  1. 安装ntpdate

在线安装:yum install -y ntpdate

离线安装:

  • 下载安装包
  • 使用rpm命令安装

例如:

X86架构:
wget http://mirror.centos.org/centos/7/os/x86_64/Packages/ntpdate-4.2.6p5-29.el7.centos.2.x86_64.rpm
rpm -ivh ntpdate-4.2.6p5-29.el7.centos.2.x86_64.rpm

arm架构:
wget http://mirror.centos.org/altarch/7/os/aarch64/Packages/ntpdate-4.2.6p5-29.el7.centos.2.aarch64.rpm
rpm -ivh ntpdate-4.2.6p5-29.el7.centos.2.aarch64.rpm
  1. 验证自建NTP服务器

使用如下命令验证:

ntpdate -q <DOCKER_HOST_IP>

例如,自建的NTP服务器的IP地址是 10.9.21.12

如果看到如下进行,可能时钟信息还没有同步,需要再等一段时间后查询
[root@drccentosserver02 ~]# ntpdate -q 10.9.21.12
server 10.13.13.9, stratum 16, offset 0.005689, delay 0.02837
11 Dec 09:47:53 ntpdate[26030]: no server suitable for synchronization found


如果时钟同步了,则显示如下信息
[root@drccentosserver02 ~]# ntpdate -q 10.9.21.12
server 10.9.21.12, stratum 3, offset 0.000961, delay 0.02625
 3 Nov 15:33:29 ntpdate[9094]: adjust time server 10.9.21.12 offset 0.000961 sec
  1. 编写定期同步脚本

使用 "crond服务" 管理定期同步任务(crond是linux下用来周期性地执行某种任务或等待处理某些事件的一个守护进程

  • 查看服务状态:systemctl status crond
  • 重启服务:systemctl restart crond
  • 编辑crontab文件内容:crontab -e
  • 查看crontab文件内容:crontab -l
  • 删除crontab文件内容:crontab -r (会清空所有的,切记不要删除。可以通过vim界面调整)

步骤:

  1. 输入contab -e 编写脚本内容,例如:
*/5 * * * * /usr/sbin/ntpdate 10.9.21.12
  1. 重启crond服务:systemctl restart crond
  1. 查看日志:tail -f /var/log/cron
查看日志:tail -f /var/log/cron

每隔5分钟执行一次
*/5 * * * * /usr/sbin/ntpdate 10.9.21.12


*/5 * * * * /usr/sbin/ntpdate 10.9.21.12 >> /opt/ntp.log 2>&1

四、安卓设备配置NTP服务器

配置地址即:10.9.21.12

Tags:

最近发表
标签列表