Puppet扩展篇5-通过多进程增强master的负载均衡能力(nginx+mongrel)

当puppetmaster管理的主机越来越多时,puppetmaster本身性能会存在性能瓶颈问题,除了增加服务器扩充puppetmaster的数量增加puppetmaster整体性能外,也可以通过单台扩充puppetmaster的进程数来增加puppetmaster的性能。

以下是通过nginx+mongrel负载均衡puppetmaster的进程,由nginx向所有puppetagent提供认证服务,除此之外的其他puppetmaster功能的实现由nginx转向puppetmaster其中一个进程去处理即可。而nginx的upstream字段里面所包含的地址填写为127.0.0.1指向puppetmaster进程,提高了安全性。

备注:nginx+mongrel只支持puppet2.7之前版本(包括2.7版本在内)。

1、安装相关软件包

  1. [root@puppetserver yum.repos.d]# yum install rubygem-mongrel nginx

2、增加puppet端口

  1. [root@puppetserver yum.repos.d]# vim /etc/sysconfig/puppetmaster
  2. PUPPETMASTER_PORTS=( 18140 18141 18142 18143 )
  3. PUPPETMASTER_EXTRA_OPTS="--servertype=mongrel --ssl_client_header=HTTP_X_SSL_SUBJECT"

3、配置nginx服务

添加upstream字段,注意ssl认证证书的路径

  1. [root@puppetserver nginx]# vim nginx.conf
  2. user nginx nginx;
  3. worker_processes 4;
  4. error_log /var/log/puppet/nginx-puppet.log notice;
  5. pid /var/run/nginx.pid;
  6. events {
  7. worker_connections 1024;
  8. }
  9. http {
  10. default_type application/octet-stream;
  11. sendfile on;
  12. tcp_nopush on;
  13. keepalive_timeout 65;
  14. tcp_nodelay on;
  15. large_client_header_buffers 16 4k;
  16. proxy_buffers 128 4k;
  17. upstream puppetmaster {
  18. server 127.0.0.1:18140;
  19. server 127.0.0.1:18141;
  20. server 127.0.0.1:18142;
  21. server 127.0.0.1:18143;
  22. }
  23. server {
  24. listen 8140;
  25. root /etc/puppet;
  26. ssl on;
  27. ssl_session_timeout 5m;
  28. ssl_certificate /var/lib/puppet/ssl/certs/puppetserver.kisspuppet.com.pem;
  29. ssl_certificate_key /var/lib/puppet/ssl/private_keys/puppetserver.kisspuppet.com.pem;
  30. ssl_client_certificate /var/lib/puppet/ssl/ca/ca_crt.pem;
  31. ssl_crl /var/lib/puppet/ssl/ca/ca_crl.pem;
  32. ssl_ciphers SSLv2:-LOW:-EXPORT:RC4+RSA;
  33. ssl_verify_client optional;
  34. location / {
  35. proxy_pass http://puppetmaster;
  36. proxy_redirect off;
  37. proxy_set_header Host $host;
  38. proxy_set_header X-Real-IP $remote_addr;
  39. proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
  40. proxy_set_header X-Client-Verify $ssl_client_verify;
  41. proxy_set_header X-Client-DN $ssl_client_s_dn;
  42. proxy_set_header X-SSL-Subject $ssl_client_s_dn;
  43. proxy_set_header X-SSL-Issuer $ssl_client_i_dn;
  44. proxy_read_timeout 65;
  45. }
  46. }
  47. }

4、分别启动nginx服务和puppetmaster服务

  1. [root@puppetserver1poc ~]# /etc/rc.d/init.d/nginx restart
  2. Stopping nginx: [FAILED]
  3. Starting nginx: [ OK ]
  4. [root@puppetserver1poc ~]# /etc/rc.d/init.d/puppetmaster start
  5. Starting puppetmaster:
  6. Port: 18140 [ OK ]
  7. Port: 18141 [ OK ]
  8. Port: 18142 [ OK ]
  9. Port: 18143 [ OK ]

