虚拟机时间同步的实现

当我们在搭建集群的时候,要实现时间同步
现在以三台虚拟机为例子,
hadoop01,hadoop02,hadoop03
现在让hadoop01来充当服务器,
让hadoop02,hadoop03来充当客户端

首先对hadoop01服务器来进行配置
1.需要先安装ntp服务和ntpdate工具
yum -y install ntp ntpdate
2.改配置文件,配置文件在/etc/ntp.conf
配置文化有几个位置需要改动
1)授权下述网段上所有的机器允许从ntp服务器上查询和同步时间

restrict 192.168.10.0 mask 255.255.255.0 nomodify notrap

192.168.10.0 ----》一定要查询自己的网关
2)#增加下述几个时间服务器列表,可以多些几个,万一有一个服务器挂掉的时候,他会自动找下一个

 server 0.asia.pool.ntp.org iburst
 server 1.asia.pool.ntp.org iburst 
 server 2.asia.pool.ntp.org iburst
 server 3.asia.pool.ntp.org iburst

3)这两行内容表示当外部时间不可用时,使用本地时间

 server 127.127.1.0 iburst
 fudge 127.127.1.0 stratum 10

4)下述四行表示允许上层服务器修改本机时间

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

3.设置开机自启动服务

systemctl enable ntpd
 使客户端工具ntpdate工具生效
 systemctl enable ntpdate
 我们可以检查一下状态
  systemctl is-enabled ntpd
  如果结果是enable就对了

4.启用ntp服务
1)我们可以查看ntpd进程

 ps -ef | grep ntpd
     如果进程存在,说明服务启动成功

5.设置防火墙关闭

   systemctl disable firewalld
     可以查看状态关闭没关闭(但是一般会有延迟)
     systemctl status firewalld

6.设置bios和系统时间同步

hwclock -w
  1. 测试
ntpstat
返回类似结果
 synchronised to NTP server (211.233.84.186) at stratum 3 time correct to within 292 ms 
 说明了本地已经和时间服务器同步了 

如果unsynchronised polling server every 8 s 这个问题
我遇到的问题是网关写错了,还可以把配置文件加一句话
interface listen 192.168.1.21 (这个ip就是充当服务器的ip)

二.配置客户端
配置客户端有两种方法
一种是让hadoop02 又有客户端又有服务器端,这样可以让hadoop02的客户端请求hadoop02的服务端,在由hadoop02的服务端请求hadoop01这个服务器:
这种写法好处是:hadoop02的ntpd服务始终运行着,我们不用再手动执行命令或者写定时器了,而且ntpd服务是慢慢改变时间的,很柔和

第一种:
配置:
1.需要先安装ntp服务和ntpdate工具

yum -y install ntp ntpdate

2.设置bios和系统时间同步

 hwclock -w

3.写配置文件

echo "server 192.168.10.200" >/etc/ntp.conf
server 192.168.10.200  这个就是hadoop01的ip地址
  1. 重启服务以使配置生效,之后大概要等10分钟左右,才会同步成功
systemctl enable ntpd
systemctl restart ntpd

第二种:是写个定时器,定时实现时间同步
1.配置
1.需要先安装ntp服务和ntpdate工具

yum -y install ntp ntpdate

2.写定时器
写定时器要注意的两点是
a.执行命令要写绝对路径
b.要写上系统和bios时间同步

* * * * * (/usr/sbin/ntpdate -u 192.168.10.200 && /sbin/hwclock -w) &> /var/log/ntpdate.log

猜你喜欢

转载自blog.csdn.net/m0_51327764/article/details/109435010