通过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 ...
随机推荐
- python面试题之介绍一下Python中webbrowser的用法
所属网站分类: 面试经典 > python 作者:外星人入侵 链接: http://www.pythonheidong.com/blog/article/13/ 来源:python黑洞网 www ...
- 从源码带你看懂functools的partial方法
1.what? partial是什么, partial也叫偏函数.源码的描述是: 部分应用给定参数和关键字的新函数. New function with partial application of ...
- 静态属性property的本质和应用
一.本质 静态属性property本质就是实现了get,set,delete三种方法 class Foo: @property def AAA(self): print('get的时候运行我啊') @ ...
- Codeforces Round #271 (Div. 2) D Flowers【计数dp】
D. Flowers time limit per test 1.5 seconds memory limit per test 256 megabytes input standard input ...
- Word 2013发布博客测试
Hello world ! I am from word2013! 测试修改 这里添加一行文字. 参考 1在 Word 中建立博客的相关帮助 2使用Word2013发布随笔到博客园 PS: 参考2 ...
- Flume-NG源码分析-整体结构及配置载入分析
在 http://flume.apache.org 上下载flume-1.6.0版本,将源码导入到Idea开发工具后如下图所示: 一.主要模块说明 flume-ng-channels 里面包含了fil ...
- Git-GIt检出
实际上在执行重置命令的时候没有使用任何参数对所要重置的分支名进行设置,这是因为重置命名实际上所针对的是头指针HEAD.之所以没有改变HEAD的内容是因为HEAD指向了一个引用refs/heads/ma ...
- 手机注册过哪些网站37kfenxi.com,查询注册过哪些网站
注册过哪些网站?发现这么一个网站,https://www.37kfenxi.com?_=cnblogs 可以根据手机号码查询注册过哪些网站,然后通过大数据分析出机主的性格,爱好等. 据说还可以查老板, ...
- 【Linked List Cycle】cpp
题目: Given a linked list, determine if it has a cycle in it. Follow up:Can you solve it without using ...
- leetcode 【 Pascal's Triangle 】python 实现
题目: Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows = 5,R ...