一、    关于 Puppet
1. 什么是 Puppet
puppet 是一种Linux、Unix平台的集中配置管理系统,使用自有的puppet描述语言,可管理配置文件、用户、cron任务、软件包、系统服务等。 puppet把这些系统实体称之为资源,puppet的设计目标是简化对这些资源的管理以及妥善处理资源间的依赖关系。
puppet 采用C/S星状的结构,所有的客户端和一个或几个服务器交互。每个客户端周期的(默认半个小时)向服务器发送请求,获得其最新的配置信息,保证和该配置信 息同步。每个puppet客户端每半小时(可以设置runinterval=30)连接一次服务器端,下载最新的配置文件,并且严格按照配置文件来配置服 务器. 配置完成以后,puppet客户端可以反馈给服务器端一个消息. 如果出错,也会给服务器端反馈一个消息.
2. 为什么要使用 puppet
当你去管理10台服务器,你肯定会说小意思。没有任何压力。
当你去管理100台服务器,你肯定也会说小意思。
当你去管理1000+台服务器呢?你是不是就头痛了,不同的机器,不同的系统,
使用不同的软件版本,配置也不一样。这样为了提升效率。Puppet就派上了大用场。
3. Puppet 架构
1.png

 

4. 简单地说下工作原理:
Puppet 后台运行的时候默认是半小时执行一次,不是很方便修改。可以考虑不让它
在后台跑而是使用crontab来调用。这样可以精确控制每台客户端的执行时间。分散
执行时间也可以减轻压力
Puppet 的工作细节分成如下几个步骤:
1、客户端puppetd调用facter,facter会探测出这台主机的一些变量如主机名、内
存大小、IP 地址等。然后puppetd把这些信息发送到服务器端。
2、服务器端的puppetmaster检测到客户端的主机名,然后会到manifest里面对应
的node 配置,然后对这段内容进行解析,facter送过来的信息可以作为变量进行处
理的,node 牵涉到的代码才解析,其它的代码不不解析,解析分几个过程:语法检
查、然后会生成一个中间的伪代码,然后再把伪代码发给客户机。
3、客户端接收到伪代码之后就会执行,客户端再把执行结果发送给服务器。
4、服务器再把客户端的执行结果写入日志。
二、   主从服务器安装Puppet (中心端和客户端相同)
1、    更改hostnam

 

#cat/etc/sysconfig/network

 

 
  
  1. NETWORKING=yes 
  2. NETWORKING_IPV6=no 
  3. HOSTNAME=puppet.test.com 
2、    安装gcc和openssl
 
  
  1. yum  -y install *gcc* 
  2. yum  -y install openssl 
3、    安装ruby

 

 
  
  1. mkdir  -p /fgn/soft/ && cd /fgn/soft/ 
  2. wget  http://ftp.ruby-lang.org/pub/ruby/1.8/ruby-1.8.7-p334.tar.gz 
  3. tar  zxvfruby-1.8.7-p334.tar.gz 
  4. cd  ruby-1.8.7-p334 
  5. ./configure&& make && make install 
4、    需要安装的 ruby
 
   
  • base64 
  • cgi 
  • digest/md5 
  • etc 
  • fileutils 
  • ipaddr 
  • openssl 
  • strscan 
  • syslog 
  • uri 
  • ebrick 
  • webrick/https 
  • xmlrpc/client

 

 
  
  1. for i in base64cgi digest/md5 etc fileutils ipaddr openssl strscan syslog uri webrick webrick/httpsxmlrpc/client 
  2. do 
  3. /usr/local/bin/ruby-r$i -e "puts:installed" 
  4. done 
5、    安装facter

 

 
  
  1. cd .. 
  2. wget http://downloads.puppetlabs.com/facter/facter-1.5.8.tar.gz 
  3. tar  zxvf  facter-1.5.8.tar.gz 
  4. cd  facter-1.5.8 
  5. ruby  install.rb 
6、    安装puppet

 

 
  
  1. cd .. 
  2. wget  http://downloads.puppetlabs.com/puppet/puppet-2.6.7.tar.gz 
  3. tar  zxvf  puppet-2.6.7.tar.gz 
  4. cd puppet-2.6.7 
  5. ruby  install.rb  --full --bindir=/usr/bin  --sbindir=/usr/sbin 
