1.1 ntp 集群时间同步服务

1.ntp集群时间同步服务规划

《1.1 ntp 集群时间同步服务》 ntp.jpeg

2.具体操作

注:使用root用户操作。

校正个节点时区

1).查看当前系统时区(东八区),如果时区存在问题,需要校正

[root@hadoop104 ~]# date -R

Wed, 18 Oct 2017 14:39:01 +0800

2).删除当前系统时区

[root@hadoop104 ~]# sudo rm -rf /etc/localtime

3).通过软连接方式,设置本地系统时区

[root@hadoop104 ~]# ln -s /usr/share/zoneinfo/Asia/Shanghai /etc/localtime

ntp-server端(hadoop104)配置

step1: 配置时间服务器前,手动联网同步校正时间

1).查看hadoop104节点的 ntpdate 服务工作状态(接下来需要手动与第三方网络时间服务器同步一次,ntpdate需要处于关闭状态)

[root@hadoop104 ~]# service ntpd status

ntpd (pid 1799) is running…

2).停止当前正在运行的ntpdate服务
[root@hadoop104 ~]# service ntpd stop

[root@hadoop104 ~]# service ntpd status

ntpd is stopped

3).与网络时间服务器手动同步(注:特殊情况下,无法访问 pool.ntp.org 时间服务器,可与 ntp1.aliyun.com 阿里云时间服务器进行同步)

[root@hadoop104 ~]# ntpdate pool.ntp.org

18 Oct 15:18:12 ntpdate[14323]: step time server 123.249.20.211 offset 34837488.962033 sec

[root@hadoop104 ~]# ntpdate pool.ntp.org

18 Oct 16:44:47 ntpdate[7739]: no server suitable for synchronization found

[root@hadoop104 ~]# ntpdate ntp1.aliyun.com

18 Oct 16:46:35 ntpdate[7743]: adjust time server 182.92.12.11 offset 0.001637 sec

4).配置定时任务,定期将系统时间同步给硬件时钟

[root@hadoop104 ~]# crontab -e

 # 每15min将系统时间(系统可以实时与网络同步)同步给硬件时钟

 */15 * * * * /sbin/hwclock -w

5).默认crond 服务随机启动,自动刷新,也可以通过以下方式手动刷新

[root@hadoop104 ~]# service crond restart

step2: ntpd-server节点配置ntp服务

[root@hadoop104 ~]# 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 kod nomodify notrap nopeer noquery
restrict -6 default kod 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 -6 ::1
 
# Hosts on local network are less restricted.
# ---------------------------------------------------------------------------------------
## 释放如下行的注释,运行1~255网段客户端过来同步时间
## nomodify 客户端不能使用ntpc与ntpq两支程式来修改服务器的时间参数
## notrap 不提供trap这个远程时间登录的功能
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:(屏蔽掉以后,hadoop104不能与网络时间同步,但集群内部时间是一致的)
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
# ---------------------------------------------------------------------------------------
# 将hadoop104的本地时钟作为时间供给源,这样,即便它失去网络连接,它也可以继续为网络提供服务;
server 127.127.1.0
fudge 127.127.1.0 stratum 10
# ---------------------------------------------------------------------------------------
step3: ntpd-server节点重启ntp服务

注:
1).前面初始化操作,停止了ntpd服务,而且修改了ntp.conf配置信息,需要重启ntpd服务才能时配置生效;
2).hadoop104 实时与第三方网络时间服务器同时时间同时,定期将系统时间同步给硬件时钟,并且对局域网内的hadoop102~3节点提供时间同步服务;
service ntpd status

ntpd is stopped

[root@hadoop104 share]# service ntpd start

Starting ntpd: [ OK ]

[root@hadoop104 share]# service ntpd status

ntpd (pid 14718) is running…

3).hadoop104节点的ntpd服务设置开启启动
[root@hadoop104 ~]# chkconfig ntpd on
[root@hadoop104 ~]# chkconfig ntpd –list

ntp-consumer端使用crond 周期性同步时间

1).停止 ntpd 服务(关闭自动同步,全部基于crontab,手动与 hadoop104进行同步)
[root@hadoop102 ~]# service ntpd stop
[root@hadoop102 ~]# service ntpd status

ntpd is stopped …

关闭开机启动
[root@hadoop102 ~]# chkconfig ntpd off

2).配置定时任务,手动同步时间(测试阶段设置阶段每1min同步一次,以便检测同步效果,生产环境调整为10min同步一次)
[root@hadoop102 ~]# crontab -e

# 与时间服务器hadoop104同步频率 1(0)min/次 (分 时 日 月 周天)
*/1 * * * * /sbin/ntpdate hadoop104
# (30min/次) 将系统时间同步给硬件
*/30 * * * * /sbin/hwclock -w

3).故意修改hadoop102~103当前系统时间,测试能否通过时间同步,校正过来
[root@hadoop102 ~]# date -s ‘2016-10-10 10:10:10’

等待 … 1min 让crond 生效 …

4).SecureCRT客户端,对当前所有session用户同时发送date命令查看时间同步效果

[root@hadoopxxx ~]# date
《1.1 ntp 集群时间同步服务》 date.jpeg

5).测试通过,hadoop102~3的定时任务,切换回,生产环境

[root@hadoop102 ~]# crontab -e

# 与时间服务器hadoop104同步频率 10min/次
*/10 * * * * /sbin/ntpdate hadoop104
# (30min/次) 将系统时间同步给硬件
*/30 * * * * /sbin/hwclock -w

6).默认crond 服务随机启动,自动刷新,也可以通过以下方式手动刷新
[root@hadoop102 ~]# service crond restart

    原文作者:淘淘浩
    原文地址: https://www.jianshu.com/p/1a095016e9dd
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