一、集群环境:
系统:CentOS7-minimal
集群中的两台主机ip:10.132.226.103/24 10.132.226.104/24
二、CentOS7中时间相关命令timedatectl例子介绍:
1.查看主机中的时间信息:timedatectl
[root@controller ~]# timedatectl
Local time: Tue -- :: CST
Universal time: Mon -- :: UTC
RTC time: Tue -- ::
Time zone: Asia/Shanghai (CST, +)
NTP enabled: yes
NTP synchronized: yes //NTP时间同步开启
RTC in local TZ: yes
DST active: n/a
2.查看可获取的时区:timedatectl list-timezones
[root@controller ~]# timedatectl list-timezones
Africa/Abidjan
Africa/Accra
Africa/Addis_Ababa
........
3.设置本地时区:timedatectl set-timezone Asia/Shanghai
# timedatectl set-timezone Asia/Shanghai
4.设置时间日期等:
[root@controller ~]# timedatectl set-time "2018-07-19 18:08:08"
[root@controller ~]# timedatectl set-time "2018-07-19"
[root@controller ~]# timedatectl set-time "18:08:08"
5.将硬件时钟设置为本地时区:
[root@controller ~]# timedatectl set-local-rtc 1
6.将硬件时钟设置为协调世界时(UTC):
[root@controller ~]# timedatectl set-local-rtc
三、安装配置chrony
1.主节点安装:
[root@controller ~]# yum install -y chrony
2.主节点配置:编辑 /etc/chrony.conf文件
[root@controller ~]# vim /etc/chrony.conf # Use public servers from the pool.ntp.org project.
# Please consider joining the pool (http://www.pool.ntp.org/join.html).
#以下0.centos.pool.ntp.org等为远程时间同步服务器可自行更改
server .centos.pool.ntp.org iburst
server .centos.pool.ntp.org iburst
server .centos.pool.ntp.org iburst
server .centos.pool.ntp.org iburst
#~~~~~~~~~~~省略若干~~~~~~~~#
# Allow NTP client access from local network.
#allow 192.168.0.0/
#修改!!!
#此处添加子节点的IP
allow 10.132.226.104/
#~~~~~~~~~~~省略若干~~~~~~~~#
3.主节点重启NTP服务
[root@controller ~]# systemctl enable chronyd.service
[root@controller ~]# systemctl start chronyd.service
4.子节点安装:
[root@compute1 ~]# yum install -y chrony
5.子节点配置:编辑 /etc/chrony.conf文件
[root@compute1 etc]# vim /etc/chrony.conf # Use public servers from the pool.ntp.org project.
# Please consider joining the pool (http://www.pool.ntp.org/join.html).
#修改 把其他远程同步节点全部注释,并添加主节点ip或者hosts
#server .centos.pool.ntp.org iburst
#server .centos.pool.ntp.org iburst
#server .centos.pool.ntp.org iburst
#server .centos.pool.ntp.org iburst
server controller iburst
四、验证同步服务是否搭建成功:
1.使用命令 chronyc sources
[root@compute1 etc]# chronyc sources
Number of sources =
MS Name/IP address Stratum Poll Reach LastRx Last sample
===============================================================================
^* controller -5232us[-6125us] +/- 47ms
注意:如果controller前为^? 可能是主节点防火墙开启,导致子节点无法进行时间同步。
可以也可以通过timedatectl命令查看:
[root@compute1 etc]# timedatectl
Local time: Mon -- :: CST
Universal time: Mon -- :: UTC
RTC time: Mon -- ::
Time zone: Asia/Shanghai (CST, +)
NTP enabled: yes
NTP synchronized: yes //此处如果为no说明NTP时钟为开启
RTC in local TZ: yes
DST active: n/a
解决方法:关闭主节点防火墙