linux集群:设置时间同步

配置参考

参考:https://mp.weixin.qq.com/s/7Lg8pgo1K6x1xEl9oLLf2Q

1, 安装chrony服务

  • yum install -y chrony
#=================== timeserver1端================
[root@timeserver1 ~]# grep -v ^# /etc/chrony.conf |grep -v ^$
driftfile /var/lib/chrony/drift
makestep 1.0 3
rtcsync
allow   10.223.0.0/16
local stratum 10
logdir /var/log/chrony

[root@timeserver1 ~]# ip a |grep 192.168
    inet 192.168.56.31/25 brd 192.168.56.127 scope global noprefixroute enp3s0
 
[root@timeserver1 ~]# systemctl restart chronyd


#=================== client端================
[root@client ~]# sed -i  -e '/^pool/server 192.168.56.31 iburst' \
                         -e 's/^pool/#pool/' /etc/chrony.conf

[root@client ~]# grep -v ^# /etc/chrony.conf |grep -v ^$
server 192.168.56.31 iburst
driftfile /var/lib/chrony/drift
makestep 1.0 3
rtcsync
logdir /var/log/chrony

[root@client ~]# systemctl restart chronyd

[root@client ~]# chronyc sources -v
210 Number of sources = 1

  .-- Source mode  '^' = timeserver1, '=' = peer, '#' = local clock.
 / .- Source state '*' = current synced, '+' = combined , '-' = not combined,
| /   '?' = unreachable, 'x' = time may be in error, '~' = time too variable.
||                                                 .- xxxx [ yyyy ] +/- zzzz
||      Reachability register (octal) -.           |  xxxx = adjusted offset,
||      Log2(Polling interval) --.      |          |  yyyy = measured offset,
||                                \     |          |  zzzz = estimated error.
||                                 |    |           \
MS Name/IP address         Stratum Poll Reach LastRx Last sample               
===============================================================================
^* timeserver1                      10   6    17     5    +19us[ +283us] +/-  127us

2, 安装ntpd服务

集群节点: s101[192.168.56.111] --> s102[192.168.56.112] --> s103[192.168.56.113]
设置: s101为时间服务器,其他节点同步s101的时间

  • 1, 所有节点 ,统一 使用 yum 安装ntp
  • 2, 修改/etc/ntpd.conf

a, master 节点: ntpd服务

(默认使用外网的时间服务器,可以连外网时不需要修改配置文件)
当集群不能访问外网: 修改 /etc/ntp.conf

 sed '/^server/s/^/#/' /etc/ntp.conf -i
 
echo "
## 配置自己,充当时间同步服务器
server 127.127.1.0 iburst
fudge  127.127.1.0  stratum 10
" >> /etc/ntp.conf


## 修改时区: 中国.上海
ln -sfT /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
#修改 ntpd启动参数
echo "SYNC_HWCLOCK=yes" >>  /etc/sysconfig/ntpd
#重启ntp, 并设置开机启动:
service ntpd restart 
chkconfig ntpd on

b, slave节点: ntpdate服务

修改 /etc/ntp.conf

echo "
## 配置自己,充当时间同步服务器
server 127.127.1.0  iburst
fudge  127.127.1.0  stratum 10
server   s101
restrict s101       nomodify notrap noquery   
# 允许上层时间服务器主动修改本机时间
" >> /etc/ntp.conf

echo  " 
# master的ip/主机名
s101 " > /etc/ntp/step-tickers

#清空始终漂移位置
 > /var/lib/ntp/drift


## 修改时区: 中国.上海
ln -sfT /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
## 修改 ntpd启动参数
sed -i 's/SYNC_HWCLOCK=no/SYNC_HWCLOCK=yes/' /etc/sysconfig/ntpdate

## 重启ntp, 并设置开机启动:
ntpdate -u s101 #-u 参数:在ntpd 启动时也可以手动同步时间
service ntpdate restart 
chkconfig ntpdate on

c, 状态查看

#查看是否和指定的: 服务器同步(要过一段时间才显示如下,否则显示unsynchronised)
[test@s101 ~]$ ntpstat 
unsynchronised
  time server re-starting
   polling server every 8 s
   
[test@s101 ~]$ ntpstat 
synchronised to NTP server (185.255.55.20) at stratum 3 
   time correct to within 1084 ms
   polling server every 64 s
   
