puppet批量管理500多台服务器

前言

puppet使用了有一段时间了,以前写的手顺书一直未发布到blog上来,今天正好有空,写下一点笔记。公司在用的服务器有500多台,基本都为CentOS,版本有5和6两种,管理起来很不方便,尤为是部署监控,其中有大量重复性工做,使用puppet能够方便不少。html

简介

安装前,简介固然是必定要有的啦,简单介绍下吧。puppet是基于客户端和服务器端的C/S架构,基于ruby开发。因此,你要明白,安装puppet,就须要安装配置ruby。web管理界面相似于redmine的安装,使用apache的passenger模块整合。mysql

服务器端部署

一、Download and install packages
URL:http://yum.puppetlabs.com/el/5/products/i386/
Packages:
puppetlabs-release-5-6.noarch.rpm(puppet repo)
puppet-dashboard-1.2.23-1.el5.noarch.rpm(puppet-dashboard)
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文档
 
三、 Server configuration
对于puppet.conf 来讲,里面分红3部分[main], [master], [agent], 外面的文档,有些是把参数添加到[main], 有些是添加到[agent], 用初学者比较困惑,到底那个是正确。对于agent来讲,你就在agent里修改就能够。若是你的设置和[main]冲突,就会保留[agent]设置。因此你基本就不须要管[main]设置就能够。
/etc/sysconfig/puppetmaster  不用修改
/etc/sysconfig/puppet        不用修改
/etc/puppet/puppet.conf     不用修改
执行:/etc/init.d/puppetmaster start
 
四、Client configuration
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

 
五、Commands
查看服务端证书
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 server
(2)从新注册证书
在客户端
rm -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)自动注册证书
能够设置master自动签发全部的证书,咱们只须要在/etc/puppet 目录下建立 autosign.conf 文件。(不须要修改 /etc/puppet/puppet.conf文件,由于我默认的autosign.conf 文件的位置没有修改)
vim /etc/puppet/autosign.conf
*.exmaple.com

     这样全部来自example.com域上的全部客户端就自动注册了。

八、 puppet dashboard
这个主要是方便看的,看报告能快速知道那台服务器puppet应用更新失败。
(1) 修改my.cnf
在[mysqld]模块下添加max_allowed_packet = 32M
(2)建立数据库
CREATE 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.rb
#config.time_zone = 'UTC'
       config.time_zone = 'Beijing'
(5) 配置apache
vim /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 server端的安装过程了,还有一些技巧随后补充,如客户端批量部署、排错等。
 

客户端部署 

既然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客户端了。

相关文章
相关标签/搜索