puppet批量管理500多台服务器

前言

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

简介

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

服务器端部署

1、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(后面两个可以不装)

2、Install ruby、mysql、apache_passenger module

参见安装redmine文档
 
3、 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
 
4、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.com

 
5、Commands
查看服务端证书
puppet cert list –all

查看模块位置

puppet config print modulepath

查看报告

Puppet agent –t –summarize

6、Certificate Register

客户端agent启动时候会给服务器发送证书申请
在服务器上有上述命令查看证书,然后签名
puppet cert sign station3.example.com

7、 Certificate Cancle

(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就能看见重新申请证书。

(3)自动注册证书
可以设置master自动签发所有的证书,我们只需要在/etc/puppet 目录下创建 autosign.conf 文件。(不需要修改 /etc/puppet/puppet.conf文件,因为我默认的autosign.conf 文件的位置没有修改)
vim /etc/puppet/autosign.conf
*.exmaple.com

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

8、 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客户端了。

posted on 2013-07-23 11:51  Yiffy  阅读(3698)  评论(1编辑  收藏  举报

导航