[test@s101 ~]$ ntpq -pn
     remote           refid      st t when poll reach   delay   offset  jitter
==============================================================================
*162.159.200.123 10.20.12.67      3 u   42  256  135  159.502  -78.917  28.187


############  字段说明  #############
[root@s101  ~]# man ntpq
....
+----------------------+--------------------------------------------------------------------------------------------------------+
   |     Variable         |        Description                                                                                     |
   +----------------------+--------------------------------------------------------------------------------------------------------+
   |     [tally]          |        single-character code indicating current value of the select field of the peer status word      |
   +----------------------+--------------------------------------------------------------------------------------------------------+
   |     remote           |        host name (or IP number) of peer                                                                |
   +----------------------+--------------------------------------------------------------------------------------------------------+
   |     refid            |        association ID or kiss code                                                                     |
   +----------------------+--------------------------------------------------------------------------------------------------------+
   |     st               |        stratum                                                                                         |
   +----------------------+--------------------------------------------------------------------------------------------------------+
   |     t                |        u: unicast, b: broadcast, l: local                                                              |
   +----------------------+--------------------------------------------------------------------------------------------------------+
   |     when             |        sec/min/hr since last received packet                                                           |
   +----------------------+--------------------------------------------------------------------------------------------------------+
   |     poll             |        poll interval (log2 s)                                                                          |
   +----------------------+--------------------------------------------------------------------------------------------------------+
   |     reach            |        reach shift register (octal)                                                                    |
   +----------------------+--------------------------------------------------------------------------------------------------------+
   |     delay            |        roundtrip delay                                                                                 |
   +----------------------+--------------------------------------------------------------------------------------------------------+
   |     offset           |        offset                                                                                          |
   +----------------------+--------------------------------------------------------------------------------------------------------+
   |     jitter           |        jitter                                                                                          |
   +----------------------+--------------------------------------------------------------------------------------------------------+

3,使用命令date -s

配置crond定时计划,执行以下脚本

for((i=101;i<=103;i++))
do
        #获取当前机器时间: 年月日, 时分秒
        datestr=` date '+%y-%m-%d %H:%M:%S'  `  # 简写为 '+%F %T'

        #同步其他主机时间
        echo "ssh s$i  date -s--------$datestr $datestr2"
        ssh s$i date -s $datestr
        ssh s$i  hwclock -w    #写入bios
        echo
done
  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
Linux 集群中,由于多台服务器之间的时间差异可能会影响到一些应用程序的正常运行,因此需要进行时间同步。下面介绍两种常用的时间同步方案: 1. NTP 时间同步方案 NTP(Network Time Protocol)是一种用于同步网络中计算机时间的协议,通过在服务器上运行 NTP 服务器,其他计算机可以通过 NTP 协议与服务器进行时间同步。 具体操作步骤如下: 1)在一个节点上安装 NTP 服务器软件: ``` yum install ntp ``` 2)编辑配置文件 /etc/ntp.conf,将服务器地址修改为本地服务器的 IP 地址: ``` server 127.127.1.0 fudge 127.127.1.0 stratum 10 server 0.cn.pool.ntp.org ``` 3)启动 NTP 服务并设置开机自启动: ``` systemctl start ntpd systemctl enable ntpd ``` 4)在其他节点上安装 NTP 客户端软件: ``` yum install ntpdate ``` 5)使用以下命令进行时间同步: ``` ntpdate -u 服务器IP地址 ``` 2. Chrony 时间同步方案 Chrony 是一种轻量级的时间同步软件,相比 NTP 更加精准和稳定。Chrony 的主要优势在于它可以快速适应网络延迟的变化,从而更加准确地进行时间同步。 具体操作步骤如下: 1)在所有节点上安装 Chrony 软件: ``` yum install chrony ``` 2)编辑配置文件 /etc/chrony.conf,将服务器地址修改为本地服务器的 IP 地址: ``` server 127.127.1.0 fudge 127.127.1.0 stratum 10 server 0.cn.pool.ntp.org ``` 3)启动 Chrony 服务并设置开机自启动: ``` systemctl start chronyd systemctl enable chronyd ``` 通过以上两种方案的操作,就可以完成 Linux 集群中的时间同步。需要注意的是,在使用 NTP 或 Chrony 进行时间同步时,要确保所有节点的时间都正确,并且所有节点都能够访问到 NTP 或 Chrony 服务器。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

根哥的博客

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值