puppet 中心端配置:
 
 
  
  1. if [ -e/etc/SuSE-release ]; then 
  2. cp  conf/suse/server.init  /etc/init.d/puppetmasterd 
  3. else 
  4. cp  conf/redhat/server.init  /etc/init.d/puppetmasterd 
  5. fi 
  6. groupadd  puppet 
  7. useradd -g puppetpuppet -M 
  8. chmod +x/etc/init.d/puppetmasterd 
  9. mkdir  -p  /var/lib/puppet/rrd 
  10. chown  puppet:puppet /var/lib/puppet/rrd/ 
  11. mkdir  -p  /var/run/puppet/ 
  12. chown  puppet:puppet  /var/run/puppet/ 
  13. chkconfig  --add  puppetmasterd 
  14. chkconfig  puppetmasterd on 
  15. /etc/init.d/puppetmasterdstart 
客户端配置:
 
 
  
  1. if [ -e/etc/SuSE-release ]; then 
  2. cp conf/suse/client.init /etc/init.d/puppetd 
  3. else 
  4. cp conf/redhat/client.init /etc/init.d/puppetd 
  5. fi 
  6.  
  7. cat  <<EOF> /etc/puppet/puppet.conf 
  8. [main] 
  9. ssl_client_header =SSL_CLIENT_S_DN 
  10. ssl_client_verify_headerSSL_CLIENT_VERIFY 
  11. [agent] 
  12. listen = true 
  13. report = true 
  14. show_diff=true 
  15. runinterval = 300 
  16. server = puppet.test.com 
  17. ca_port = 8141 
  18. EOF 
  19.  
  20. cat<<EOF> /etc/puppet/namespaceauth.conf 
  21. [puppetrunner] 
  22. allow cloudcenter.test.net 
  23. EOF 
  24.  
  25. chmod +x/etc/init.d/puppetd 
  26. chkconfig  --add  puppetd 
  27. chkconfig  puppet on 
  28. ln -sf/usr/local/sbin/puppetd  /usr/sbin/puppetd 
  29. /etc/init.d/puppetd restart 
  30. echo "192.168.0.1     puppet.test.com   puppet">> /etc/hosts //IP为中心端地址 

 

三、   配置中心端
1、    puppet 结构
|-- puppet.conf #主配置配置文件
|-- fileserver.conf#文件服务器配置文件
|-- auth.conf #认证配置文件
|-- autosign.conf #自动验证配置文件
|-- tagmail.conf #邮件配置文件(将错误信息发送)
|-- manifests #文件存储目录(puppet 会先读取该目录的.PP 文件<site.pp>)
| |--nodes
| ||--puppetclient.pp
| |-- site.pp #定义puppet相关的变量和默认配置。
| |-- modules.pp #加载class类模块文件(include syslog)
|-- modules #定义模块
| |-- syslog #以syslog为例
| |-- file
| |-- manifests
| | |-- init.pp
| |-- templates #模块配置目录
| | |-- syslog.erb#erb 模板

 

2、    配置文件

 

 
  
  1. cat<<EOF>/etc/puppet/auth.conf 
  2. path / 
  3. auth no 
  4. allow * 
  5. EOF 
  6.  
  7. cat<<EOF>/etc/puppet/autosign.conf 
  8. *.test.net 
  9. EOF 
  10.  
  11. cat <<EOF>/etc/puppet/fileserver.conf 
  12. [files] 
  13. path/etc/puppet/manifests/files 
  14. allow * 
  15. [moudles] 
  16. path/etc/puppet/modules 
  17. allow *.test.net 
  18. EOF 
  19.  
  20. cat<<EOF> /etc/puppet/puppet.conf 
  21. [main] 
  22. ssl_client_header = SSL_CLIENT_S_DN 
  23. ssl_client_verify_header = SSL_CLIENT_VERIFY 
  24. [master] 
  25. fileserverconfig = /etc/puppet/fileserver.conf 
  26. reports = http 
  27. reporturl = http://192.168.0.1:4000/reports 
  28. masterlog = /var/lib/puppet/log/puppetmaster.log 
  29. logdir = /var/lib/puppet/log 
  30. puppetdlog = /var/lib/puppet/log/puppetd.log 
  31. EOF 
  32.  
  33. echo "err:xiangbo.huang@test.net" > /etc/puppet/tagmail.conf 
  34. mkdir /etc/puppet/modules 
