环境准备

[root@server ~]# cat /etc/redhat-release
CentOS Linux release 7.2. (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-.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 - .com
#mirrorlist=http://mirrorlist.centos.org/?release=$releasever&arch=$basearch&repo=os
baseurl=http://mirrors.163.com/centos/$releasever/os/$basearch/
gpgcheck=
gpgkey=http://mirrors.163.com/centos/RPM-GPG-KEY-CentOS-7 #released updates
[updates]
name=CentOS-$releasever - Updates - .com
#mirrorlist=http://mirrorlist.centos.org/?release=$releasever&arch=$basearch&repo=updates
baseurl=http://mirrors.163.com/centos/$releasever/updates/$basearch/
gpgcheck=
gpgkey=http://mirrors.163.com/centos/RPM-GPG-KEY-CentOS-7 #additional packages that may be useful
[extras]
name=CentOS-$releasever - Extras - .com
#mirrorlist=http://mirrorlist.centos.org/?release=$releasever&arch=$basearch&repo=extras
baseurl=http://mirrors.163.com/centos/$releasever/extras/$basearch/
gpgcheck=
gpgkey=http://mirrors.163.com/centos/RPM-GPG-KEY-CentOS-7 #additional packages that extend functionality of existing packages
[centosplus]
name=CentOS-$releasever - Plus - .com
baseurl=http://mirrors.163.com/centos/$releasever/centosplus/$basearch/
gpgcheck=
enabled=
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\n10.0.0.12\n10.0.0.13\n10.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[-] -m shell -a "echo 'server 10.0.0.10' >>/etc/ntp.conf"
systemctl enable ntpd; systemctl restart ntpd
ansible other[-] -m shell -a "systemctl stop ntpd"
ansible other[-] -m shell -a "ntpdate 10.0.0.10"
ansible other[-] -m shell -a "systemctl start ntpd;systemctl enable ntpd"
ansible other[-] -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..com
* updates: mirrors..com
Package ansible-2.4.2.0-.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= >>
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-.el7.centos..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= >>
Loaded plugins: fastestmirror, langpacks
Loading mirror speeds from cached hostfile
* base: mirrors..com
* epel: mirrors.ustc.edu.cn
* extras: mirrors..com
* updates: mirrors.aliyun.com
Package ntp-4.2.6p5-.el7.centos..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= >>
Loaded plugins: fastestmirror, langpacks
Loading mirror speeds from cached hostfile
* base: mirrors..com
* epel: ftp.cuhk.edu.hk
* extras: mirrors..com
* updates: mirrors.cn99.com
Package ntp-4.2.6p5-.el7.centos..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= >>
Loaded plugins: fastestmirror, langpacks
Loading mirror speeds from cached hostfile
* base: mirrors.cn99.com
* epel: mirror01.idc.hinet.net
* extras: mirrors..com
* updates: mirrors..com
Package ntp-4.2.6p5-.el7.centos..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= >>
Loaded plugins: fastestmirror, langpacks
Loading mirror speeds from cached hostfile
* base: mirrors.cn99.com
* epel: mirror01.idc.hinet.net
* extras: mirrors..com
* updates: mirrors..com
Package ntp-4.2.6p5-.el7.centos..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= >> 10.0.0.14 | SUCCESS | rc= >> 10.0.0.12 | SUCCESS | rc= >> 10.0.0.13 | SUCCESS | rc= >> 10.0.0.10 | SUCCESS | rc= >> [WARNING]: Consider using template or lineinfile module rather than running sed 10.0.0.12 | SUCCESS | rc= >> 10.0.0.13 | SUCCESS | rc= >> 10.0.0.14 | SUCCESS | rc= >> 10.0.0.11 | SUCCESS | rc= >> 10.0.0.10 | SUCCESS | rc= >> [WARNING]: Consider using template or lineinfile module rather than running sed 10.0.0.11 | SUCCESS | rc= >> 10.0.0.14 | SUCCESS | rc= >> 10.0.0.13 | SUCCESS | rc= >> 10.0.0.12 | SUCCESS | rc= >> 10.0.0.10 | SUCCESS | rc= >> [WARNING]: Consider using template or lineinfile module rather than running sed 10.0.0.13 | SUCCESS | rc= >> 10.0.0.12 | SUCCESS | rc= >> 10.0.0.11 | SUCCESS | rc= >> 10.0.0.14 | SUCCESS | rc= >> 10.0.0.10 | SUCCESS | rc= >> [WARNING]: Consider using template or lineinfile module rather than running sed 10.0.0.14 | SUCCESS | rc= >> 10.0.0.12 | SUCCESS | rc= >> 10.0.0.13 | SUCCESS | rc= >> 10.0.0.11 | SUCCESS | rc= >> 10.0.0.10 | SUCCESS | rc= >> [WARNING]: Use [x:y] inclusive subscripts instead of [x-y] which has been removed 10.0.0.11 | SUCCESS | rc= >> 10.0.0.13 | SUCCESS | rc= >> 10.0.0.14 | SUCCESS | rc= >> 10.0.0.12 | SUCCESS | rc= >> [WARNING]: Use [x:y] inclusive subscripts instead of [x-y] which has been removed 10.0.0.13 | SUCCESS | rc= >> 10.0.0.12 | SUCCESS | rc= >> 10.0.0.11 | SUCCESS | rc= >> 10.0.0.14 | SUCCESS | rc= >> [WARNING]: Use [x:y] inclusive subscripts instead of [x-y] which has been removed 10.0.0.13 | SUCCESS | rc= >>
Apr :: ntpdate[]: adjust time server 10.0.0.10 offset 0.007596 sec 10.0.0.12 | SUCCESS | rc= >>
Apr :: ntpdate[]: adjust time server 10.0.0.10 offset 0.017386 sec 10.0.0.14 | SUCCESS | rc= >>
Apr :: ntpdate[]: adjust time server 10.0.0.10 offset 0.013542 sec 10.0.0.11 | SUCCESS | rc= >>
Apr :: ntpdate[]: 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= >> 10.0.0.13 | SUCCESS | rc= >> 10.0.0.14 | SUCCESS | rc= >> 10.0.0.11 | SUCCESS | rc= >> [WARNING]: Use [x:y] inclusive subscripts instead of [x-y] which has been removed 10.0.0.11 | SUCCESS | rc= >> 10.0.0.13 | SUCCESS | rc= >> 10.0.0.14 | SUCCESS | rc= >> 10.0.0.12 | SUCCESS | rc= >> [root@server ~]# ntpstat
synchronised to local net at stratum
time correct to within ms
polling server every 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\n10.0.0.12\n10.0.0.13\n10.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[-] -m shell -a "echo 'server 10.0.0.10' >>/etc/ntp.conf"
systemctl enable ntpd; systemctl restart ntpd
ansible other[-] -m shell -a "systemctl stop ntpd"
ansible other[-] -m shell -a "ntpdate 10.0.0.10"
ansible other[-] -m shell -a "systemctl start ntpd;systemctl enable ntpd"
ansible other[-] -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() u 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() u 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() u 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() u 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() u 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() u 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() u 0.233 -6.150 17.846
[root@client01 ~]# ntpstat
unsynchronised
time server re-starting
polling server every s
[root@client01 ~]# ntpstat
unsynchronised
time server re-starting
polling server every s
[root@client01 ~]# ntpq -p
remote refid st t when poll reach delay offset jitter
==============================================================================
10.0.0.10 LOCAL() u 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() u 0.206 -6.143 12.614
[root@client01 ~]# ntpstat
unsynchronised
time server re-starting
polling server every 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

通过ansible一键部署集群ntp时间同步的更多相关文章

  1. 5分钟实现集群-NTP时间同步

    环境:VMware-Workstation-12-Pro,Windows-10,CentOS-7.5,Xshell5 NTP基本介绍 NTP(Network TimeProtocol,网络时间协议), ...

  2. Kubernetes学习之路(26)之kubeasz+ansible部署集群

    目录 1.环境说明 2.准备工作 3.分步骤安装 3.1.创建证书和安装准备 3.2.安装etcd集群 3.3.安装docker 3.4.安装master节点 3.5.安装node节点 3.6.部署集 ...

  3. [转帖]Ansible管理windows集群

    Ansible管理windows集群 http://www.cnblogs.com/Dev0ps/p/10026908.html 写的挺好的 我关注点还是不够好呢 最近公司新项目需要安装400+win ...

  4. redis在Windows下以后台服务一键搭建集群(多机器)

    redis在Windows下以后台服务一键搭建集群(多机器) 一.概述 此教程介绍如何在windows系统中多台机器之间布置redis集群,同时要以后台服务的模式运行.布置以脚本的形式,一键完成.多台 ...

  5. 从0到1使用Kubernetes系列(三):使用Ansible安装Kubernetes集群

    前两期的文章介绍了Kubernetes基本概念和架构,用Kubeadm+Ansible搭建Kubernetes集群所需要的工具及其作用.本篇介绍怎么使用Ansible安装Kubernetes集群. 启 ...

  6. WebLogic部署集群和代理服务器

    应公司要求,最近在学习weblogic集群这块的知识,下面我把我这几天学到的,以及过程中遇到的问题及如何解决的,分享给大家.首先,weblogic是Orcale公司的一款产品,至于其作用,我想就不用我 ...

  7. shell脚本一键同步集群时间

    shell脚本一键同步集群时间 弋嘤捕大 椿澄辄 ψ壤 茇徜燕 ㄢ交涔沔 阚龇棚绍 テ趼蜱棣 灵打了个寒颤也没有去甩脱愣是拖着 喇吉辔 秋北酏崖 琮淄脸酷 茇呶剑 莲夤罱 陕遇骸淫  ...

  8. ansible一键部署k8s单机环境

    一.虚拟机准备 干净的Centsot7.4.4G内存.2个CPU 最小化安装,最好带虚拟化 二.执行初始化脚本 注意:脚本中配置静态网卡根据实际网卡名称配置,我用的是ens33 可以用 sed -i ...

  9. [转贴]CentOS7.5 Kubernetes V1.13(最新版)二进制部署集群

    CentOS7.5 Kubernetes V1.13(最新版)二进制部署集群 http://blog.51cto.com/10880347/2326146   一.概述 kubernetes 1.13 ...

随机推荐

  1. P1880 [NOI1995]石子合并【区间DP】

    题目描述 在一个圆形操场的四周摆放N堆石子,现要将石子有次序地合并成一堆.规定每次只能选相邻的2堆合并成新的一堆,并将新的一堆的石子数,记为该次合并的得分. 试设计出1个算法,计算出将N堆石子合并成1 ...

  2. Scrapy的架构与原理的理解【转】

    Scrapy 框架 Scrapy是用纯Python实现一个为了爬取网站数据.提取结构性数据而编写的应用框架,用途非常广泛. 框架的力量,用户只需要定制开发几个模块就可以轻松的实现一个爬虫,用来抓取网页 ...

  3. Python+Selenium基础篇之4-XPath的使用

    开始写自动化脚本之前,我们先学习几个概念,在完全掌握了这几个概念之后,有助于我们快速上手,如何去编写自动化测试脚本. 元素,在这个教程系列,我们说的元素之网页元素(web element).在网页上面 ...

  4. Spring boot 上传文件大小限制

    1.spring boot 1.x 版本 application.properties 文件中 位置在(resources下) spring.http.multipart.maxFileSize = ...

  5. SQL 基础语法详解

    SQL 命令一般分为 DQL.DML.DDL DQL:数据查询语句,基本就是 SELECT 查询命令,用于数据查询 DML:Data Manipulation Language 的简称,即数据操纵语言 ...

  6. Spring框架DataSource

    一 DataSource 简易介绍 JDK里 javax.sql的一个接口 public interface DataSource 表示无力数据源的连接,作为DriverManager设施的替代项, ...

  7. VS 2013插件

    http://www.spersky.com/post/vsPlugins.html http://vswebessentials.com/download http://developer.51ct ...

  8. AsyncSocket

    如果需要在项目中像QQ微信一样做到即时通讯,必须使用socket通讯,本人也是刚学习,分享一下,有什么不对的地方希望大家指正 ios原生的socket用起来不是很直观,所以我用的是AsyncSocke ...

  9. table单元格内容过多换行显示

    <table class="am-table am-table-striped am-table-hover table-main am-table-compact " st ...

  10. IIS8 不能在此路径中使用此配置节。如果在父级别上锁定了该节

    问题: 不能在此路径中使用此配置节.如果在父级别上锁定了该节,便会出现这种情况.锁定是默认设置的(overrideModeDefault="Deny"),或者是通过包含 overr ...