设为首页 收藏本站
查看: 5306|回复: 0

[经验分享] 搭建ntp时间服务器并借助ansible工具进行管理

[复制链接]
累计签到:1 天
连续签到:1 天
发表于 2014-12-22 09:16:44 | 显示全部楼层 |阅读模式
                      ntp服务的重要性本文不再赘述,有兴趣的可以百度或者鸟哥的书翻一下。这里跟大家分享一下如何配置ntp服务器   
原理:让一部主要的Primary Server校对时间---->开放网络服务让Clients主机来连接 ----->Clients自己调整时间。   
有关ntp服务的配置参数   
ntp服务进程监听在UDP的123端口。   
/usr/share/zoneinfo里面存放着世界各个地区的时间区域数据(timezone data),不要企图用文本编辑器直接打开!   
/etc/sysconfig/clock  Linux开机后会自动读取该文件中的数据来设定系统时间,如在中国,通常该项为   
                                    ZONE="Asia/Shanghai"   
/usr/sbin/ntpd   服务端启动ntp的服务名称   
/usr/sbin/ntpdate  客户端启动校对时间的命令,注意这里的路径/usr/sbin,通常ntpdate命令是需要写入crontab列表中,千万别写错路径,曾经有血淋淋的教训。
ntp服务器端主配置文件 /etc/ntp.conf  (注意配置ntp服务器端的前提是你需要有一个上游时间服务器真正运行,这里笔者用的是阿里的时间服务器=110.75.186.249,IP地址: 110.75.186.249浙
江省杭州市 阿里巴巴)
/etc/ntp.conf配置文件主要定义了三段   
1、server 定义了你上一层ntp服务器的地址   
格式: server [IP OR HOSTNAME] [prefer]   
2、restrict  管理和控制该ip地址的权限   
格式: restrict [IP] mask [netmask_IP掩码][parameter]   
参数:说常用的   
nomodify:允许客户端同步服务器端的时间,不允许客户端修改服务器端时间。   
3、driftfile  用来记录与server中的上一层服务器的差异时间   
格式:driftfile /PATH/TO/FILE  (使用绝对路径)  
FILE档案记录的数值单位为百万分之一秒 ppm
这是我的ntp服务器的主配置文件参数
driftfile /var/lib/ntp/drift
# Permit time synchronization with our time source, but do not   
# permit the source to query or modify the service on this system.   
restrict default kod nomodify notrap nopeer noquery   
restrict -6 default kod nomodify notrap nopeer noquery
# Permit all access over the loopback interface.  This could   
# be tightened as well, but to do so would effect some of   
# the administrative functions.   
restrict 127.0.0.1   
restrict -6 ::1
# Hosts on local network are less restricted.   
#restrict 192.168.1.0 mask 255.255.255.0 nomodify notrap   
restrict 192.168.1.0 mask 255.255.255.0 nomodify     # 我允许192.168.1.0网段内的客户端主机从我这里同步时间   
# Use public servers from the pool.ntp.org project.   
# Please consider joining the pool (http://www.pool.ntp.org/join.html).   
#server 0.centos.pool.ntp.org iburst   
#server 1.centos.pool.ntp.org iburst   
#server 2.centos.pool.ntp.org iburst   
#server 3.centos.pool.ntp.org iburst  
server 110.75.186.249 prefer    #上层服务器我用的是110.75.186.249   
实验环境,一台CentOS6.5操作系统的主机当做ntp服务器提供服务,另外找两台主机当做客户端同步时间。   
主ntp服务器 192.168.1.141   
当前时间 Sat Dec 20 23:34:34 CST 2014   
1、ntp服务器yum install ntp即可     
2、修改配置文件定义了如上的参数   
3、service ntpd start    &&   chkconfig ntpd on   最好加入开机启动项中   
4、netstat -tunlp | grep  123 是否有ntpd服务进程启动
客户端ntp服务器1 192.168.1.111   
当前时间  Sat Dec 20 22:27:22 CST 2014  肯定与主服务器不同步   
客户端ntp服务器2 192.168.1.115   
当前时间  Sat Dec 20 22:34:32 CST 2014 肯定也与主服务器不同步
两个客户端使用ntpdate 命令同步主服务器看看是否有效果   
第1台返回结果:20 Dec 23:38:58 ntpdate[2269]: step time server 192.168.1.141 offset 4102.953608 sec   
第2台返回结果:20 Dec 23:39:00 ntpdate[2149]: step time server 192.168.1.141 offset 3798.445042 sec   
经试验,两台时间均与服务器保持一致。至此ntp服务器建立完成!   
P.S还可以在两台客户端节点上使用crontab命令来实施日常同步时间   
*/3 * * * * /usr/sbin/ntpdate 192.168.1.141 &> /dev/null  定义每3分钟同步一次时间

