通过ansible一键部署集群ntp时间同步
环境准备
[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时间同步的更多相关文章
- 5分钟实现集群-NTP时间同步
环境:VMware-Workstation-12-Pro,Windows-10,CentOS-7.5,Xshell5 NTP基本介绍 NTP(Network TimeProtocol,网络时间协议), ...
- Kubernetes学习之路(26)之kubeasz+ansible部署集群
目录 1.环境说明 2.准备工作 3.分步骤安装 3.1.创建证书和安装准备 3.2.安装etcd集群 3.3.安装docker 3.4.安装master节点 3.5.安装node节点 3.6.部署集 ...
- [转帖]Ansible管理windows集群
Ansible管理windows集群 http://www.cnblogs.com/Dev0ps/p/10026908.html 写的挺好的 我关注点还是不够好呢 最近公司新项目需要安装400+win ...
- redis在Windows下以后台服务一键搭建集群(多机器)
redis在Windows下以后台服务一键搭建集群(多机器) 一.概述 此教程介绍如何在windows系统中多台机器之间布置redis集群,同时要以后台服务的模式运行.布置以脚本的形式,一键完成.多台 ...
- 从0到1使用Kubernetes系列(三):使用Ansible安装Kubernetes集群
前两期的文章介绍了Kubernetes基本概念和架构,用Kubeadm+Ansible搭建Kubernetes集群所需要的工具及其作用.本篇介绍怎么使用Ansible安装Kubernetes集群. 启 ...
- WebLogic部署集群和代理服务器
应公司要求,最近在学习weblogic集群这块的知识,下面我把我这几天学到的,以及过程中遇到的问题及如何解决的,分享给大家.首先,weblogic是Orcale公司的一款产品,至于其作用,我想就不用我 ...
- shell脚本一键同步集群时间
shell脚本一键同步集群时间 弋嘤捕大 椿澄辄 ψ壤 茇徜燕 ㄢ交涔沔 阚龇棚绍 テ趼蜱棣 灵打了个寒颤也没有去甩脱愣是拖着 喇吉辔 秋北酏崖 琮淄脸酷 茇呶剑 莲夤罱 陕遇骸淫 ...
- ansible一键部署k8s单机环境
一.虚拟机准备 干净的Centsot7.4.4G内存.2个CPU 最小化安装,最好带虚拟化 二.执行初始化脚本 注意:脚本中配置静态网卡根据实际网卡名称配置,我用的是ens33 可以用 sed -i ...
- [转贴]CentOS7.5 Kubernetes V1.13(最新版)二进制部署集群
CentOS7.5 Kubernetes V1.13(最新版)二进制部署集群 http://blog.51cto.com/10880347/2326146 一.概述 kubernetes 1.13 ...
随机推荐
- 第15课 栏目的排序处理(组件化) Thinkphp5商城第四季
目录 要实现的功能 思路: 视图层 控制器里: 扩展函数里 要实现的功能 用表单里的提交过来的sort数据,批量修改表里的排序值 界面效果: 思路: 视图层表单提交数据主键=>sort值 控制器 ...
- B - Sonya and Exhibition CodeForces - 1004B (思维题)
B. Sonya and Exhibition time limit per test 1 second memory limit per test 256 megabytes input stand ...
- 问题 B: 分组统计
分组统计 问题 B: 分组统计时间限制: 1 Sec 内存限制: 32 MB 提交: 416 解决: 107 [提交][状态][讨论版][命题人:外部导入] 题目描述 先输入一组数,然后输入其分组,按 ...
- CDOJ:1636-梦后楼台高锁,酒醒帘幕低垂(Kruskal+暴力)
梦后楼台高锁,酒醒帘幕低垂 Time Limit: 3000/1000MS (Java/Others) Memory Limit: 65535/65535KB (Java/Others) Submit ...
- CodeForces 781E Andryusha and Nervous Barriers 线段树 扫描线
题意: 有一个\(h \times w\)的矩形,其中有\(n\)个水平的障碍.从上往下扔一个小球,遇到障碍后会分裂成两个,分别从障碍的两边继续往下落. 如果从太高的地方落下来,障碍会消失. 问从每一 ...
- Java并发之(3):锁
锁是并发编程中的重要概念,用来控制多个线程对同一资源的并发访问,在支持并发的编程语言中都有体现,比如c++ python等.本文主要讲解Java中的锁,或者说是重入锁.之所以这么说是因为在Java中, ...
- Lucene.Net 精品教程
http://www.cnblogs.com/piziyimao/archive/2013/01/31/2887072.html
- iOS笔记052- Quartz2D-绘图
简介 Quartz 2D是一个二维绘图引擎,同时支持iOS和Mac系统 Quartz 2D能完成的工作 绘制图形 : 线条\三角形\矩形\圆\弧等 绘制文字 绘 ...
- 静态文本框控件的美化CStatic
VC通用控件都是灰色,当对程序界面进行美化时,使用通用控件就和美化后的程序界面不搭配,在VB,C#中,可以很方便的更改控件背景颜色,但在VC中就不能,需要我们自己来完善这方面的功能.我在这只简单的介绍 ...
- 踩坑 PHP Fatal Error Failed opening required File
使用 require 引用文件时,报错如下: require 'https://dev.ryan.com/test.php'; [Sat Mar 19 23:10:50 2011] [warn] mo ...