5、查看监听端口

  1. [root@puppetserver1poc ~]# netstat -nlp | grep 814
  2. tcp 0 0 0.0.0.0:8140 0.0.0.0:* LISTEN 6224/nginx
  3. tcp 0 0 127.0.0.1:18140 0.0.0.0:* LISTEN 6271/ruby
  4. tcp 0 0 127.0.0.1:18141 0.0.0.0:* LISTEN 6312/ruby
  5. tcp 0 0 127.0.0.1:18142 0.0.0.0:* LISTEN 6351/ruby
  6. tcp 0 0 127.0.0.1:18143 0.0.0.0:* LISTEN 6390/ruby
  7. 3.5.6 通过进程查看运行状况
  8. [root@puppetserver1poc ~]# ps -ef | grep ruby
  9. puppet 5422 1 1 13:58 ? 00:00:22 /usr/bin/ruby /usr/sbin/puppetmasterd
  10. root 6431 1 0 14:10 ? 00:00:01 ruby /usr/sbin/mcollectived --pid=/var/run/mcollectived.pid --config=/etc/mcollective/server.cfg
  11. puppet 7139 1 0 14:25 ? 00:00:00 /usr/bin/ruby /usr/sbin/puppetmasterd --servertype=mongrel --servertype=mongrel --ssl_client_header=HTTP_X_SSL_SUBJECT --masterport=18140 --pidfile=/var/run/puppet/puppetmaster.18140.pid
  12. puppet 7171 1 0 14:25 ? 00:00:00 /usr/bin/ruby /usr/sbin/puppetmasterd --servertype=mongrel --servertype=mongrel --ssl_client_header=HTTP_X_SSL_SUBJECT --masterport=18141 --pidfile=/var/run/puppet/puppetmaster.18141.pid
  13. puppet 7203 1 0 14:25 ? 00:00:00 /usr/bin/ruby /usr/sbin/puppetmasterd --servertype=mongrel --servertype=mongrel --ssl_client_header=HTTP_X_SSL_SUBJECT --masterport=18142 --pidfile=/var/run/puppet/puppetmaster.18142.pid
  14. puppet 7235 1 0 14:25 ? 00:00:00 /usr/bin/ruby /usr/sbin/puppetmasterd --servertype=mongrel --servertype=mongrel --ssl_client_header=HTTP_X_SSL_SUBJECT --masterport=18143 --pidfile=/var/run/puppet/puppetmaster.18143.pid
  15. root 7243 3858 0 14:26 pts/3 00:00:00 grep ruby

6、通过日志查看运行状况

  1. [root@puppetserver1poc nodes]# tailf /var/log/nginx/access.log
  2. 192.168.100.127 - - [25/Nov/2013:16:42:49 +0800] "POST /production/catalog/agent2.kisspuppet.com HTTP/1.1" 200 570 "-" "-"
  3. 192.168.100.127 - - [25/Nov/2013:16:42:52 +0800] "PUT /production/report/agent2.kisspuppet.com HTTP/1.1" 200 58 "-" "-"
  4. 192.168.100.126 - - [25/Nov/2013:16:42:54 +0800] "GET /production/file_metadatas/plugins?links=manage&checksum_type=md5&&ignore=---+%0A++-+%22.svn%22%0A++-+CVS%0A++-+%22.git%22&recurse=true HTTP/1.1" 404 56 "-" "-"
  5. 192.168.100.126 - - [25/Nov/2013:16:42:54 +0800] "GET /production/file_metadata/plugins? HTTP/1.1" 404 36 "-" "-"
  6. 192.168.100.126 - - [25/Nov/2013:16:42:55 +0800] "POST /production/catalog/agent1.kisspuppet.com HTTP/1.1" 200 570 "-" "-"
  7. 192.168.100.126 - - [25/Nov/2013:16:42:58 +0800] "PUT /production/report/agent1.kisspuppet.com HTTP/1.1" 200 58 "-" "-"
  8. 192.168.100.125 - - [25/Nov/2013:16:43:07 +0800] "GET