前言

平时做实验的时候发现集群时间状态不一致,有的集群为了模拟真实环境的状态都是断网的所以我要在内网也搞一个NTP服务器用于时间同步。

环境

虚拟机列表如下:

主机名OSipfirewalldselinux
ntpCentOS7192.168.56.2, 10.0.2.2enableenforcing

使用的网络如下:

  • hostonly (192.168.56.0/24) 用于hostonly网络通讯
  • NAT network (10.0.2.0/24)用于链接外网

设置主机名和ip:

1
2
3
4
5
hostnamectl set-hostname ntp
nmcli con mod enp0s3 ipv4.method manual ipv4.addresses "192.168.56.2/24" connection.autoconnect yes
nmcli con mod enp0s8 ipv4.method manual ipv4.addresses "10.0.2.2/24" ipv4.dns 114.114.114.114 ipv4.gateway 10.0.2.1 connection.autoconnect yes
nmcli con up enp0s3
nmcli con up enp0s8

部署

RHEL7中默认使用chrony作为时间服务器,也支持NTP,需要额外安装。 NTP与chrony不能同时存在,只能用其中一个,并将另一个mask掉。

这篇文章配置的是NTP服务器非默认的chrony服务器。

安装NTP

1
yum -y install ntp 

编辑/etc/ntp.conf配置文件,修改内容如下:

1
2
3
4
5
6
vi /etc/ntp.conf 
restrict 192.168.56.2
# 允许192.168.56.0/24 网络访问这个NTP服务器
restrict 192.168.56.0 mask 255.255.255.0 nomodify notrap
# 设置阿里云的NTP服务器为上游服务器,其他的全部删除
server ntp1.aliyun.com iburst

添加防火墙规则:

1
2
firewall-cmd --add-service=ntp --permanent
firewall-cmd --reload 

启动ntp服务并将其加入到开机启动中:

1
2
3
systemctl start ntpd 
systemctl enable ntpd 
systemctl status ntpd 

验证:

1
2
ntpq -p
date

验证

这里我开了一台新的虚拟机用于验证ntp服务是否工作正常。

安装ntp:

1
yum install ntp -y

配置ntp:

1
2
3
vi /etc/ntp.conf
# 注释掉原有的ntp server,添加这一条:
server 192.168.56.2 iburst

启动ntp服务并将其加入到开机启动中:

1
2
3
systemctl start ntpd 
systemctl enable ntpd 
systemctl status ntpd 

最终验证时间是否正常:

1
2
ntpq -p
date

后记

在设置了NTP服务器之后集群的时间不一致问题就得到了完美解决,好诶🎉~

参考资料