大数据集群之自定义时间同步服务器(NTP)
大数据集群要求各节点的时间是一致的,因此需要用到NTP(时间同步服务)。
一、安装NTP软件
1、操作平台
[root@Hexindai-C12-120 ~]# cat /etc/centos-release
CentOS Linux release 7.6.1810 (Core)
[root@Hexindai-C12-120 ~]#
2、检查NTP服务是否安装
[root@Hexindai-C12-120 ~]# rpm -qa|grep ntp #如果没有输出,则表示没有安装NTP服务
3、使用root权限安装NTP软件
[root@Hexindai-C12-120 ~]# yum -y install ntp Loaded plugins: fastestmirror Loading mirror speeds from cached hostfile * base: mirrors.aliyun.com * epel: mirrors.yun-idc.com * extras: mirrors.aliyun.com * updates: mirrors.aliyun.com Resolving Dependencies --> Running transaction check ---> Package ntp.x86_64 0:4.2.6p5-28.el7.centos will be installed --> Processing Dependency: ntpdate = 4.2.6p5-28.el7.centos for package: ntp-4.2.6p5-28.el7.centos.x86_64 --> Processing Dependency: libopts.so.25()(64bit) for package: ntp-4.2.6p5-28.el7.centos.x86_64 --> Running transaction check ---> Package autogen-libopts.x86_64 0:5.18-5.el7 will be installed ---> Package ntpdate.x86_64 0:4.2.6p5-28.el7.centos will be installed --> Finished Dependency Resolution Dependencies Resolved ===================================================================================================================================================================================================== Package Arch Version Repository Size ===================================================================================================================================================================================================== Installing: ntp x86_64 4.2.6p5-28.el7.centos base 549 k Installing for dependencies: autogen-libopts x86_64 5.18-5.el7 base 66 k ntpdate x86_64 4.2.6p5-28.el7.centos base 86 k Transaction Summary ===================================================================================================================================================================================================== Install 1 Package (+2 Dependent packages) Total download size: 701 k Installed size: 1.6 M Downloading packages: (1/3): autogen-libopts-5.18-5.el7.x86_64.rpm | 66 kB 00:00:00 (2/3): ntpdate-4.2.6p5-28.el7.centos.x86_64.rpm | 86 kB 00:00:00 (3/3): ntp-4.2.6p5-28.el7.centos.x86_64.rpm | 549 kB 00:00:00 ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- Total 2.9 MB/s | 701 kB 00:00:00 Running transaction check Running transaction test Transaction test succeeded Running transaction Installing : autogen-libopts-5.18-5.el7.x86_64 1/3 Installing : ntpdate-4.2.6p5-28.el7.centos.x86_64 2/3 Installing : ntp-4.2.6p5-28.el7.centos.x86_64 3/3 Verifying : ntpdate-4.2.6p5-28.el7.centos.x86_64 1/3 Verifying : autogen-libopts-5.18-5.el7.x86_64 2/3 Verifying : ntp-4.2.6p5-28.el7.centos.x86_64 3/3 Installed: ntp.x86_64 0:4.2.6p5-28.el7.centos Dependency Installed: autogen-libopts.x86_64 0:5.18-5.el7 ntpdate.x86_64 0:4.2.6p5-28.el7.centos Complete! [root@Hexindai-C12-120 ~]#
4、检查是否安装成功
[root@Hexindai-C12-120 ~]# rpm -qa|grep ntp ntp-4.2.6p5-28.el7.centos.x86_64 ntpdate-4.2.6p5-28.el7.centos.x86_64 [root@Hexindai-C12-120 ~]#
二、配置NTP服务
1、设置本地网络上的主机不受限制
[root@Hexindai-C12-120 ~]# grep \#restrict /etc/ntp.conf #查看修改前内容 #restrict 192.168.1.0 mask 255.255.255.0 nomodify notrap [root@Hexindai-C12-120 ~]# [root@Hexindai-C12-120 ~]# sed -i \'s@\#restrict@restrict@\' /etc/ntp.conf #编辑配置文件,将注释去掉,表示启用授权功能 [root@Hexindai-C12-120 ~]# grep 192.168.1.0 /etc/ntp.conf #查看修改后的内容 restrict 192.168.1.0 mask 255.255.255.0 nomodify notrap [root@Hexindai-C12-120 ~]# [root@Hexindai-C12-120 ~]# sed -i \'s#192.168.1.0#172.20.102.0#\' /etc/ntp.conf #授权只有172.20.102.0段的服务器可以来同步时间
2、注释默认的时间服务器(不用公共的服务器)
[root@Hexindai-C12-120 ~]# grep ^server /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 [root@Hexindai-C12-120 ~]# [root@Hexindai-C12-120 ~]# sudo sed -i \'s@^server@#server@\' /etc/ntp.conf #编辑配置文件,将官方的配置注释掉 [root@Hexindai-C12-120 ~]# grep ^#server /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 [root@Hexindai-C12-120 ~]#
3、设置自定义时间服务器(添加默认的内部时钟数据,使用它为局域网服务器节点提供服务)
[root@Hexindai-C12-120 ~]# echo "#Add by wangruopeng" >> /etc/ntp.conf [root@Hexindai-C12-120 ~]# echo "server 127.127.1.0" >> /etc/ntp.conf #将本地时间作为时间同步源 [root@Hexindai-C12-120 ~]# echo "fudge 127.127.1.0 stratum 10" >> /etc/ntp.conf #指定间隔更新时间,这里指定10分钟,stratrum(层级)的值不能太大,如果要向上级NTP更新可以设成2 [root@Hexindai-C12-120 ~]# tail -3 /etc/ntp.conf #Add by wangruopeng server 127.127.1.0 fudge 127.127.1.0 stratum 10 [root@Hexindai-C12-120 ~]#
4、修改/etc/sysconfig/ntpd文件
[root@Hexindai-C12-120 ~]# cat /etc/sysconfig/ntpd # Command line options for ntpd OPTIONS="-g" [root@Hexindai-C12-120 ~]# echo SYNC_HWCLOCK=yes >> /etc/sysconfig/ntpd #让硬件时间和系统时间一起同步 [root@Hexindai-C12-120 ~]# cat /etc/sysconfig/ntpd # Command line options for ntpd OPTIONS="-g" SYNC_HWCLOCK=yes [root@Hexindai-C12-120 ~]#
5、启动ntpd
[root@Hexindai-C12-120 ~]# systemctl start ntpd [root@Hexindai-C12-120 ~]# systemctl enable ntpd Created symlink from /etc/systemd/system/multi-user.target.wants/ntpd.service to /usr/lib/systemd/system/ntpd.service. [root@Hexindai-C12-120 ~]# systemctl status ntpd ● ntpd.service - Network Time Service Loaded: loaded (/usr/lib/systemd/system/ntpd.service; enabled; vendor preset: disabled) Active: active (running) since Mon 2019-07-01 17:01:15 CST; 20s ago Main PID: 14630 (ntpd) CGroup: /system.slice/ntpd.service └─14630 /usr/sbin/ntpd -u ntp:ntp -g Jul 01 17:01:15 hexindai-c12-120 ntpd[14630]: ntp_io: estimated max descriptors: 1024, initial socket boundary: 16 Jul 01 17:01:15 hexindai-c12-120 ntpd[14630]: Listen and drop on 0 v4wildcard 0.0.0.0 UDP 123 Jul 01 17:01:15 hexindai-c12-120 ntpd[14630]: Listen and drop on 1 v6wildcard :: UDP 123 Jul 01 17:01:15 hexindai-c12-120 ntpd[14630]: Listen normally on 2 lo 127.0.0.1 UDP 123 Jul 01 17:01:15 hexindai-c12-120 ntpd[14630]: Listen normally on 3 eth0 172.20.102.120 UDP 123 Jul 01 17:01:15 hexindai-c12-120 ntpd[14630]: Listening on routing socket on fd #20 for interface updates Jul 01 17:01:15 hexindai-c12-120 ntpd[14630]: 0.0.0.0 c016 06 restart Jul 01 17:01:15 hexindai-c12-120 ntpd[14630]: 0.0.0.0 c012 02 freq_set kernel 0.000 PPM Jul 01 17:01:15 hexindai-c12-120 ntpd[14630]: 0.0.0.0 c011 01 freq_not_set Jul 01 17:01:16 hexindai-c12-120 ntpd[14630]: 0.0.0.0 c514 04 freq_mode [root@Hexindai-C12-120 ~]#
修改本机时区:(有的系统是美东时区,与北京差12小时,即EDT,需要修改为CST,北京时间)如下操作:
[root@Hexindai-C12-120 ~]# mv /etc/localtime /etc/localtime.bak [root@Hexindai-C12-120 ~]# ln -s /usr/share/zoneinfo/Asia/Shanghai /etc/localtime [root@Hexindai-C12-120 ~]# date Mon Jul 1 16:40:20 CST 2019 [root@Hexindai-C12-120 ~]#
三、集群中其他节点(NTPD客户端)配置
1、安装NTP服务
[root@hexindai-c12-121 ~]# yum -y install ntp Loaded plugins: fastestmirror Loading mirror speeds from cached hostfile * base: mirror.bit.edu.cn * extras: mirror.bit.edu.cn * updates: mirrors.tuna.tsinghua.edu.cn Resolving Dependencies --> Running transaction check ---> Package ntp.x86_64 0:4.2.6p5-28.el7.centos will be installed --> Processing Dependency: ntpdate = 4.2.6p5-28.el7.centos for package: ntp-4.2.6p5-28.el7.centos.x86_64 --> Processing Dependency: libopts.so.25()(64bit) for package: ntp-4.2.6p5-28.el7.centos.x86_64 --> Running transaction check ---> Package autogen-libopts.x86_64 0:5.18-5.el7 will be installed ---> Package ntpdate.x86_64 0:4.2.6p5-28.el7.centos will be installed --> Finished Dependency Resolution Dependencies Resolved ===================================================================================================================================================================================================== Package Arch Version Repository Size ===================================================================================================================================================================================================== Installing: ntp x86_64 4.2.6p5-28.el7.centos base 549 k Installing for dependencies: autogen-libopts x86_64 5.18-5.el7 base 66 k ntpdate x86_64 4.2.6p5-28.el7.centos base 86 k Transaction Summary ===================================================================================================================================================================================================== Install 1 Package (+2 Dependent packages) Total download size: 701 k Installed size: 1.6 M Downloading packages: (1/3): autogen-libopts-5.18-5.el7.x86_64.rpm | 66 kB 00:00:00 (2/3): ntpdate-4.2.6p5-28.el7.centos.x86_64.rpm | 86 kB 00:00:00 (3/3): ntp-4.2.6p5-28.el7.centos.x86_64.rpm | 549 kB 00:00:00 ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- Total 4.5 MB/s | 701 kB 00:00:00 Running transaction check Running transaction test Transaction test succeeded Running transaction Installing : autogen-libopts-5.18-5.el7.x86_64 1/3 Installing : ntpdate-4.2.6p5-28.el7.centos.x86_64 2/3 Installing : ntp-4.2.6p5-28.el7.centos.x86_64 3/3 Verifying : ntpdate-4.2.6p5-28.el7.centos.x86_64 1/3 Verifying : autogen-libopts-5.18-5.el7.x86_64 2/3 Verifying : ntp-4.2.6p5-28.el7.centos.x86_64 3/3 Installed: ntp.x86_64 0:4.2.6p5-28.el7.centos Dependency Installed: autogen-libopts.x86_64 0:5.18-5.el7 ntpdate.x86_64 0:4.2.6p5-28.el7.centos Complete! [root@hexindai-c12-121 ~]#
2、设置其他节点的同步脚本
[root@hexindai-c12-121 ~]# crontab -l
*/1 * * * * /usr/sbin/ntpdate 172.20.102.120
[root@hexindai-c12-121 ~]#
四、验证各节点是否与时间服务器同步
[root@Hexindai-C12-120 ~]# date -s \'2019-07-02\' Tue Jul 2 00:00:00 CST 2019 [root@Hexindai-C12-120 ~]# date Tue Jul 2 00:00:48 CST 2019 [root@hexindai-c12-121 ~]# date Tue Jul 2 00:01:20 CST 2019 [root@hexindai-c12-121 ~]# date Tue Jul 2 00:01:40 CST 2019 [root@Hexindai-C12-120 ~]# date -s \'2019-07-01 17:13\' Mon Jul 1 17:13:00 CST 2019 [root@Hexindai-C12-120 ~]# date Mon Jul 1 17:13:01 CST 2019 [root@Hexindai-C12-120 ~]# [root@hexindai-c12-121 ~]# date Mon Jul 1 17:13:43 CST 2019 [root@hexindai-c12-121 ~]#
五、将系统时间和服务器硬件时间同步
[root@Hexindai-C12-120 ~]# date -s \'2019-07-01 17:13\' Mon Jul 1 17:13:00 CST 2019 [root@Hexindai-C12-120 ~]# date Mon Jul 1 17:13:01 CST 2019 [root@Hexindai-C12-120 ~]# hwclock Mon 01 Jul 2019 05:20:21 PM CST -0.490291 seconds [root@Hexindai-C12-120 ~]# date Mon Jul 1 17:20:32 CST 2019 [root@Hexindai-C12-120 ~]# hwclock -w #同步用 [root@Hexindai-C12-120 ~]# hwclock Mon 01 Jul 2019 05:20:54 PM CST -0.848898 seconds [root@Hexindai-C12-120 ~]#