ntp时间同步服务

前言

一个集群要求做到稳定统一,所以时间的统一很重要,这也方便运维管理,如果时间相差很大,会出现很多问题,设置服务器之间时间同步,为了避免很多问题的发生!

一、环境准备

  • 1.几台服务器的角色:
IP 主机名 用途
172.16.1.6 web01 时间同步服务器
172.16.1.31 nfs 客户端
172.16.1.41 backup 客户端
172.16.1.61 m01 客户端
  • 2.远程为所有服务器安装ntp服务
    上一篇中介绍可以使用ansible管理
ansible all -a 'yum install ntp -y'
ansible all -a 'rpm -qa ntp'
ansible all -a 'systemctl start ntpd'
ansible all -a 'systemctl enable ntpd'
ansible all -a 'timedatectl set-timezone Asia/Shanghai' #设置为同步中国时区

以上几条命令依次执行,当然也可以自己拓展使用ansible playbook来执行

二、配置时间同步服务器

在web服务器中设置,添加以下几行:

restrict 172.16.1.0 mask 255.255.255.0
表示允许此网段来同步此服务器
server 127.127.1.0
server设置127.127.1.0为其自身

vim /etc/ntp.conf
# 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
restrict 172.16.1.0 mask 255.255.255.0
# Hosts on local network are less restricted.
#restrict 192.168.1.0 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 127.127.1.0
....

三、客户端修改配置

客户端配置的机器分别为nfs,backup,分别修改其配置文件,最好使用ansible

vim /etc/ntp.conf
# 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 172.16.1.7
...

配置完成重启ntp服务systemctl restart ntpd
使用命令ntpdate -u 172.16.1.7便可完成与web时间服务器同步时间

四、使用定时任务同步时间

此定时任务分别写入三条需要同步时间的服务器内,可以尝试使用ansible playbook进行分发

crontab -e
#Ansible: sync time 
*/5 * * * * /sbin/ntpdate -u 172.16.1.7 >/dev/null 2>&1

五、测试

在nfs使用修改下时间,使用date -s 要修改为的时间 ,查看时间date,等待定时任务执行后时间是否完成同步
定时任务要求每五分钟执行一次,为更快体现测试结果,可修改执行时间为每分钟!

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

友情链接更多精彩内容