Loading

CentOS7 下配置多台服务器时间同步

现有三台虚拟机,如果虚拟机之间实间差距太大就会导致一些服务异常

192.168.139.121 master
192.168.139.122 slave1
192.168.139.123 slave2

安装 NTP

先查看ntp是否已经安装:rpm -qa | grep ntp
如果没有 yum -y install ntp

安装完成后设置 ntp 开机启动并启动 ntp,如下:

systemctl enable ntpd

systemctl start ntpd

主服务器均能连接外网

所有机器都能够访问外网的话,我们可以使用 ntpdate 来进行时间同步
前提是我们服务器上面有 ntp ,如果没有的话可以使用 yum 安装

同步网络时间

其中 ntp 一种网络时间协议,ntpdate 是时间同步软件的命令, ntp.api.bz 是个稳定的ntp时间同步服务器集群。

ntpdate -u ntp.api.bz
# 或
ntpdate -u us.pool.ntp.org

编辑定时任务

当然你可以使用linux系统的定时任务来设置每隔多长时间同步一次,使用 linux 系统下使用crontab -e 可以编辑任务。
这里我是用 crontab -e 添加了一个定时每分钟同步时间的任务 ,然后将执行日志写到了 /tmp/dateSync.log下面

crontab -e

# 将以下内容写入 >> 将日志写入到 /tmp/dateSync.log
*/1 * * * * /usr/sbin/ntpdate -u us.pool.ntp.org

crontab 规则在线生成:https://www.bejson.com/othertools/cron/
image

NTP 服务器

我们把这台能连接外网的服务器当作时间服务器,整个集群服务器向该时间服务器同步时间,然后咱们内网的这台时间服务器向公网时间服务器同步时间,然后内网时间服务器不能联网,则自己设置时间,所有的集群服务器以它的时间为准。

image

配置 NTP 服务器

使用 master 作为 NTP 服务器

  1. 修改 NTP 配置文件 vim /etc/ntp.conf
# For more information about this file, see the man pages
# ntp.conf(5), ntp_acc(5), ntp_auth(5), ntp_clock(5), ntp_misc(5), ntp_mon(5).

driftfile /var/lib/ntp/drift

# Permit time synchronization with our time source, but do not
# permit the source to query or modify the service on this system.
restrict default nomodify notrap nopeer noquery

# Permit all access over the loopback interface.  This could
# be tightened as well, but to do so would effect some of
# the administrative functions.
restrict 127.0.0.1 
restrict ::1

# Hosts on local network are less restricted.
#restrict 192.168.139.2 mask 255.255.255.0 nomodify notrap

# Use public servers from the pool.ntp.org project.
# Please consider joining the pool (http://www.pool.ntp.org/join.html).
#server 0.centos.pool.ntp.org iburst
#server 1.centos.pool.ntp.org iburst
#server 2.centos.pool.ntp.org iburst
#server 3.centos.pool.ntp.org iburst

server 0.cn.pool.ntp.org
server 1.cn.pool.ntp.org
server 2.cn.pool.ntp.org
server 3.cn.pool.ntp.org

restrict 0.cn.pool.ntp.org nomodify notrap noquery
restrict 1.cn.pool.ntp.org nomodify notrap noquery
restrict 2.cn.pool.ntp.org nomodify notrap noquery
restrict 3.cn.pool.ntp.org nomodify notrap noquery

server 127.0.0.1 # local clock
Fudge 127.0.0.1 stratum 10

  1. 重启 NTP 服务器
systemctl restart ntpd
systemctl enable ntpd
# 检查是否启动成功
systemctl status ntpd

配置 NTP 客户端

  1. 安装 NTP
yum -y install ntp
  1. 修改 /etc/ntp.conf 配置文件
    注释以下内容
 # server 0.centos.pool.ntp.org iburst
 # server 1.centos.pool.ntp.org iburst
 # server 2.centos.pool.ntp.org iburst
 # server 3.centos.pool.ntp.org iburst

新增以下内容

server 192.168.139.121
restrict 192.168.139.121 nomodify notrap noquery

server 127.127.1.0 # local clock
Fudge 127.127.1.0 stratum 10

修改后重启 ntpd 服务

systemctl restart ntpd
systemctl enable ntpd
# 检查是否启动成功
systemctl status ntpd

使用ntpq -p 查看网络中的NTP服务器,同时显示客户端和每个服务器的关系

使用ntpstat 命令查看时间同步状态,这个一般需要5-10分钟后才能成功连接和同步。所以,服务器启动后需要稍等下:
刚启动的时候,执行ntpstat一般会提示以下信息:

unsynchronised
time server re-starting
polling server every 64 s

注意

  1. 防火墙是否关闭
  2. 123 端口是否开发(如果防火墙已关闭请忽略)
  3. 关闭 Selinux
posted @ 2021-10-09 21:02  白日醒梦  阅读(744)  评论(0编辑  收藏  举报