• 通过ansible一键部署集群ntp时间同步


    环境准备

    [root@server ~]# cat /etc/redhat-release 
    CentOS Linux release 7.2.1511 (Core) 

      [root@server ~]# uname -r
      3.10.0-327.el7.x86_64

    [root@server ~]# getenforce 
    Disabled
    [root@server ~]# systemctl status firewalld
    ● firewalld.service - firewalld - dynamic firewall daemon
       Loaded: loaded (/usr/lib/systemd/system/firewalld.service; disabled; vendor preset: enabled)
       Active: inactive (dead)
    [root@server ~]# cat /etc/yum.repos.d/CentOS7-Base-163.repo 
    # CentOS-Base.repo
    #
    # The mirror system uses the connecting IP address of the client and the
    # update status of each mirror to pick mirrors that are updated to and
    # geographically close to the client.  You should use this for CentOS updates
    # unless you are manually picking other mirrors.
    #
    # If the mirrorlist= does not work for you, as a fall back you can try the 
    # remarked out baseurl= line instead.
    #
    #
    [base]
    name=CentOS-$releasever - Base - 163.com
    #mirrorlist=http://mirrorlist.centos.org/?release=$releasever&arch=$basearch&repo=os
    baseurl=http://mirrors.163.com/centos/$releasever/os/$basearch/
    gpgcheck=1
    gpgkey=http://mirrors.163.com/centos/RPM-GPG-KEY-CentOS-7
    
    #released updates
    [updates]
    name=CentOS-$releasever - Updates - 163.com
    #mirrorlist=http://mirrorlist.centos.org/?release=$releasever&arch=$basearch&repo=updates
    baseurl=http://mirrors.163.com/centos/$releasever/updates/$basearch/
    gpgcheck=1
    gpgkey=http://mirrors.163.com/centos/RPM-GPG-KEY-CentOS-7
    
    #additional packages that may be useful
    [extras]
    name=CentOS-$releasever - Extras - 163.com
    #mirrorlist=http://mirrorlist.centos.org/?release=$releasever&arch=$basearch&repo=extras
    baseurl=http://mirrors.163.com/centos/$releasever/extras/$basearch/
    gpgcheck=1
    gpgkey=http://mirrors.163.com/centos/RPM-GPG-KEY-CentOS-7
    
    #additional packages that extend functionality of existing packages
    [centosplus]
    name=CentOS-$releasever - Plus - 163.com
    baseurl=http://mirrors.163.com/centos/$releasever/centosplus/$basearch/
    gpgcheck=1
    enabled=0
    gpgkey=http://mirrors.163.com/centos/RPM-GPG-KEY-CentOS-7

    主机分配

    sever 10.0.0.10 主节点

    其他节点

    client01 10.0.0.11

    client02 10.0.0.12

    client03 10.0.0.13

    client04 10.0.0.14

    设置密钥认证

    主节点

    ssh-keygen -t rsa -P '' -f ~/.ssh/id_rsa 
    cat ~/.ssh/id_rsa.pub >>~/.ssh/authorized_keys
    ssh-copy-id -i ~/.ssh/id_rsa.pub root@10.0.0.10

    所有其他节点

    ssh-keygen -t rsa -P '' -f ~/.ssh/id_rsa
    ssh-copy-id -i ~/.ssh/id_rsa.pub root@10.0.0.10

    在主节点

    scp ~/.ssh/authorized_keys 10.0.0.11:.ssh/
    scp ~/.ssh/authorized_keys 10.0.0.12:.ssh/
    scp ~/.ssh/authorized_keys 10.0.0.13:.ssh/
    scp ~/.ssh/authorized_keys 10.0.0.14:.ssh/

    编写脚本

    #!/bin/bash
    #安装ansible
    yum install ansible –y
    echo "10.0.0.10" >>/etc/ansible/hosts
    echo "[other]" >>/etc/ansible/hosts
    echo -e "10.0.0.11
    10.0.0.12
    10.0.0.13
    10.0.0.14" >>/etc/ansible/hosts
    #批量安装ntp服务
    ansible all -a "yum install -y ntp"
    #批量设置 主机 时区为亚洲上海
    ansible all -a "timedatectl set-timezone Asia/Shanghai"
    #更改sever端的ntp配置
    sed -i 's/# restrict 192.168.1.0 mask 255.255.255.0 nomodify notrap/ restrict 10.0.0.0 mask 255.255.255.0/' /etc/ntp.conf
    ansible all -m shell -a "sed -i 's/server 0.centos.pool.ntp.org iburst/#server 0.centos.pool.ntp.org iburst/' /etc/ntp.conf"
    ansible all -m shell -a "sed -i 's/server 1.centos.pool.ntp.org iburst/#server 1.centos.pool.ntp.org iburst/' /etc/ntp.conf"
    ansible all -m shell -a "sed -i 's/server 2.centos.pool.ntp.org iburst/#server 2.centos.pool.ntp.org iburst/' /etc/ntp.conf"
    ansible all -m shell -a "sed -i 's/server 3.centos.pool.ntp.org iburst/#server 3.centos.pool.ntp.org iburst/' /etc/ntp.conf"
    echo "server 127.127.1.0" >>/etc/ntp.conf
    ansible other[0-3] -m shell -a  "echo 'server 10.0.0.10' >>/etc/ntp.conf"
    systemctl enable ntpd; systemctl restart ntpd
    ansible other[0-3] -m shell -a "systemctl stop ntpd"
    ansible other[0-3] -m shell -a  "ntpdate 10.0.0.10"  
    ansible other[0-3] -m shell -a "systemctl start ntpd;systemctl enable ntpd"
    ansible other[0-3] -m shell -a "timedatectl set-ntp yes"

    执行过程

    [root@server ~]# bash ntp.sh 
    Loaded plugins: fastestmirror, langpacks
    Repository base is listed more than once in the configuration
    Repository updates is listed more than once in the configuration
    Repository extras is listed more than once in the configuration
    Repository centosplus is listed more than once in the configuration
    Loading mirror speeds from cached hostfile
     * base: mirrors.cn99.com
     * epel: mirror01.idc.hinet.net
     * extras: mirrors.163.com
     * updates: mirrors.163.com
    Package ansible-2.4.2.0-2.el7.noarch already installed and latest version
    Nothing to do
     [WARNING]: Consider using yum module rather than running yum
    
    10.0.0.13 | SUCCESS | rc=0 >>
    Loaded plugins: fastestmirror, langpacks
    Loading mirror speeds from cached hostfile
     * base: mirrors.cn99.com
     * epel: ftp.cuhk.edu.hk
     * extras: mirrors.cn99.com
     * updates: mirrors.cn99.com
    Package ntp-4.2.6p5-25.el7.centos.2.x86_64 already installed and latest version
    Nothing to doRepository base is listed more than once in the configuration
    Repository updates is listed more than once in the configuration
    Repository extras is listed more than once in the configuration
    Repository centosplus is listed more than once in the configuration
    
    10.0.0.12 | SUCCESS | rc=0 >>
    Loaded plugins: fastestmirror, langpacks
    Loading mirror speeds from cached hostfile
     * base: mirrors.163.com
     * epel: mirrors.ustc.edu.cn
     * extras: mirrors.163.com
     * updates: mirrors.aliyun.com
    Package ntp-4.2.6p5-25.el7.centos.2.x86_64 already installed and latest version
    Nothing to doRepository base is listed more than once in the configuration
    Repository updates is listed more than once in the configuration
    Repository extras is listed more than once in the configuration
    Repository centosplus is listed more than once in the configuration
    
    10.0.0.11 | SUCCESS | rc=0 >>
    Loaded plugins: fastestmirror, langpacks
    Loading mirror speeds from cached hostfile
     * base: mirrors.163.com
     * epel: ftp.cuhk.edu.hk
     * extras: mirrors.163.com
     * updates: mirrors.cn99.com
    Package ntp-4.2.6p5-25.el7.centos.2.x86_64 already installed and latest version
    Nothing to doRepository base is listed more than once in the configuration
    Repository updates is listed more than once in the configuration
    Repository extras is listed more than once in the configuration
    Repository centosplus is listed more than once in the configuration
    
    10.0.0.14 | SUCCESS | rc=0 >>
    Loaded plugins: fastestmirror, langpacks
    Loading mirror speeds from cached hostfile
     * base: mirrors.cn99.com
     * epel: mirror01.idc.hinet.net
     * extras: mirrors.163.com
     * updates: mirrors.163.com
    Package ntp-4.2.6p5-25.el7.centos.2.x86_64 already installed and latest version
    Nothing to doRepository base is listed more than once in the configuration
    Repository updates is listed more than once in the configuration
    Repository extras is listed more than once in the configuration
    Repository centosplus is listed more than once in the configuration
    
    10.0.0.10 | SUCCESS | rc=0 >>
    Loaded plugins: fastestmirror, langpacks
    Loading mirror speeds from cached hostfile
     * base: mirrors.cn99.com
     * epel: mirror01.idc.hinet.net
     * extras: mirrors.163.com
     * updates: mirrors.163.com
    Package ntp-4.2.6p5-25.el7.centos.2.x86_64 already installed and latest version
    Nothing to doRepository base is listed more than once in the configuration
    Repository updates is listed more than once in the configuration
    Repository extras is listed more than once in the configuration
    Repository centosplus is listed more than once in the configuration
    
    10.0.0.11 | SUCCESS | rc=0 >>
    
    
    10.0.0.14 | SUCCESS | rc=0 >>
    
    
    10.0.0.12 | SUCCESS | rc=0 >>
    
    
    10.0.0.13 | SUCCESS | rc=0 >>
    
    
    10.0.0.10 | SUCCESS | rc=0 >>
    
    
     [WARNING]: Consider using template or lineinfile module rather than running sed
    
    10.0.0.12 | SUCCESS | rc=0 >>
    
    
    10.0.0.13 | SUCCESS | rc=0 >>
    
    
    10.0.0.14 | SUCCESS | rc=0 >>
    
    
    10.0.0.11 | SUCCESS | rc=0 >>
    
    
    10.0.0.10 | SUCCESS | rc=0 >>
    
    
     [WARNING]: Consider using template or lineinfile module rather than running sed
    
    10.0.0.11 | SUCCESS | rc=0 >>
    
    
    10.0.0.14 | SUCCESS | rc=0 >>
    
    
    10.0.0.13 | SUCCESS | rc=0 >>
    
    
    10.0.0.12 | SUCCESS | rc=0 >>
    
    
    10.0.0.10 | SUCCESS | rc=0 >>
    
    
     [WARNING]: Consider using template or lineinfile module rather than running sed
    
    10.0.0.13 | SUCCESS | rc=0 >>
    
    
    10.0.0.12 | SUCCESS | rc=0 >>
    
    
    10.0.0.11 | SUCCESS | rc=0 >>
    
    
    10.0.0.14 | SUCCESS | rc=0 >>
    
    
    10.0.0.10 | SUCCESS | rc=0 >>
    
    
     [WARNING]: Consider using template or lineinfile module rather than running sed
    
    10.0.0.14 | SUCCESS | rc=0 >>
    
    
    10.0.0.12 | SUCCESS | rc=0 >>
    
    
    10.0.0.13 | SUCCESS | rc=0 >>
    
    
    10.0.0.11 | SUCCESS | rc=0 >>
    
    
    10.0.0.10 | SUCCESS | rc=0 >>
    
    
     [WARNING]: Use [x:y] inclusive subscripts instead of [x-y] which has been removed
    
    10.0.0.11 | SUCCESS | rc=0 >>
    
    
    10.0.0.13 | SUCCESS | rc=0 >>
    
    
    10.0.0.14 | SUCCESS | rc=0 >>
    
    
    10.0.0.12 | SUCCESS | rc=0 >>
    
    
     [WARNING]: Use [x:y] inclusive subscripts instead of [x-y] which has been removed
    
    10.0.0.13 | SUCCESS | rc=0 >>
    
    
    10.0.0.12 | SUCCESS | rc=0 >>
    
    
    10.0.0.11 | SUCCESS | rc=0 >>
    
    
    10.0.0.14 | SUCCESS | rc=0 >>
    
    
     [WARNING]: Use [x:y] inclusive subscripts instead of [x-y] which has been removed
    
    10.0.0.13 | SUCCESS | rc=0 >>
     2 Apr 21:52:05 ntpdate[5518]: adjust time server 10.0.0.10 offset 0.007596 sec
    
    10.0.0.12 | SUCCESS | rc=0 >>
     2 Apr 21:52:05 ntpdate[5755]: adjust time server 10.0.0.10 offset 0.017386 sec
    
    10.0.0.14 | SUCCESS | rc=0 >>
     2 Apr 21:52:05 ntpdate[6043]: adjust time server 10.0.0.10 offset 0.013542 sec
    
    10.0.0.11 | SUCCESS | rc=0 >>
     2 Apr 21:52:05 ntpdate[5523]: adjust time server 10.0.0.10 offset 0.012333 sec
    
     [WARNING]: Use [x:y] inclusive subscripts instead of [x-y] which has been removed
    
    10.0.0.12 | SUCCESS | rc=0 >>
    
    
    10.0.0.13 | SUCCESS | rc=0 >>
    
    
    10.0.0.14 | SUCCESS | rc=0 >>
    
    
    10.0.0.11 | SUCCESS | rc=0 >>
    
    
     [WARNING]: Use [x:y] inclusive subscripts instead of [x-y] which has been removed
    
    10.0.0.11 | SUCCESS | rc=0 >>
    
    
    10.0.0.13 | SUCCESS | rc=0 >>
    
    
    10.0.0.14 | SUCCESS | rc=0 >>
    
    
    10.0.0.12 | SUCCESS | rc=0 >>
    
    
    [root@server ~]# ntpstat
    synchronised to local net at stratum 6 
       time correct to within 7948 ms
       polling server every 64 s
    [root@server ~]# cat ntp.sh 
    #!/bin/bash
    #安装ansible
    yum install ansible –y
    echo "10.0.0.10" >>/etc/ansible/hosts
    echo "[other]" >>/etc/ansible/hosts
    echo -e "10.0.0.11
    10.0.0.12
    10.0.0.13
    10.0.0.14" >>/etc/ansible/hosts
    #批量安装ntp服务
    ansible all -a "yum install -y ntp"
    #批量设置 主机 时区为亚洲上海
    ansible all -a "timedatectl set-timezone Asia/Shanghai"
    #更改sever端的ntp配置
    sed -i 's/# restrict 192.168.1.0 mask 255.255.255.0 nomodify notrap/ restrict 192.168.1.0 mask 255.255.255.0/' /etc/ntp.conf
    ansible all -m shell -a "sed -i 's/server 0.centos.pool.ntp.org iburst/#server 0.centos.pool.ntp.org iburst/' /etc/ntp.conf"
    ansible all -m shell -a "sed -i 's/server 1.centos.pool.ntp.org iburst/#server 1.centos.pool.ntp.org iburst/' /etc/ntp.conf"
    ansible all -m shell -a "sed -i 's/server 2.centos.pool.ntp.org iburst/#server 2.centos.pool.ntp.org iburst/' /etc/ntp.conf"
    ansible all -m shell -a "sed -i 's/server 3.centos.pool.ntp.org iburst/#server 3.centos.pool.ntp.org iburst/' /etc/ntp.conf"
    echo "server 127.127.1.0" >>/etc/ntp.conf
    ansible other[0-3] -m shell -a  "echo 'server 10.0.0.10' >>/etc/ntp.conf"
    systemctl enable ntpd; systemctl restart ntpd
    ansible other[0-3] -m shell -a "systemctl stop ntpd"
    ansible other[0-3] -m shell -a  "ntpdate 10.0.0.10"  
    ansible other[0-3] -m shell -a "systemctl start ntpd;systemctl enable ntpd"
    ansible other[0-3] -m shell -a "timedatectl set-ntp yes"

    查看客户端同步情况

    随着 reach值不断增加,同步状态由 unsynchronised ----》syncchronised

    [root@client01 ~]# ntpq -p
         remote           refid      st t when poll reach   delay   offset  jitter
    ==============================================================================
     10.0.0.10       LOCAL(0)         6 u   65   64    1    1.081   11.696   0.000
    [root@client01 ~]# ntpq -p
         remote           refid      st t when poll reach   delay   offset  jitter
    ==============================================================================
     10.0.0.10       LOCAL(0)         6 u    4   64    3    0.233   -6.150  17.846
    [root@client01 ~]# ntpq -p
         remote           refid      st t when poll reach   delay   offset  jitter
    ==============================================================================
     10.0.0.10       LOCAL(0)         6 u    5   64    3    0.233   -6.150  17.846
    [root@client01 ~]# ntpq -p
         remote           refid      st t when poll reach   delay   offset  jitter
    ==============================================================================
     10.0.0.10       LOCAL(0)         6 u    7   64    3    0.233   -6.150  17.846
    [root@client01 ~]# ntpq -p
         remote           refid      st t when poll reach   delay   offset  jitter
    ==============================================================================
     10.0.0.10       LOCAL(0)         6 u   17   64    3    0.233   -6.150  17.846
    [root@client01 ~]# ntpq -p
         remote           refid      st t when poll reach   delay   offset  jitter
    ==============================================================================
     10.0.0.10       LOCAL(0)         6 u   26   64    3    0.233   -6.150  17.846
    [root@client01 ~]# ntpq -p
         remote           refid      st t when poll reach   delay   offset  jitter
    ==============================================================================
     10.0.0.10       LOCAL(0)         6 u   27   64    3    0.233   -6.150  17.846
    [root@client01 ~]# ntpstat          
    unsynchronised
      time server re-starting
       polling server every 8 s
    [root@client01 ~]# ntpstat
    unsynchronised
      time server re-starting
       polling server every 8 s
    [root@client01 ~]# ntpq -p
         remote           refid      st t when poll reach   delay   offset  jitter
    ==============================================================================
     10.0.0.10       LOCAL(0)         6 u   58   64    3    0.233   -6.150  17.846
    [root@client01 ~]# ntpq -p
         remote           refid      st t when poll reach   delay   offset  jitter
    ==============================================================================
     10.0.0.10       LOCAL(0)         6 u    1   64    7    0.206   -6.143  12.614
    [root@client01 ~]# ntpstat
    unsynchronised
      time server re-starting
       polling server every 8 s

    [root@client01 ~]# ntpstat
    unsynchronised
    time server re-starting
    polling server every 8 s
    [root@client01 ~]# ntpq -p
    remote refid st t when poll reach delay offset jitter
    ==============================================================================
    *10.0.0.10 LOCAL(0) 6 u 22 64 37 0.332 -6.075 8.886
    [root@client01 ~]# ntpq -p
    remote refid st t when poll reach delay offset jitter
    ==============================================================================
    *10.0.0.10 LOCAL(0) 6 u 25 64 37 0.332 -6.075 8.886
    [root@client01 ~]# ntpq -p
    remote refid st t when poll reach delay offset jitter
    ==============================================================================
    *10.0.0.10 LOCAL(0) 6 u 26 64 37 0.332 -6.075 8.886
    [root@client01 ~]# ntpq -p
    remote refid st t when poll reach delay offset jitter
    ==============================================================================
    *10.0.0.10 LOCAL(0) 6 u 28 64 37 0.332 -6.075 8.886
    [root@client01 ~]# ntpstat
    synchronised to NTP server (10.0.0.10) at stratum 7
    time correct to within 894 ms
    polling server every 64 s

  • 相关阅读:
    vue脚手架搭建项目步骤(转载)
    使用css样式设置按钮禁用
    如何设置页面文本不可复制?
    System.IO.IOException:需要的特权不被客户端持有。
    vs中 git fetch与git pull的区别
    如何获取按钮开关选中的状态?
    Uncaught TypeError: XXX is not a function...
    解决bootstrap缩小布局会乱的问题,缩小一定程度后出现横向滚动条
    日期(年月日)与时间戳互转换
    Java中的Finally
  • 原文地址:https://www.cnblogs.com/benjamin77/p/8698146.html
Copyright © 2020-2023  润新知