四、   用nginx来代理puppetmaster, 支持更多的客户端访问
1、    工作原理图
2.png
优点

 

*性能:nginx因为精简,运行起来非常快速,许多人声称它的比pound更高效。
*日志,调试:在这两个方面,nginx比pound更简洁。
*灵活性:nginx的处理SSL客户端验证是在应用层上实现的,而不会终止SSL连接。
*nginx可以拿来即用, 不需要像pound打补丁,同时配置的语法也很直观。

 

缺点

 

*一但在服务端使用puppetca进行sgin以后,无法主动在服务端撤销授权,
*不过你可以在客户端删除ssl目录来取消授权,一般情况下没什么影响。

 

2、    安装rubygem
 
  
  1. cd /fgn/soft/ 
  2. wget http://production.cf.rubygems.org/rubygems/rubygems-1.6.2.tgz 
  3. tar zxvf rubygems-1.6.2.tgz 
  4. cd rubygems-1.6.2 
  5. ruby setup.rb 
  6. gem installmongrel 
3、    安装nginx和配置

 

 
  
  1. cd /fgn/soft/ 
  2. wget http://nginx.org/download/nginx-1.0.12.tar.gz 
  3. tar zxvfnginx-1.0.12.tar.gz 
  4. cd nginx-1.0.12 
  5. ./configure--with-http_stub_status_module --with-http_ssl_module 
  6. make  && make install 
  7. useradd daemon 

 

