下面内容引自于  https://blog.csdn.net/hellboy0621/article/details/81903091

ntp分为服务端和客户端

一、服务端配置

下面执行命令目录无要求,有要求会提示

1、查看服务器是否安装ntp,系统默认安装ntpdate;

[root@k8s-01 /]# rpm -qa | grep ntp
fontpackages-filesystem-1.44-8.el7.noarch
python-ntplib-0.3.2-1.el7.noarch
ntpdate-4.2.6p5-25.el7.centos.x86_64

2、安装ntp ntpdate,其中ntpdate默认安装,可以只安装ntp;

[root@k8s-01 /]# yum install ntp ntpdate -y

3、查看是否已安装完成,与第2步对比

[root@k8s-01 /]# rpm -qa | grep ntp
ntpdate-4.2.6p5-28.el7.centos.x86_64
fontpackages-filesystem-1.44-8.el7.noarch
ntp-4.2.6p5-28.el7.centos.x86_64
python-ntplib-0.3.2-1.el7.noarch

4、查看ntp服务器状态,两条命令效果一样

[root@k8s-01 /]# systemctl status ntpd
● ntpd.service - Network Time Service
   Loaded: loaded (/usr/lib/systemd/system/ntpd.service; disabled; vendor preset: disabled)
   Active: inactive (dead)

5、修改配置文件,使该NTP服务器在不联网的情况下,使用本服务器的时间作为同步时间

[root@k8s-01 /]# vim /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

在下面再添加一行,不能是127.0.0.1,切记

# 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
server 127.127.1.0 iburst

6、启动ntp服务并再次查看状态

[root@k8s-01 /]# systemctl start ntpd
[root@k8s-01 /]# systemctl status ntpd
● ntpd.service - Network Time Service
   Loaded: loaded (/usr/lib/systemd/system/ntpd.service; disabled; vendor preset: disabled)
   Active: active (running) since Thu 2019-07-18 15:07:51 CST; 6s ago
  Process: 44578 ExecStart=/usr/sbin/ntpd -u ntp:ntp $OPTIONS (code=exited, status=0/SUCCESS)
 Main PID: 44579 (ntpd)
   Memory: 820.0K
   CGroup: /system.slice/ntpd.service
           └─44579 /usr/sbin/ntpd -u ntp:ntp -g

Jul 18 15:07:51 k8s-01 ntpd[44579]: Listen normally on 2 lo 127.0.0.1 UDP 123
Jul 18 15:07:51 k8s-01 ntpd[44579]: Listen normally on 5 docker0 172.17.0.1 UDP 123
Jul 18 15:07:51 k8s-01 ntpd[44579]: Listen normally on 6 lo ::1 UDP 123
Jul 18 15:07:51 k8s-01 ntpd[44579]: Listen normally on 7 eth0 fe80::e5d0:2b73:64b:3000 UDP 123
Jul 18 15:07:51 k8s-01 ntpd[44579]: Listening on routing socket on fd #24 for interface updates
Jul 18 15:07:51 k8s-01 ntpd[44579]: 0.0.0.0 c016 06 restart
Jul 18 15:07:51 k8s-01 ntpd[44579]: 0.0.0.0 c012 02 freq_set kernel 0.000 PPM
Jul 18 15:07:51 k8s-01 ntpd[44579]: 0.0.0.0 c011 01 freq_not_set

7、查看是否同步

[root@k8s-01 /]# ntpq -p
     remote           refid      st t when poll reach   delay   offset  jitter
==============================================================================
 localhost       .INIT.          16 l    -  512    0    0.000    0.000   0.000

8、设置开机启动

[root@k8s-01 /]# systemctl enable ntpd
Created symlink from /etc/systemd/system/multi-user.target.wants/ntpd.service to /usr/lib/systemd/system/ntpd.service.

9、防火墙设置参考开头的引用链接

二、客户端配置

前4步与服务端配置一致

5、修改配置文件,将刚刚搭建好的NTP服务器作为客户端上游时间服务器

[root@k8s-02 /]# vim /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

修改如下

# 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
server 192.168.0.11 iburst
restrict 192.168.0.11 nomodify notrap noquery
192.168.0.11 为服务端的ip,根据实际情况替换

a:注释掉其他上游时间服务器

b:配置上游时间服务器为本地的ntpd Server服务器

c:配置允许上游时间服务器主动修改本机的时间

6、与本地ntpd Server同步一下

[root@k8s-02 /]# ntpdate -u 192.168.0.11
18 Jul 16:06:07 ntpdate[44317]: adjust time server 192.168.0.11 offset -0.000050 sec

7、启动ntp服务

[root@k8s-02 /]# systemctl start ntpd

 8、设置开机启动

[root@k8s-02 /]# systemctl enable ntpd
Created symlink from /etc/systemd/system/multi-user.target.wants/ntpd.service to /usr/lib/systemd/system/ntpd.service.

9、查看状态

[root@k8s-02 /]# ntpq -p
     remote           refid      st t when poll reach   delay   offset  jitter
==============================================================================
*192.168.0.11   LOCAL(0)         6 u    6   64  377    0.195   -0.002   0.021

 

10、防火墙设置参考开头的引用链接

结束