这第一大步算是迈出去了,剩下还有一大步就是如何使用ansible工具管理呢?
简介ansible:基于python语言开发出来的agentless(无agent节点)命令发布、管理工具,它具有幂等性而且基于模块进行工作。
好,开始在控制节点上部署ansible,环境是CentOS6.5(这里注意下,需要你配置好epel源,这里笔者提供一个http://mirrors.aliyun.com/epel/6Server/x86_64/
1、yum install ansible
(由于ansible依赖python开发的,因此yum安装ansible时候同时也会安装python的几个框架,分别有
1、python-jinja2 专门来使用文本写playbook,2、python-paramiko 基于ssh连接agentless的客户端。)
2、默认安装ansible,主配置文件是/etc/ansible/ansible.cfg
         Host inventory文件是/etc/ansible/hosts
         主程序/usr/bin/ansible
         /usr/bin/ansible-playbook运行playbook的ansible程序
         /usr/bin/ansible-vault 加密存放ansible的playbook文件
ansible <主机模式>  [-m MODULE] -a 'MODULE_ARGS'
3、vim /etc/ansible/hosts文件,可以将文件清空,定义属于自己的配置文件。如:
[hbhosts]
192.168.1.111
192.168.1.115
(这里,如果你控制主机上没有安装sshpass,当你使用ansible命令时,会提醒你先安装sshpass yum install sshpass即可)
4、在使用ansible控制之前,默认它需要你把控制主机生成的ssh-keygen复制到各被控制主机上的,不然就要加-k 选项
如:
[iyunv@localhost ~]# ansible hbhosts -m command -a 'date' -k
[WARNING]: The version of gmp you have installed has a known issue regarding
timing vulnerabilities when used with pycrypto. If possible, you should update
it (i.e. yum update gmp).
SSH password:
192.168.1.111 | FAILED => Authentication failure.  (这里报错的原因是我这台主机密码并不是输入的哪一个)
192.168.1.115 | success | rc=0 >>
Sun Dec 21 15:26:58 CST 2014
如何解决这个问题呢?
可以这样设置,还是编辑主配置文件
192.168.1.111  ansible_ssh_pass=h2026825
192.168.1.115  ansible_ssh_pass=mageedu  #甚至还可以以其他用户来连接ansible_ssh_user=用户名的方法
这里就报出了结果
192.168.1.115 | success | rc=0 >>
Filesystem            Size  Used Avail Use% Mounted on
/dev/mapper/vg0-root   20G  434M   19G   3% /
tmpfs                 116M     0  116M   0% /dev/shm
/dev/sda1             194M   30M  155M  16% /boot
/dev/mapper/vg0-usr   9.9G  2.4G  7.0G  26% /usr
/dev/mapper/vg0-var    20G  373M   19G   2% /var
192.168.1.111 | success | rc=0 >>
Filesystem            Size  Used Avail Use% Mounted on
/dev/mapper/vg0-root   20G  1.2G   18G   7% /
tmpfs                 116M     0  116M   0% /dev/shm
/dev/sda1             194M   30M  155M  16% /boot
/dev/mapper/vg0-usr   9.9G  2.4G  7.0G  26% /usr
/dev/mapper/vg0-var    20G  412M   19G   3% /var
执行成功
5、如果想查看ansible支持的模块, ansibledoc -l 列出所有支持的模块 ,想查看单一模块支持的具体选项
ansibledoc [模块名称]即可
如何利用ansible添加用户叫centos并设定密码:
第一步:生成一个密码串
# openssl passwd -1 -salt `openssl rand -hex 4`
Password:
$1$6b0ba54d$Ia6IPhDOPsr6sR9dqTptr1
第二步:创建centos用户
[iyunv@localhost ~]# ansible hbhosts -m user -a 'name=centos password=$1$6b0ba54d$Ia6IPhDOPsr6sR9dqTptr1'
[WARNING]: The version of gmp you have installed has a known issue regarding
timing vulnerabilities when used with pycrypto. If possible, you should update
it (i.e. yum update gmp).
192.168.1.115 | success >> {
    "changed": true,
    "comment": "",
    "createhome": true,
    "group": 502,
    "home": "/home/centos",
    "name": "centos",
    "password": "NOT_LOGGING_PASSWORD",
    "shell": "/bin/bash",
    "state": "present",
    "system": false,
    "uid": 502
}

192.168.1.111 | success >> {
    "changed": true,
    "comment": "",
    "createhome": true,
    "group": 502,
    "home": "/home/centos",
    "name": "centos",
    "password": "NOT_LOGGING_PASSWORD",
    "shell": "/bin/bash",
    "state": "present",
    "system": false,
    "uid": 502
}
分别创建成功
案例1、
修改ansible使用其他用户
编辑主配置文件
192.168.1.115  ansible_ssh_pass=mageedu ansible_ssh_user=centos
再次执行命令查看
[iyunv@localhost ~]# ansible hbhosts -m command  -a 'whoami'
[WARNING]: The version of gmp you have installed has a known issue regarding
timing vulnerabilities when used with pycrypto. If possible, you should update
it (i.e. yum update gmp).
192.168.1.115 | success | rc=0 >>
centos     #已经变成了centos
192.168.1.111 | success | rc=0 >>
root
案例2、利用ansible复制功能实现被控主机都有对应文件,比如把python的tar包复制到多节点的tmp目录下
[iyunv@localhost ~]# ansible hbhosts -m copy -a 'src=/root/Python-2.7.6.tar.xz dest=/tmp/'
[WARNING]: The version of gmp you have installed has a known issue regarding
timing vulnerabilities when used with pycrypto. If possible, you should update
it (i.e. yum update gmp).
192.168.1.115 | success >> {
    "changed": true,
    "checksum": "8321636af2acbeaa68fc635d7dda7369ed446a80",
    "dest": "/tmp/Python-2.7.6.tar.xz",
    "gid": 502,
    "group": "centos",
    "md5sum": "bcf93efa8eaf383c98ed3ce40b763497",
    "mode": "0664",
    "owner": "centos",
    "size": 10431288,
    "src": "/home/centos/.ansible/tmp/ansible-tmp-1419151990.97-242090512088858/source",
    "state": "file",
    "uid": 502
}
192.168.1.111 | success >> {
    "changed": true,
    "checksum": "8321636af2acbeaa68fc635d7dda7369ed446a80",
    "dest": "/tmp/Python-2.7.6.tar.xz",
    "gid": 0,
    "group": "root",
    "md5sum": "bcf93efa8eaf383c98ed3ce40b763497",
    "mode": "0644",
    "owner": "root",
    "size": 10431288,
    "src": "/root/.ansible/tmp/ansible-tmp-1419151990.97-280490291820146/source",
    "state": "file",
    "uid": 0
}
案例三、利用ansible给各多节点部署crontab,让多节点被控制主机每隔三分钟同步一次时间服务器(注意在部署crontab之前需要将在crontabst ~]# ansible hbhosts -m cron -a 'name="sync time" minute="*/3" job="/usr/sbin/ntpdate 192.168.1.141"'
[WARNING]: The version of gmp you have installed has a known issue regarding
timing vulnerabilities when used with pycrypto. If possible, you should update
it (i.e. yum update gmp).
192.168.1.115 | success >> {
    "changed": true,
    "jobs": [
        "sync time"
    ]
}
192.168.1.111 | success >> {
    "changed": true,
    "jobs": [
        "sync time"
    ]
}
再看各节点的信息
QQ截图20141222091600.png
wKioL1SWsDWjpkLxAABsKgQA5n0081.jpg
执行成功
                   


运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-38372-1-1.html 上篇帖子: ansible when在include中使用 下篇帖子: ansible 批量创建用户 服务器
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表