设为首页 加入收藏

TOP

集群时钟同步必读-NTP和chrony(一)
2023-07-23 13:33:00 】 浏览:45
Tags:时钟同 步必读 -NTP chrony

 

一、  前言

搭建和维护集群环境中时钟同步是非常重要一环。如果集群的时间不统一,例如ceph集群就会报错无法更新数据、CDH集群无法添加客户端等等。目前主流在Linux系统搭建集群用到NTP和chrony软件,本文简单介绍两者的集群搭建。

二、  NTP和chrony区别

根据chrony官网描述,主要区别如下图:(图太大了,截取了一部分)

详细见官网:chrony – NTP 实施的比较

 

三、  环境准备

注意:ntp和chrony无法同时再一台机器运行;请单独安装运行

 

ntp下载地址(Centos7):

http://mirror.centos.org/centos/7/os/x86_64/Packages/ntp-4.2.6p5-29.el7.centos.2.x86_64.rpm

http://mirror.centos.org/centos/7/os/x86_64/Packages/ntpdate-4.2.6p5-29.el7.centos.2.x86_64.rpm

 

chrony下载地址(Centos7):

http://mirror.centos.org/centos/7/os/x86_64/Packages/chrony-3.4-1.el7.x86_64.rpm

 

四、  NTP搭建集群同步时间

1、  更新阿里源、安装ntp|ntpdate

离线环境提前下载离线包

#更新阿里源
cd /etc/yum.repos.d/ 
curl -L -O https://mirrors.aliyun.com/repo/Centos-7.repo && mv ./Centos-7.repo /etc/yum.repos.d/CentOS-Base.repo
sed -i -e '/mirrors.cloud.aliyuncs.com/d' -e '/mirrors.aliyuncs.com/d' /etc/yum.repos.d/CentOS-Base.repo
curl -L -O  http://mirrors.aliyun.com/repo/epel-7.repo && mv ./epel-7.repo /etc/yum.repos.d/epel.repo
yum clean all && yum makecache
yum install -y epel-*

#安装NTP
rpm -qa | grep ntp                    
yum -y remove ntpdate ntp        
yum -y install ntp  ntpdate

#设置时区上海
timedatectl set-timezone Asia/Shanghai    
date
mv /etc/localtime /etc/localtime.bak
ln -s /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
timedatectl set-timezone Asia/Shanghai

2、  离线环境:3台机器时钟同步其中一台

将192.168.1.131作为主节点,其他节点都同步它,允许同步网段设置为192.168.1.0

● 主节点选择192.168.1.131,修改主节点配置文件

#主节点:修改配置文件
mv /etc/ntp.conf /etc/ntp.conf.bakk
cat >>/etc/ntp.conf<<EOF                
driftfile /var/lib/ntp/drift
restrict default nomodify notrap nopeer noquery
restrict 192.168.1.131 nomodify notrap nopeer noquery
restrict 127.0.0.1
restrict ::1
restrict 192.168.1.0 mask 255.255.255.0 nomodify notrap
server 127.127.1.0
fudge 127.127.1.0 stratum 10
includefile /etc/ntp/crypto/pw
keys /etc/ntp/keys
disable monitor
EOF
cat /etc/ntp.conf
#需要把对应restrict IP和restrict网段进行修改即可

● 其他节点作为客户端192.168.1.132/133,修改其他节点配置文件

#其他节点:ntp客户端配置
mv /etc/ntp.conf /etc/ntp.conf.bakk
cat >>/etc/ntp.conf<<EOF    
driftfile /var/lib/ntp/drift
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
server 192.168.1.131   
fudge 192.168.1.131 stratum 10 
includefile /etc/ntp/crypto/pw
keys /etc/ntp/keys
disable monitor 
EOF
cat /etc/ntp.conf
#需要把对应server Fudge IP和restrict网段进行修改即可

● 注意:先启动主节点的NTP服务、再启动其他节点

#主节点执行同步
systemctl restart ntpd
systemctl status ntpd
#systemctl enable ntpd

● 同步硬件时间,检查集群同步状态,关闭chyony

#其他节点执行同步
ntpdate -u 192.168.1.131

#同步硬件时间
sed -i 's#SYNC_HWCLOCK=no#SYNC_HWCLOCK=yes#g' /etc/sysconfig/ntpdate
hwclock -w            
hwclock -r        


#检查是否成功
ntpstat
ntpq -p
timedatectl

#会冲突,需要停止chronyd.service
systemctl stop chronyd.service
systemctl disable chronyd.service

3、  在线环境:3台机器同步外网时钟服务器即可

在线环境同步时间

#手动同步
ntpdate -u ntp.ntsc.ac.cn

#写入配置文件自动同步
mv /etc/ntp.conf /etc/ntp.conf.bakk
cat >>/etc/ntp.conf<<EOF    
driftfile /var/lib/ntp/drift
restrict default nomodify notrap nopeer noquery
restrict 127.0.0.1 
restri
首页 上一页 1 2 下一页 尾页 1/2/2
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇ansible基础使用 下一篇vim 多行编辑模式

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目