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客戶端了。

相關文章
相關標籤/搜索