主服务器代理配置
cat /usr/local/nginx/conf/nginx.conf
 
  
  1. user  daemon daemon; 
  2. worker_processes  4; 
  3. worker_rlimit_nofile 65535; 
  4.  
  5. error_log       /var/log/nginx-puppet.log notice; 
  6. pid             /var/run/nginx-puppet.pid; 
  7.  
  8. events { 
  9.     use                 epoll; 
  10.     worker_connections  32768; 
  11.  
  12.  http { 
  13.   sendfile           on; 
  14.   tcp_nopush         on; 
  15.  
  16.   keepalive_timeout  300; 
  17.   tcp_nodelay        on; 
  18.  
  19.    ssl                     on; 
  20.    ssl_session_timeout     5m; 
  21.    ssl_certificate         /etc/puppet/ssl/certs/puppet.test.com.pem; 
  22.    ssl_certificate_key     /etc/puppet/ssl/private_keys/puppet.test.com.pem; 
  23.    ssl_client_certificate  /etc/puppet/ssl/ca/ca_crt.pem; 
  24.    ssl_crl                 /etc/puppet/ssl/ca/ca_crl.pem; 
  25.    ssl_ciphers             SSLv2:-LOW:-EXPORT:RC4+RSA; 
  26.    ssl_session_cache       shared:SSL:8m; 
  27.  
  28.   upstream puppetmaster { 
  29.         server 127.0.0.1:18140; 
  30.         server 127.0.0.1:18141; 
  31.         server 127.0.0.1:18142; 
  32.         server 127.0.0.1:18143; 
  33.   } 
  34.  
  35.   upstream dashboard { 
  36.     server 127.0.0.1:4000; 
  37.   } 
  38.  
  39.      log_format download '$remote_addr, $http_x_forwarded_for $remote_user [$time_local] $request_time $host "$request_method $request_uri $server_protocol" $status - $body_bytes_sent $bytes_sent $sent_http_content_length  "$sent_http_content_Range"   "$http_referer" "$http_user_agent" $sent_http_x_cache $sent_http_content_type' " up_addr:$upstream_addr" " up_resp:$upstream_response_time" "s" " up_status:$upstream_status" ; 
  40.  
  41.     access_log logs/access.log download; 
  42.  
  43.  #+--------------------------------------------------------------------------------------------+ 
  44.  
  45.     server { 
  46.  
  47.     listen 8140; 
  48.     server_name puppet.test.com; 
  49.     ssl_verify_client       on; 
  50.     root                    /etc/puppet; 
  51.  
  52.     # Ask the puppetmaster for everything else 
  53.  
  54.         # File sections 
  55.         location /production/file_content/files/ { 
  56.                 types { } 
  57.                 default_type application/x-raw; 
  58.                 alias /etc/puppet/manifests/files/; 
  59.         } 
  60.  
  61.         # Modules files sections 
  62. location ~ /production/file_content/modules/.+/ { 
  63.                 root /etc/puppet/modules; 
  64.                 types { } 
  65.                 default_type application/x-raw; 
  66.                 rewrite ^/production/file_content/modules/([^/]+)/(.+)$ /$1/files/$2 break; 
  67.         } 
  68.  
  69.          location / { 
  70.             proxy_pass          http://puppetmaster; 
  71.             proxy_redirect      off; 
  72.             proxy_set_header    Host             $host; 
  73.             proxy_set_header    X-Real-IP        $remote_addr; 
  74.             proxy_set_header    X-Forwarded-For  $proxy_add_x_forwarded_for; 
  75.             proxy_set_header    X-Client-Verify  SUCCESS; 
  76.             proxy_set_header    X-Client-DN      $ssl_client_s_dn; 
  77.             proxy_set_header    X-SSL-Subject    $ssl_client_s_dn; 
  78.             proxy_set_header    X-SSL-Issuer     $ssl_client_i_dn; 
  79.             proxy_read_timeout  65; 
  80.         } 
  81.  
  82.   }#server end 
  83.  
  84.      server { 
  85.         listen                  8141; 
  86.         ssl_verify_client       off; 
  87.         root                    /etc/puppet; 
  88.         access_log              /usr/local/nginx/logs/access-8141.log download; 
  89.  
  90.         # File sections 
  91.         location /production/file_content/files/ { 
  92.                 types { } 
  93.                 default_type application/x-raw; 
  94.                 alias /etc/puppet/manifests/files/; 
  95.         } 
  96.  
  97.          # Modules files sections 
  98. location ~ /production/file_content/modules/.+/ { 
  99.                 root /etc/puppet/modules; 
  100.                 types { } 
  101.                 default_type application/x-raw; 
  102.                 rewrite ^/production/file_content/modules/([^/]+)/(.+)$ /$1/files/$2 break; 
  103.         } 
  104.  
  105.          location / { 
  106.             proxy_pass  http://puppetmaster; 
  107.             proxy_redirect     off; 
  108.             proxy_set_header   Host             $host; 
  109.             proxy_set_header   X-Real-IP        $remote_addr; 
  110.             proxy_set_header   X-Forwarded-For  $proxy_add_x_forwarded_for; 
  111.             proxy_set_header   X-Client-Verify  FAILURE; 
  112.             proxy_set_header   X-Client-DN      $ssl_client_s_dn; 
  113.             proxy_set_header   X-SSL-Subject    $ssl_client_s_dn; 
  114.             proxy_set_header   X-SSL-Issuer     $ssl_client_i_dn; 
  115.             proxy_read_timeout  65; 
  116.         } 
  117.     } 
  118. }#http end 
*注意:puppet.test.com 部分为hostname

 

4、    配置puppetmaster让它启动多个端口支持
cat /etc/sysconfig/puppetmaster
 
  
  1. # Location of the main manifest 
  2. #PUPPETMASTER_MANIFEST=/etc/puppet/manifests/site.pp 
  3.  
  4.  # Where to log general messages to. 
  5. # Specify syslog to send log messages to the system log. 
  6. PUPPETMASTER_LOG=/var/log/puppet/puppetmaster.log 
  7.  
  8. PUPPETMASTER_PORTS=( 18140 18141 18142 18143 ) 
  9. PUPPETMASTER_EXTRA_OPTS="--servertype=mongrel  --ssl_client_header=HTTP_X_SSL_SUBJECT" 
  10. # You may specify other parameters to the puppetmaster here 
  11. #PUPPETMASTER_EXTRA_OPTS=--noca 
重启puppetmasterd和nginx

 

 
  
  1. /etc/init.d/puppetmasterdrestart 
  2. /usr/local/nginx/sbin/nginx 

 

5、    验证

 

配置site.pp
 
 
  
  1. cat<<EOF> /etc/puppet/manifests/site.pp 
  2. node default { 
  3. file {"/tmp/temp1.txt": content => "hello,first puppetmanifest"; } 
  4. EOF 

 

客户端运行:
 
 
  
  1. puppetd --test --serverpuppet.test.com