ntpd 运行但未同步的虚拟机

Jér*_*ôme 5 kvm ntpd

TL; DR

VM使用KVM,时间不同步。暂停 2 分钟后,它会保持永久的 2 分钟间隔。使用不同的网络配置设置另一个 VM 显示网络配置阻止 ntp 工作。修复此网络问题已不合时宜。

但是,没有网络问题的新虚拟机在恢复后也不同步。相同的测试:暂停 2 分钟。使用正确同步的机器检查日期差异。2 分钟的延迟是永久性的。

这似乎是一个常见问题,关于如何保持 VM 同步以及同时使用 NTP 和 kvm-clock 存在争议。我找到了很多参考资料,但没有答案。

我有一个 Debian VMntpd正在运行但没有更正时间。例如,在暂停/恢复之后,我得到了 2 分钟的永久偏移。

/etc/ntp.conf 是默认的或接近默认的,没什么特别的:

# /etc/ntp.conf, configuration for ntpd; see ntp.conf(5) for help

driftfile /var/lib/ntp/ntp.drift


# Enable this if you want statistics to be logged.
#statsdir /var/log/ntpstats/

statistics loopstats peerstats clockstats
filegen loopstats file loopstats type day enable
filegen peerstats file peerstats type day enable
filegen clockstats file clockstats type day enable


# You do need to talk to an NTP server or two (or three).
#server ntp.your-provider.example

# pool.ntp.org maps to about 1000 low-stratum NTP servers.  Your server will
# pick a different set every time it starts up.  Please consider joining the
# pool: <http://www.pool.ntp.org/join.html>
server 0.debian.pool.ntp.org iburst
server 1.debian.pool.ntp.org iburst
server 2.debian.pool.ntp.org iburst
server 3.debian.pool.ntp.org iburst


# Access control configuration; see /usr/share/doc/ntp-doc/html/accopt.html for
# details.  The web page <http://support.ntp.org/bin/view/Support/AccessRestrictions>
# might also be helpful.
#
# Note that "restrict" applies to both servers and clients, so a configuration
# that might be intended to block requests from certain clients could also end
# up blocking replies from your own upstream servers.

# By default, exchange time with everybody, but don't allow configuration.
restrict -4 default kod notrap nomodify nopeer noquery
restrict -6 default kod notrap nomodify nopeer noquery

# Local users may interrogate the ntp server more closely.
restrict 127.0.0.1
restrict ::1

# Clients from this (example!) subnet have unlimited access, but only if
# cryptographically authenticated.
#restrict 192.168.123.0 mask 255.255.255.0 notrust


# If you want to provide time to your local subnet, change the next line.
# (Again, the address is an example only.)
#broadcast 192.168.123.255

# If you want to listen to time broadcasts on your local subnet, de-comment the
# next lines.  Please do this only if you trust everybody on the network!
#disable auth
#broadcastclient
Run Code Online (Sandbox Code Playgroud)

ntpq 似乎报告了一个问题:

# cat ntpq -pn
     remote           refid      st t when poll reach   delay   offset  jitter
==============================================================================
 37.187.7.160    .INIT.          16 u    - 1024    0    0.000    0.000   0.000
 195.154.211.37  .INIT.          16 u    - 1024    0    0.000    0.000   0.000
 195.154.216.44  .INIT.          16 u    - 1024    0    0.000    0.000   0.000
 95.81.173.155   .INIT.          16 u    - 1024    0    0.000    0.000   0.000
Run Code Online (Sandbox Code Playgroud)

但是,我不是 netcat 向导,但 UDP 端口 123 上的 AFAIU 传出流量会通过:

# nc -vvzu 37.187.7.160 123
mail.lafkor.de [37.187.7.160] 123 (ntp) open
 sent 0, rcvd 0
Run Code Online (Sandbox Code Playgroud)

这个测试是否足以排除防火墙问题?

主机(也是 Debian 机器)具有相同的 NTP 配置并且同步工作正常。两台机器的网络配置不同,这就是为什么我认为这可能是网络问题。

我可以运行任何其他有用的测试吗?

我认为该tinker panic 0参数与此处无关,因为它旨在强制更新巨大的差距,而不是 2 分钟的差距。无论如何,AFAIU,它会在时间偏移的情况下影响行为,但它不会解决ntpq -pn只返回零的问题。

FWIW,受此问题启发的其他测试输出:

# ntpq
ntpq> pe
     remote           refid      st t when poll reach   delay   offset  jitter
==============================================================================
 mail.lafkor.de  .INIT.          16 u    - 1024    0    0.000    0.000   0.000
 atoll.tropicdre .INIT.          16 u    - 1024    0    0.000    0.000   0.000
 oods.roflcopter .INIT.          16 u    - 1024    0    0.000    0.000   0.000
 ntp-3.arkena.ne .INIT.          16 u    - 1024    0    0.000    0.000   0.000
ntpq> as

ind assid status  conf reach auth condition  last_event cnt
===========================================================
  1 21025  8011   yes    no  none    reject    mobilize  1
  2 21026  8011   yes    no  none    reject    mobilize  1
  3 21027  8011   yes    no  none    reject    mobilize  1
  4 21028  8011   yes    no  none    reject    mobilize  1
