java ntp服务器ip地址_NTP服务器时间同步

一、简要说明

二、安装步骤

三、配置文件

四、常用命令

五、注意事项

六、运行截图

七、参考资料

一、简要说明

搭建Kubernetes环境,需要几台、几十台机器配合运作,许多集群服务比如Etcd等都依赖系统的时间,如果机器的系统时间不一致,可能会出现各种问题。因此有必要统一集群内所有服务器的系统时间。

NTP(Network Time Protocol)可以很方便的解决服务器之间的时间同步问题,Ubuntu系统下NTP安装也很方便,经过测试,在Ubuntu 16.04环境下,直接安装NTP服务,使用Ubuntu系统自带的pool ntp.ubuntu.com 时间服务器地址池,就可以实现时间同步。用户也可以选择NTP官方网站推荐的pool pool.ntp.org地址池,或者选择中国区的pool cn.pool.ntp.org地址池,都会生效的。

用户还可以选择集群中的1台作为主授时服务器(NTP 服务器角色),通过配置文件中的pool 地址池与上层的服务器同步时间,集群内所有其他机器(NTP客户端角色)的NTP配置文件中,使用Server xx.xx.xx.xx形式,明确指向主授时服务器IP地址,也可以实现为集群提供统一的时间服务。如果考虑高可靠性,还可以将多台服务器作为集群的授时服务器。

也可以到www.ntp.org网站中查找中国区的服务器地址,直接在NTP配置文件中使用这些地址,比如 server  xx.xx.xx.xx 。

下面的例子中,我们使用到的服务器信息:

服务器名   IP地址                 角色

rancher2   192.168.3.220   NTP 服务器

node221    192.168.3.221   NTP 客户端

二、安装步骤

#在集群的所有机器上安装NTP相关程序即可

sudo apt install ntp ntpdate ntpstat

三、配置文件

#3.1 NTP 主授时服务器(192.168.3.220)配置文件/etc/ntp.conf去除注释后的内容:

driftfile /var/lib/ntp/ntp.drift

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

server 1.networktime.org iburst

server 2.networktime.org iburst

server ntp.synet.edu.cn iburst

server ntp.neu6.edu.cn iburst

server ntp.gwadar.cn iburst

server ntp.neu.edu.cn iburst

restrict -4 default kod notrap nomodify nopeer limited

restrict -6 default kod notrap nomodify nopeer limited

restrict 127.0.0.1

restrict ::1

restrict source notrap nomodify noquery

#3.2 NTP 客户端服务器(192.168.3.221)配置文件/etc/ntp.conf去除注释后的内容:

driftfile /var/lib/ntp/ntp.drift

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

server 192.168.3.220 iburst

restrict -4 default kod notrap nomodify nopeer limited

restrict -6 default kod notrap nomodify nopeer limited

restrict 127.0.0.1

restrict ::1

restrict source notrap nomodify noquery

#3.3 其他地址池和授时服务器地址参考

#3.3.1===Ubuntu 16.04系统默认的地址池===

pool ntp.ubuntu.com

pool 0.ubuntu.pool.ntp.org

pool 1.ubuntu.pool.ntp.org

pool 2.ubuntu.pool.ntp.org

pool 3.ubuntu.pool.ntp.org

#3.3.2===www.pool.ntp.org官网上推荐的地址池===

pool pool.ntp.org

pool 0.pool.ntp.org

pool 1.pool.ntp.org

pool 2.pool.ntp.org

pool 3.pool.ntp.org

#3.3.3===中国区地址池===

pool cn.pool.ntp.org

pool 0.cn.pool.ntp.org

pool 1.cn.pool.ntp.org

pool 2.cn.pool.ntp.org

pool 3.cn.pool.ntp.org

#3.3.4===www.ntp.org官网上提供的中国区服务器地址===

server 1.networktime.org

server 2.networktime.org

server ntp.synet.edu.cn

server ntp.neu6.edu.cn

server ntp.gwadar.cn

server ntp.neu.edu.cn

四、常用命令

#停止NTP服务

sudo service ntp stop

#只查询、不更新本机系统时间

sudo ntpdate -q pool.ntp.org

#使用debug(-d)模式查询详细更新信息

sudo ntpdate -d pool.ntp.org

#直接与pool.ntp.org中的服务器同步本机系统时间

sudo ntpdate pool.ntp.org

#查询NTP连接上层授时服务器的状态

nptq -p

#启动NTP服务

sudo service ntp start

#查询ntp运行状态

sudo ntpstat

#查看系统时间

date

#设置系统时间的日期为2018年07月09日08点44分30秒sudo date -s "2018/07/09 08:44:30"

#查看硬件时间

sudo hwclock  --show

#设置硬件时间

sudo hwclock --set --date="07/09/18 14:55:30"

#使用硬件时间同步系统时间

sudo hwclock --hctosys

#使用系统时间同步硬件时间

sudo hwclock --systohc

五、注意事项

1、NTP服务和ntpdate命令不可同时使用。使用ntpdate之前,一定要先停止NTP服务。启用NTP服务之前,应先使用ntpdate命令 同步一下服务器时间,或者直接使用date命令设置系统时间,以免服务器时间相差太大,NTP服务不起作用。

2、注意互联网授时服务器地址是否可用?正式使用之前,可以使用ntpdate 验证一下,是否可以同步时间。

3、除了Linux自带的防火墙要打开UDP123端口外,网络出口防火墙也要打开UDP123端口!这样运行ntpdate同步时间时,才不会出现“no server suitable for synchronization found”错误。

六、运行截图

图01-Rancher2主授时服务器配置文件-与上层的中国区的服务器地址进行同步

b465a1654f5f98455618cde6c269e757.png

图02-Rancher2主授时服务器同步结果

8f0cf8961473597d0af71fab4126ac88.png

图03-NTP客户端服务器node221,配置文件指向主授时服务器

33b949d5abd610801cdd955bcca77c48.png

图04-NTP客户端服务器node221时间同步结果

bcbad8162cf280db849ddcfe7b8b474e.png

图05-使用Ubuntu系统自带的NTP地址池pool,能够正常同步时间

5a20f72cf23bf4df6d348c737f299219.png

图06-使用pool.ntp.org地址池也是能正常同步时间的

2d21acfd55b1f45c9133a03171df25a4.png

图07-使用中国区的地址池cn.pool.ntp.org 也是可以同步时间的

b1dbcfabb995f6d7e580251b84ab1127.png

图08-使用nslookup查询cn.pool.ntp.org地址池域名的地址信息

536048dce91cb125992dd43c9ae5b56e.png

图09-NTP相关配置文件位置

50321a35b51f6df6781b225bc41b152c.png

七、参考资料

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值