puppet使用了有一段时间了,以前写的手顺书一直未发布到blog上来,今天正好有空,写下一点笔记。公司在用的服务器有500多台,基本都为CentOS,版本有5和6两种,管理起来很不方便,尤为是部署监控,其中有大量重复性工做,使用puppet能够方便不少。html
安装前,简介固然是必定要有的啦,简单介绍下吧。puppet是基于客户端和服务器端的C/S架构,基于ruby开发。因此,你要明白,安装puppet,就须要安装配置ruby。web管理界面相似于redmine的安装,使用apache的passenger模块整合。mysql
rpm –ivh puppetlabs-release-5-6.noarch.rpm rpm –ivh puppet-dashboard-1.2.23-1.el5.noarch.rpm yum install puppet-server puppetdb puppetdb-termius(后面两个能够不装)
二、Install ruby、mysql、apache_passenger moduleweb
参见安装 redmine文档。rpm –ivh puppetlabs-release-5-6.noarch.rpm yum install puppet /etc/puppet/puppet.conf 修改以下内容,值为puppet服务器的hostname PUPPET_SERVER=server.example.com 执行 /etc/init.d/puppet start
或者不修改配置文件,直接puppet agent –server=server.example.comsql
puppet cert list –all
查看模块位置数据库
puppet config print modulepath
查看报告apache
Puppet agent –t –summarize
六、Certificate Registervim
客户端agent启动时候会给服务器发送证书申请puppet cert sign station3.example.com
七、 Certificate Canclecentos
(1)注销证书puppet cert revoke station3.example.com(只是让证书失效)
puppet cert clean station3.example.com (删除证书)
重启puppetmaster安全
此时,station3.exmaple.com不能链接到puppet serverrm -f /var/lib/puppet/ssl/certs/station3.example.com.pem rm -f /var/lib/puppet/ssl/certificate_requests/station3.example.com.pem
而后重启puppet,在服务器端执行puppet cert list就能看见从新申请证书。ruby
(3)自动注册证书vim /etc/puppet/autosign.conf
*.exmaple.com
这样全部来自example.com域上的全部客户端就自动注册了。
八、 puppet dashboardCREATE DATABASE dashboard_production CHARACTER SET utf8; CREATE USER 'dashboard'@'localhost' IDENTIFIED BY 'password'; GRANT ALL PRIVILEGES ON dashboard.* TO 'dashboard'@'localhost'; FLUSH PRIVILEGES;
(3)编辑 /usr/share/puppet-dashboard/config/database.yml
(4)修改时区 /usr/share/puppet-dashboard/config/environment.rbvim /etc/httpd/conf.d/puppet.conf LoadModule passenger_module /usr/local/ruby/lib/ruby/gems/1.8/gems/passenger-3.0.18/ext/apache2/mod_passenger.so PassengerRoot /usr/local/ruby/lib/ruby/gems/1.8/gems/passenger-3.0.18 PassengerRuby /usr/local/ruby/bin/ruby Listen 3001 <VirtualHost *:3001> ServerName server.example.com # !!! Be sure to point DocumentRoot to 'public'! DocumentRoot /usr/share/puppet-dashboard/public <Directory /usr/share/puppet-dashboard/public > # This relaxes Apache security settings. AllowOverride all # MultiViews must be turned off. Options -MultiViews </Directory> </VirtualHost>
这样puppet就跟redmine同样用apache的方式启动了。
(6)初始化数据库cd /usr/share/puppet-dashboard/
rake RAILS_ENV=production db:migrate
(7) 导入reports(默认在/var/lib/puppet/reports目录下)
cd /usr/share/puppet-dashboard/
rake RAILS_ENV=production reports:import REPORT_DIR= /var/lib/puppet/reports
(8)Delayed Job Workers
cd /usr/share/puppet-dashboard/ env RAILS_ENV=production script/delayed_job -p dashboard -n 4 -m start(开始分析reports) ps -ef|grep delayed_job|grep -v grep(查看delayed_job 进程) env RAILS_ENV=production script/delayed_job -p dashboard -n 4 -m stop(中止分析)
注意这个进程不要停掉,要一直存在,因此么,有时候重启服务器会忘记,干脆写入到/etc/rc.local中
既然puppet是C/S架构的,因此还得在每台服务器上部署客户端,可是500多台服务器,不可能手工的一台一台去部署,天然经过脚本的方式。
(1)免认证
对于500台及以上的服务器集群,应用之间的耦合度很是高,并且为了管理方便,通常都有操做系统层级的互信,也就是ssh免认证。
固然,有人会说这样会有内网安全的隐患,若是控制了一台服务器,那么整个内网都将沦陷。是的,一点没错。安全跟便利自己就是相互矛盾的,我认为对于IDC服务器集群架构的安全防御主要仍是从防火墙访问限制和权限控制上着手,要既能保证业务正常运做也能保证服务器自己的安全。
(2)hosts文件
因为是服务器集群,全部服务器之间的信任通常都是经过/etc/hosts文件记录其余服务器hostname跟IP的映射关系。
基于内网中服务器之间的免认证,咱们可使用脚本将安装脚本推送到全部服务器上,而后再执行安装脚本,这样就是实现了puppet客户端的自动安装。
安装脚本,install_puppet.sh
#!/bin/bash version5=0 version6=0 [ -f /etc/init.d/puppet ]&& /etc/init.d/puppet restart [ -f /etc/sysconfig/puppet ]&& exit version5=`/bin/cat /etc/issue|head -1|grep '5.'|wc -l` if [ $version5 = 1 ];then rpm -ivh http://yum.puppetlabs.com/el/5/products/i386/puppetlabs-release-5-6.noarch.rpm yum -y install puppet puppet agent --server server.example.com [ -f /sbin/chkconfig ]&&`chkconfig puppet on` #echo "centos 5" else version6=`/bin/cat /etc/issue|head -1|grep '6.'|wc -l` if [ $version6 = 1 ];then rpm -ivh http://yum.puppetlabs.com/el/6/products/i386/puppetlabs-release-6-6.noarch.rpm yum -y install puppet puppet agent --server server.example.com [ -f /sbin/chkconfig ]&&`chkconfig puppet on` #echo "centos 6" fi fi
推送脚本push.pl,基于/etc/hosts文件中的记录。
#!/usr/bin/perl -w if (@ARGV) { foreach (@ARGV) { if ($ARGV[0] =~ "all") { open(FILE,"</etc/hosts")||die"cannot open the file: $!\n"; while (<FILE>) { if ($_ =~ /^10/) { my @host=split; print "########It's coping file to $host[1]########\n"; system("/usr/bin/rsync install_puppet.sh $host[0]:/"); system("/usr/bin/ssh $host[0] /install_puppet.sh"); } } close FILE; } else { print "########It's coping file to $_########\n"; system("/usr/bin/rsync install_puppet.sh $_:/"); system("/usr/bin/ssh $_ /install_puppet.sh"); } } } else { print "1.Usage: $0 hostname1 hostname2 ... \n"; print "2.Usage: $0 all\n"; }
这样执行将两个脚本放在同一目录,而后执行./push.pl all,而后就不用管了,全部服务器都自动部署对应版本的puppet客户端了。