通过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 ...
随机推荐
- 使用HTTP协议访问网络
在Android上发送http请求有2种方式,分别由两个类完成,HttpURLConnection和HttpClient. 一.使用HttpURLConnection方式 1.1 建立连接的基本步骤 ...
- A1027 Colors in Mars (20)(20 分)
A1027 Colors in Mars (20)(20 分) People in Mars represent the colors in their computers in a similar ...
- Contest1893 - 2019年6月多校联训b层测试1
传送门 密码:waxadyt T1 暴力 对于任意相邻的两个值 中间能到达的最大高度是固定的 加上头尾,判一下就好了 代码//感谢Th Au K #include<bits/stdc++.h&g ...
- ajax跨域请求的处理
跨域的情形有很多种,网上有人给出了一份表格, 表格中标识为”不允许”通信的情况都属于跨域.实际网络服务中需要跨域的情况确实存在,于是开发者们提供了一种解决方案,就是使用jsonp格式进行数据交互,它不 ...
- Java web学习总结
javaweb学习总结(十四)——JSP原理 孤傲苍狼 2014-07-24 09:38 阅读:46603 评论:37 JavaWeb学习总结(十三)——使用Session防止表单重复提交 孤 ...
- leetcode 【 Reverse Linked List II 】 python 实现
题目: Reverse a linked list from position m to n. Do it in-place and in one-pass. For example:Given 1- ...
- Python-S9——Day100-Web前端框架之Vue
01 课程简介: 02 let和const: 03 箭头函数: 04 对象的单体模式: 05 nodejs介绍和npm操作: 06 webpack.babel介绍和vue的第一个案例: 07 昨日内容 ...
- Leetcode 592.分数加减运算
分数加减运算 给定一个表示分数加减运算表达式的字符串,你需要返回一个字符串形式的计算结果. 这个结果应该是不可约分的分数,即最简分数. 如果最终结果是一个整数,例如 2,你需要将它转换成分数形式,其分 ...
- Matlab freqs 函数
freqs 模拟滤波器的频率响应 语法: h = freqs(b,a,w)[h,w] = freqs(b,a)[h,w] = freqs(b,a,f)freqs(b,a) 描述: freqs 返回一个 ...
- Redis实现缓存,你应该懂的哪些思路!
场景一:类似于微博,实现关注和被关注功能. 思路: 对每个用户使用两个集合类型键,用来存储关注别人的用户和被该用户关注的用户.当用户A关注用户B的时候,执行两步操作: sadd user:A B sa ...