这次笔者需要面对的环境对时间的同步有比较高的要求, 而虚拟化的环境中时间是比较容易出问题的, 您可以参考上一篇博文为什么Domain controller上的time synchronization非常重要? 笔者的环境里, 经过亲自观察, 如果没有时间同步, 虚拟机与标准时间差距在短短的两个小时之内竟然就偏差了近半个小时!
按照VMware KB 1003063的说法, ESXi 5.5只要在vCenter的vSphere里配好NTP client就可以了.
Note: For ESX 3.5 and above, NTP can be configured from vSphere Client and no longer requires manual configuration.
然而, 这不是真的.
问题描述
===============================================
先介绍笔者的环境:
- 一台安装了Windows 2012的实体机, 它并不是Domain Controller.
- 三台ESXi 5.5.
- 一台安装了vCenter 5.5的Windows 2008 R2虚拟机, 运行在上面的一台ESXi上.
按照文档Configuring Windows Time service to use an internal hardware clock的步骤,我将Windows 2012实体机配置成了使用内部硬件时钟的NTP server. 注意, 微软的文章如何在 Windows Server 中配置权威时间服务器是不work的.
按照VMware KB 1003736的步骤, 将三台ESXi 5.5 配置成了与Windows 实体机NTP server 同步时间.
结果如图:
NTP Server的时间
一台ESXi的时间
懂行的你一定会说, ESXi上只存UTC的时间, 你在vCenter vSphere中看到的时间是convert过了的. 那我就补充一句, 我的vSphere所在的机器的时区已经被设置为与NTP server一样的时区, 而且时间是一样的.
其结论就是, 这个ESXi的NTP有问题, 时间没有同步.
问题排查
==============================================
首先, 需要verify我们的NTP server是否是好的.
于是笔者挑选了一台安装在其中一台ESXi上的虚拟机. 注意, 默认情况下只要安装了VMware Tools, 那么即使你没有开启VMware tools的timesync, 虚拟机也会在某些情况下和其宿主去sync时间的, 详见Configure Time Synchronization Between Guest and Host Operating Systems.
这台ESXi的虚拟机的时间是错误的, 因为ESXi的时间是错误的. –_-||
笔者运行了如下的命令, 使这台虚拟机的时间正确了.
net start w32tm
w32tm /config /manualpeerlist:10.110.69.124 /syncfromflags:manual /reliable:yes /update
w32tm /resync /rediscover
由此, 我们证明了NTP server是正常工作的. 笔者把NTP Server的时间设置的与标准时间相差了几分钟, 所以, 可以避免混淆.
下一步, 笔者在vSphere里尝试了界面能操作的所有相关东西, 都没能让NTP client正常工作.
笔者按照文章Troubleshooting NTP on ESX and ESXi 4.x / 5.x (1005092)所讲的去排查, 没有结果. 直到, 笔者找到这篇文章.
详细步骤不介绍了, 大家可以自己去看, 我只简单说一下:
1. 在/etc/ntp.conf的最后一行添加一行tos maxdist 30.
2. 修改/etc/likewise/lsassd.conf文件, 取消掉#sync-system-time的注释, 显式地设置sync-system-time = yes
3. 重启lsassd服务和ntpd服务.
困扰了笔者好几天的问题终于解决了!
另外, 文章ntpd - Network Time Protocol (NTP) daemon有如下的介绍:
Most operating systems and hardware of today incorporate a time-of-year (TOY) chip to maintain the time during periods when the power is off. When the machine is booted, the chip is used to initialize the operating system time. After the machine has synchronized to a NTP server, the operating system corrects the chip from time to time. In case there is no TOY chip or for some reason its time is more than 1000s from the server time, ntpd assumes something must be terribly wrong and the only reliable action is for the operator to intervene and set the clock by hand. This causes ntpd to exit with a panic message to the system log. The -g option overrides this check and the clock will be set to the server time regardless of the chip time. However, and to protect against broken hardware, such as when the CMOS battery fails or the clock counter becomes defective, once the clock has been set, an error greater than 1000s will causentpd to exit anyway.
注意粗体的部分, 说如果NTP client的时间与server的时间相差超过1000秒, 那么NTPD就会假设发生了严重的错误, 从而只能手动进行时间调整. 笔者在测试时, 时间差超过了1000秒, 也成功同步了.
命令列表
==============================
cp /etc/ntp.conf /etc/ntp.conf.bak
vi /etc/ntp.conf
“tos maxdist 30”
cp /etc/likewise/lsassd.conf /etc/likewise/lsassd.conf.bak
chmod +w /etc/likewise/lsassd.conf
vi /etc/likewise/lsassd.conf
“sync-system-time = yes”
/sbin/auto-backup.sh
./etc/init.d/lsassd restart
./etc/init.d/ntpd restart
参考资料
==============================
Configuring Windows Time service to use an internal hardware clock
http://www.denningelectronics.com/wp-content/uploads/2010/04/TimeService.pdf
VMware ESXi 5.1 will not sync time with Windows 2008 R2 NTP Domain Controller
Guide to configure NTP on ESX servers (1003063)
Configuring the Windows Time Service
http://blogs.msmvps.com/acefekay/2014/04/26/configuring-the-windows-time-service/
Verifying time synchronization across an ESX/ESXi host environment(1003736)
Troubleshooting NTP on ESX and ESXi 4.x / 5.x (1005092)
W32tm
http://technet.microsoft.com/en-us/library/w32tm.aspx
Windows Time Service Tools and Settings
http://technet.microsoft.com/en-us/library/cc773263(v=ws.10).aspx