导语
大数据有不少分布式组件,是需要各个节点保持时间同步的,比如hbase,cdh管理平台等。本文重点介绍centos7环境下,ntp服务的搭建。
核心过程
需要做时间同步的各个节点,选取一台作为主节点,其他从节点都向这台主节点做时间同步,而主节点向网络标准时间做同步。
详细步骤
各节点安装ntp,ntpdate服务
执行如下两个命令
[root@cdh02 ~]$ yum install -y ntp
# Loaded plugins: fastestmirror
# Loading mirror speeds from cached hostfile
# * base: mirrors.163.com
# * extras: mirrors.cqu.edu.cn
# * updates: mirrors.cqu.edu.cn
# base # | 3.6 kB 00:00:00
# extras # | 2.9 kB 00:00:00
# updates # | 2.9 kB 00:00:00
# Package ntp-4.2.6p5-29.el7.centos.x86_64 already installed and latest version
# Nothing to do
[root@cdh02 ~]$ yum install -y ntpdate
# Loaded plugins: fastestmirror
# Loading mirror speeds from cached hostfile
# * base: mirrors.163.com
# * extras: mirrors.cqu.edu.cn
# * updates: mirrors.cqu.edu.cn
# Package ntpdate-4.2.6p5-29.el7.centos.x86_64 already installed and latest version
# Nothing to do
如果已经安装过,则会出现如上的提示,如未安装,则执行在线安装
编辑ntp配置
主节点: vi /etc/ntp.conf
# Use public servers from the pool.ntp.org project.
# Please consider joining the pool (http://www.pool.ntp.org/join.html).
# 1 同步网络时间地址
server 0.asia.pool.ntp.org
server 1.asia.pool.ntp.org
server 2.asia.pool.ntp.org
server 3.asia.pool.ntp.org
# 2 当外部时间不可用,则使用当前硬件时间
server 127.127.1.0 iburst local lock
# 3 允许哪些网段的机器,来同步时间
restrict 192.168.60.0 mask 255.255.255.0 nomodify notrap
注:1位置为修改原有的配置,2,3位置的新增加的,其余原有文件配置保持不动
各从节点; vi /etc/ntp.conf
# Use public servers from the pool.ntp.org project.
# Please consider joining the pool (http://www.pool.ntp.org/join.html).
# 客户端节点配置同步时间的主节点,表示向主节点cdh01同步时间
server cdh01 prefer
server 127.127.1.0
fudge 127.127.1.0 stratum 10
各从节点 vi /etc/ntp/step-tickers
#配置上主机的ntp主服务器,去掉原来的远程ntp服务器
cdh01
各节点启动ntp服务
# 启动ntp服务 centos7 环境下需要加sudo,否则关机后重启有可能启动失败
[root@cdh01 ~]$ sudo systemctl restart ntpd
#查看ntp服务状态
[root@cdh01 ~]$ systemctl status ntpd
● ntpd.service - Network Time Service
Loaded: loaded (/usr/lib/systemd/system/ntpd.service; enabled; vendor preset: disabled)
Active: active (running) since Sun 2020-05-03 12:07:12 CST; 48s ago
Process: 25722 ExecStart=/usr/sbin/ntpd -u ntp:ntp $OPTIONS (code=exited, status=0/SUCCESS)
Main PID: 25723 (ntpd)
CGroup: /system.slice/ntpd.service
└─25723 /usr/sbin/ntpd -u ntp:ntp -g
May 03 12:07:12 cdh01 ntpd[25723]: Listen and drop on 0 v4wildcard 0.0.0.0 UDP 123
May 03 12:07:12 cdh01 ntpd[25723]: Listen and drop on 1 v6wildcard :: UDP 123
May 03 12:07:12 cdh01 ntpd[25723]: Listen normally on 2 lo 127.0.0.1 UDP 123
May 03 12:07:12 cdh01 ntpd[25723]: Listen normally on 3 ens33 192.168.60.111 UDP 123
May 03 12:07:12 cdh01 ntpd[25723]: Listen normally on 4 lo ::1 UDP 123
May 03 12:07:12 cdh01 ntpd[25723]: Listen normally on 5 ens33 fe80::76b6:c7f8:7df3:13c1 UDP 123
May 03 12:07:12 cdh01 ntpd[25723]: Listening on routing socket on fd #22 for interface updates
May 03 12:07:13 cdh01 ntpd[25723]: 0.0.0.0 c016 06 restart
May 03 12:07:13 cdh01 ntpd[25723]: 0.0.0.0 c012 02 freq_set kernel -6.567 PPM
May 03 12:07:17 cdh01 ntpd[25723]: 0.0.0.0 c515 05 clock_sync
启动服务后,可以看到ntp服务状态为running
各从节点检查是否能向主节点同步时间
[root@cdh02 ~]$ ntpdate -u cdh01
# 3 May 12:11:22 ntpdate[13376]: adjust time server 192.168.60.111 offset 0.002199 sec
各节点设置开机启动ntp服务
# 执行命令,开机启动ntp服务
[root@cdh01 ~]$ sudo systemctl enable ntpd
# Created symlink from /etc/systemd/system/multi-user.target.wants/ntpd.service to /usr/lib/systemd/system/ntpd.service.
各从节点配置crontab,定时同步主节点时间
[root@cdh03 ~]$ crontab -e
# follow time from master host
0 * * * * ntpdate -u cdh01
# write system time to hardware time
1 * * * * hwclock --systohc
注:同步时间后,还需把同步到的时间写入机器硬件时间,命令为 hwclock --systohc