ntpq> rv
associd=0 status=c012 leap_alarm, sync_unspec, 1 event, freq_set,
version="ntpd 4.2.6p5@1.2349-o Fri Apr 10 19:04:04 UTC 2015 (1)",
processor="x86_64", system="Linux/3.16.0-4-amd64", leap=11, stratum=16,
precision=-23, rootdelay=0.000, rootdisp=6683.055, refid=INIT,
reftime=00000000.00000000  Mon, Jan  1 1900  0:09:21.000,
clock=d9b51587.b7a1085f  Tue, Sep 29 2015 15:49:59.717, peer=0, tc=3,
mintc=3, offset=0.000, frequency=-0.125, sys_jitter=0.000,
clk_jitter=0.000, clk_wander=0.000
ntpq> rv 21025
associd=21025 status=8011 conf, sel_reject, 1 event, mobilize,
srcadr=mail.lafkor.de, srcport=123, dstadr=147.210.157.185, dstport=123,
leap=11, stratum=16, precision=-23, rootdelay=0.000, rootdisp=0.000,
refid=INIT, reftime=00000000.00000000  Mon, Jan  1 1900  0:09:21.000,
rec=00000000.00000000  Mon, Jan  1 1900  0:09:21.000, reach=000,
unreach=1137, hmode=3, pmode=0, hpoll=10, ppoll=10, headway=0,
flash=1600 peer_stratum, peer_dist, peer_unreach, keyid=0, offset=0.000,
delay=0.000, dispersion=15937.500, jitter=0.000, xleave=0.167,
filtdelay=     0.00    0.00    0.00    0.00    0.00    0.00    0.00    0.00,
filtoffset=    0.00    0.00    0.00    0.00    0.00    0.00    0.00    0.00,
filtdisp=   16000.0 16000.0 16000.0 16000.0 16000.0 16000.0 16000.0 16000.0
Run Code Online (Sandbox Code Playgroud)

tcpdump / ntpdate 测试

在 NTP 同步正常工作的机器上,我启动tcpdump udp port ntp并重新启动时ntpd,我看到这种输出:

# tcpdump udp port ntp
tcpdump: verbose output suppressed, use -v or -vv for full protocol decode
listening on eth0, link-type EN10MB (Ethernet), capture size 262144 bytes
17:31:33.719166 IP 10.0.2.15.ntp > spica.beduzar.fr.ntp: NTPv4, Client, length 48
17:31:33.736804 IP spica.beduzar.fr.ntp > 10.0.2.15.ntp: NTPv4, Server, length 48
17:31:35.973551 IP 10.0.2.15.ntp > ntp.tuxfamily.net.ntp: NTPv4, Client, length 48
17:31:35.992671 IP ntp.tuxfamily.net.ntp > 10.0.2.15.ntp: NTPv4, Server, length 48
[...]
Run Code Online (Sandbox Code Playgroud)

在我遇到问题的机器上,重新启动时我根本看不到任何输出ntpd(没有请求,没有回复)。我不应该至少看到请求吗?

在好机器上:

# ntpdate 0.debian.pool.ntp.org
29 Sep 17:24:49 ntpdate[700]: adjust time server 193.55.167.1 offset -0.005196 sec
Run Code Online (Sandbox Code Playgroud)

在坏机器上:

# ntpdate 0.debian.pool.ntp.org
29 Sep 17:43:18 ntpdate[3180]: no server suitable for synchronization found
Run Code Online (Sandbox Code Playgroud)

用另一个虚拟机测试

我们设置了另一个具有相同 NTP 配置但另一个网络配置的 VM。

这一结果tcpdumpntpdate是否正确,并ntpq -pn回报良好的效果。显然,网络配置确实是故障虚拟机上的问题。

但是,新 VM 也不同步。如果我暂停它,让它有大约 100 秒的延迟,它就不会同步(我的意思是几分钟后,差距仍然是相同的秒数)。但是,当重新启动 ntpd 时,它会立即同步。

我似乎有两个问题:

  • 第一个虚拟机上的网络配置

  • ntp 不会在两者上同步(除非重新启动)

Jér*_*ôme 2

问题解决了。

网络问题

VM 存在网络问题,导致 ntpd 无法成功。它有两个eth接口,带有网关的接口通过我们不直接管理的路由器。虽然我的测试没有显示这一点,但我猜想一些 UDP 帧被阻止了。我们使用另一个网络配置设置了另一个虚拟机,并ntpq产生了更好的结果。

最终,我们更改了ntp配置,以便主机在本地广播时间并且所有虚拟机在其上同步。更有意义并最大限度地减少公共ntp服务器上的负载。

ntpd几分钟后立即设置时钟

在测试过程中可能误导我的一件事是 ntpd 不会立即同步。我认为它会立即检测到间隙,然后修改时钟速度,以便时钟逐渐加入源时钟。事实上,我们注意到(除非ntpd重新启动)时钟在几分钟内没有变化,然后突然它被设置为看起来立即的样子。与此同时,ntpq输出中最右边的列显示同步正在进行。

这种ntpd行为可能解释了为什么我认为ntpd即使有效也不起作用。我只是等待的时间不够长,而且我不明白ntpq输出。