概述:
Hadoop对集群中各个机器的时间同步要求比较高,要求各个机器的系统时间不能相差太多,不然会造成很多问题。可以配置集群中各个机器和互联网的时间服务器进行时间同步,但是在实际生产环境中,集群中大部分服务器是不能连接外网的,这时候可以在内网搭建一个自己的时间服务器(NTP服务器),集群的各个机器与这个时间服务器进行时间同步。
NTP 网络时间协议用来同步网络上不同主机的系统时间。你管理的所有主机都可以和一个指定的被称为 NTP 服务器的时间服务器同步它们的时间。而另一方面,一个 NTP 服务器会将它的时间和任意公共 NTP 服务器,或者你选定的服务器同步。由 NTP 管理的所有系统时钟都会同步精确到毫秒级。
怎么同步时间:NTP在linux下有两种时钟同步方式,分别为直接同步和平滑同步:
1、直接同步
使用ntpdate命令进行同步,直接进行时间变更。如果服务器上存在一个12点运行的任务,当前服务器时间是13点,但标准时间时11点,使用此命令可能会造成任务重复执行。因此使用ntpdate同步可能会引发风险,因此该命令也多用于配置时钟同步服务时第一次同步时间时使用。
2、 平滑同步
使用ntpd进行时钟同步,可以保证一个时间不经历两次,它每次同步时间的偏移量不会太陡,是慢慢来的,这正因为这样,ntpd平滑同步可能耗费的时间比较长。
同步谁的时间:客户机需要同步时间可以有两种方式
方式1: 直接在防火墙上放行此客户机同外部时间服务器同步,此时直接安装
yum -y install ntp
配置即同配置局域网ntp服务器一样。直接启动
systemctl enable ntpd
systemctl start ntpd
此时ntp会以守护进程的方式去运行,且会自动同配置文件中的外网NTP服务器同步时间,不需人工干预。
方式2:和内网NTP服务器去同步
但需添加计划任务 让服务器周期性去同内网NTP同步
vi /var/spool/cron/root(或crontab -e)
在每天的0点10分、8点10分、16点10分与时间同步服务器进行同步并写入BIOS
10 0,8,16 * * * /usr/sbin/ntpdate 192.168.100.10 (服务器的ip为192.168.100.10,也可以输入服务器的hostname)
如果同步不正常,可以加输出日志1.txt或看系统日志者看/var/mail/root系统邮件
0 */8 * * * /usr/sbin/ntpdate 10.3.5.5 >>/tmp/1.txt; /sbin/hwclock -w
编辑cat /etc/sysconfig/ntpdate 中的SYNC_HWCLOCK=yes 自动同步到硬件时间。
时间服务器搭建
虚拟机需关闭防火墙 :https://blog.youkuaiyun.com/Henry_Lin_Wind/article/details/89469340
1、配置NTP服务器
现有三台主机,设置第三台 hadoop3 为NTF服务器,其他机器和这台机器进行同步。
Hostname | IP |
hadoop1 | 192.168.100.10 |
hadoop2 | 192.168.100.11 |
hadoop3 | 192.168.100.12 |
2、 检查ntp服务是否已经安装
[root@hadoop3 ~]# sudo rpm -qa | grep ntp
fontpackages-filesystem-1.44-8.el7.noarch
python-ntplib-0.3.2-1.el7.noarch
ntpdate-4.2.6p5-28.el7.centos.x86_64
ntp-4.2.6p5-28.el7.centos.x86_64
显示已经安装过了ntp程序,其中
ntpdate-4.2.6p5-28.el7.centos.x86_64
是用来和某台服务器进行同步的,
ntp-4.2.6p5-28.el7.centos.x86_64
是用来提供时间同步服务的。
如果未安装, 可以如下执行安装命令安装NTF服务器。
yum -y install ntp
3、查看配置文件ntp.conf
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.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
#broadcast 192.168.1.255 autokey # broadcast server
#broadcastclient # broadcast client
#broadcast 224.0.1.1 autokey # multicast server
#multicastclient 224.0.1.1 # multicast client
#manycastserver 239.255.254.254 # manycast server
#manycastclient 239.255.254.254 autokey # manycast client
# Enable public key cryptography.
#crypto
includefile /etc/ntp/crypto/pw
# Key file containing the keys and key identifiers used when operating
# with symmetric key cryptography.
keys /etc/ntp/keys
# Specify the key identifiers which are trusted.
#trustedkey 4 8 42
# Specify the key identifier to use with the ntpdc utility.
#requestkey 8
# Specify the key identifier to use with the ntpq utility.
#controlkey 8
# Enable writing of statistics records.
#statistics clockstats cryptostats loopstats peerstats
# Disable the monitoring facility to prevent amplification attacks using ntpdc
# monlist command when default restrict does not include the noquery flag. See
# CVE-2013-5211 for more details.
# Note: Monitoring will not be disabled with the limited restriction flag.
disable monitor
参数说明
a、 restrice:用于对访问ntp的客户端的限制
restrict参数 | |
kod | 使用kod技术防范“kiss of death”攻击 |
ignore | 拒绝任何NTP连接 |
nomodify | 用户端不能使用ntpc,ntpq修改时间服务器参数,可以进行网络校时 |
noquery | 用户端不能使用ntpc,ntpq查询时间服务器参数,可以进行网络校时 |
notrap | 不提供远程日志功能 |
notrust | 拒绝没有认证的客户端 |
restrict default nomodify notrap nopeer noquery #默认拒绝所有访问 只可以同步时间
restrict 127.0.0.1 #允许本机的一切操作
restrict ::1 #同步时间.但是拒绝让他们修改服务器上的时间
restrict 192.168.1.0 mask 255.255.255.0 nomodify notrap #添加该网段所有主机可访问,但不可以修改服务器时间参数
b、 server : server用于设定ntp同步时间的外网时间服务器。如果是不连接互联网,则注释掉
4、修改配置文件ntp.conf
a 修改 restrict
去掉注释,修改为 restrict 192.168.1.0 mask 255.255.255.0 nomodify notrap
b 新增:当外部时间不可用时,使用本地时间
server 127.127.1.0
fudge 127.127.1.0 stratum 10
c 修改配置文件ntpd,自动同步到硬件时间
sudo vim /etc/sysconfig/ntpd
添加配置: SYNC_HWCLOCK=yes
5、 启动服务器的NTP服务
sudo chkconfig ntpd on
主机启动后,NTP服务都会自动启动。
6、客户机的同步设置
[root@hadoop1 ~]# vim /var/spool/cron/root
添加配置: */5 * * * * /usr/sbin/ntpdate hadoop3 (也可以直接输入服务器的IP)# 每隔5分钟请求同步
或者 0 0 12 * * * /usr/sbin/ntpdate hadoop3 (也可以直接输入服务器的IP)# 每天的 0点和12点整同步
7、测试同步
a 查看系统当前时间
[root@hadoop1 ~]# date "+%Y-%m-%d %H:%M:%S"
b 修改时间
sudo date -s '2016-01-01 00:00:00'
c 等待5分钟后查看系统时间 ,查看是否自动同步时间
[root@hadoop1 ~]# date "+%Y-%m-%d %H:%M:%S"
2019-04-24 15:53:52
您在 /var/spool/mail/root 中有新邮件
d 在系统日志中查看 /var/mail/root 系统邮件
vim /var/spool/mail/root