(九)、时间同步-ntp

中小型规模网站集群架构:时间同步-ntp

: Ago linux运维群:https://hankbook.cn


前言:

时间服务器部署,这样可以防止网络带来的阻塞和延迟导致服务器的时间不一致,时间的不一致容易导致各种错误。假如监控时间报错了,你老大会怎么想

部署:

1.安装ntp

yum -y install ntp
[root@nfs01 ~]# rpm -qa ntp
ntp-4.2.6p5-10.el6.centos.2.x86_64

2.配置文件

[root@m01 ~]# grep -Ev "#|^$" /etc/ntp.conf 
driftfile /var/lib/ntp/drift
restrict default nomodify
restrict 127.0.0.1 
restrict -6 ::1
server ntp1.aliyun.com
server time.nist.gov
includefile /etc/ntp/crypto/pw
keys /etc/ntp/keys

步骤,此处略low,可以使用直推推送配置文件的方法

sed -i 's#server 3.centos.pool.ntp.org iburst##g' /etc/ntp.conf
sed -i 's#server 2.centos.pool.ntp.org iburst##g' /etc/ntp.conf
sed -i 's#server 1.centos.pool.ntp.org iburst##g' /etc/ntp.conf
sed -i 's#server 0.centos.pool.ntp.org iburst##g' /etc/ntp.conf
sed -i 's#restrict default kod nomodify notrap nopeer noquery##g' /etc/ntp.conf
sed -i 's#restrict -6 default kod nomodify notrap nopeer noquery##g' /etc/ntp.conf
echo -e "server ntp1.aliyun.com\nserver time.nist.gov\nrestrict default nomodify" >> /etc/ntp.conf

3.启动并查看

/etc/init.d/ntpd start
ntpq -p

剧本

---
- hosts: 172.16.1.61
  tasks:
  - name: install ntp
    yum:
      name: ntp
      state: installed
  - name: update cfg
    shell: sed -i 's#server 3.centos.pool.ntp.org iburst##g' /etc/ntp.conf;sed -i 's#server 2.centos.pool.ntp.org iburst##g' /etc/ntp.conf;sed -i 's#server 1.centos.pool.ntp.org iburst##g' /etc/ntp.conf;sed -i 's#server 0.centos.pool.ntp.org iburst##g' /etc/ntp.conf;sed -i 's#restrict default kod nomodify notrap nopeer noquery##g' /etc/ntp.conf;sed -i 's#restrict -6 default kod nomodify notrap nopeer noquery##g /etc/ntp.conf';echo -e "server ntp1.aliyun.com\nserver time.nist.gov\nrestrict default nomodify" >> /etc/ntp.conf
  - name: start ntp
    service:
      name: ntpd
      state: started
      enabled: yes
- hosts: aige
  tasks:
  - name: cron ntp
    cron:
      name: sync time
      minute: "*/5"
      job: '/usr/sbin/ntpdate 172.16.1.61 >/dev/null 2>&1'
posted @ 2017-04-04 10:28  汉克书  阅读(442)  评论(0编辑  收藏  举报