mr923 发表于 2018-8-2 12:59:31

Puppet系列之四:Puppet利用Nginx多端口实现负载均衡

  1 概述
  当代理服务器数量增加时,单台puppetmaster压力增大,会出现解析缓慢,甚至出现“time out”之类报错的错误。Nginx工作于网络的七层之上,可以作为负载均衡服务器。利用Load Balancing可以优化上述出现的问题。
  2 服务器环境及软件版本
  server version:CentOS6.5 i386
  Ruby:ruby-1.8.7.352-13.el6.i686
  Puppet: puppet-2.7.23
  Nginx: nginx-1.4.4
  
  3 安装Mongrel
  
  通过指定mongrel类型来使用puppet多端口配置
  yum install -y rubygem-mongrel
  4 配置puppetmaster
  vim /etc/sysconfig/puppetmaster,添加以下两行,分别代表多端口、mongrel类型
  PUPPETMASTER_PORTS=(8141 8142 8143 8144 8145 )
  PUPPETMASTER_EXTRA_OPTS="--servertype=mongrel --ssl_client_header=HTTP_X_SSL_SUBJECT"
  
  5 安装Nginx服务
  安装之前请确保系统已经安装pcre-devel正则库,然后再编译安装Nginx,需要添加SSL模块参数支持,Nginx的安装过程如下所示:
  yum -y install pcre-devel
  cd $hd
  wget http://nginx.org/download/nginx-1.4.4.tar.gz
  tar zxvf nginx-1.4.4.tar.gz
  cd nginx-1.4.4
  ./configure --prefix=/usr/local/nginx --with-http_ssl_module
  make && make install
  添加www用户组及用户:
  groupadd www
  useradd -g www www
  6 配置Nginx
  vim /usr/local/nginx/conf/nginx.conf:
  user www;
  worker_processes 8;
  events{
  worker_connections 65535;
  }
  http{
  include      mime.types;
  default_type application/octet-stream;
  #定义puppet客户端访问puppet-server端日志格式
  log_format main'$remote_addr - $remote_user [$time_local] "$request"$request_length $request_time $time_local'
  '$status$body_bytes_sent $bytes_sent $connection $msec "$http_referer"'      '"$http_user_agent""$http_x_forwarded_for" upstream_response_time$upstream_addr $upstream_status ';
  access_log /usr/local/nginx/logs/access.logmain;
  sendfile       on;
  tcp_nopush    on;
  keepalive_timeout 65;
  upstreampuppetmaster {
  server127.0.0.1:8141;
  server127.0.0.1:8142;
  server127.0.0.1:8143;
  server127.0.0.1:8144;
  server127.0.0.1:8145;
  }
  server{
  listen      8140;
  root /etc/puppet;
  ssl on;
  ssl_session_timeout 5m;
  #如下为puppemaster服务器端证书地址
  ssl_certificate /var/lib/puppet/ssl/certs/rango.fugue.com.pem;
  ssl_certificate_key /var/lib/puppet/ssl/private_keys/rango.fugue.com.pem;
  ssl_client_certificate /var/lib/puppet/ssl/ca/ca_crt.pem;
  ssl_crl /var/lib/puppet/ssl/ca/ca_crl.pem;
  ssl_verify_client optional;
  #File sections
  location /production/file_content/files/ {
  types{ }
  default_type application/x-raw;
  #定义puppet推送路径别名
  alias /etc/puppet/files/;
  }
  #Modules files sections
  location ~ /production/file_content/modules/.+/ {
  root /etc/puppet/modules;
  types{ }
  default_type application/x-raw;
  rewrite ^/production/file_content/modules/(.+)/(.+)$ /$1/files/$2 break; }
  location/ {
  ##设置跳转到puppetmaster负载均衡
  proxy_pass http://puppetmaster;
  proxy_redirect off;
  proxy_set_header Host $host;
  proxy_set_header X-Real-IP $remote_addr;
  proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
  proxy_set_header X-Client-Verify $ssl_client_verify;
  proxy_set_header X-SSL-Subject $ssl_client_s_dn;
  proxy_set_header X-SSL-Issuer $ssl_client_i_dn;
  proxy_buffer_size 10m;
  proxy_buffers 1024 10m;
  proxy_busy_buffers_size 10m;
  proxy_temp_file_write_size 10m;
  proxy_read_timeout 120;
  }
  }
  }
  7 启动Nginx及puppet-server
  7.1 首先关闭puppetmaster进程:/etc/init.d/puppetmaster stop
  7.2 启动Nginx
  /usr/local/nginx/sbin/nginx
  nginx占用puppetmaster默认的8140端口后,用如下命令来检查8140端口是否被nginx接管:
  lsof-i:8140
  此命令显示结果表明8140被nginx进程接管:

  COMMANDPID USER   FD   TYPEDEVICE>  nginx12433 root    6uIPv4 1309402      0t0TCP *:8140 (LISTEN)
  nginx12434www    6uIPv4 1309402      0t0TCP *:8140 (LISTEN)
  nginx12435www    6uIPv4 1309402      0t0TCP *:8140 (LISTEN)
  nginx12436www    6uIPv4 1309402      0t0TCP *:8140 (LISTEN)
  nginx12437www    6uIPv4 1309402      0t0TCP *:8140 (LISTEN)
  nginx12438www    6uIPv4 1309402      0t0TCP *:8140 (LISTEN)
  nginx12439www    6uIPv4 1309402      0t0TCP *:8140 (LISTEN)
  nginx12440www    6uIPv4 1309402      0t0TCP *:8140 (LISTEN)
  nginx12441www    6uIPv4 1309402      0t0TCP *:8140 (LISTEN)
  7.3 启动puppetmaster
  /etc/init.d/puppetmaster start
  Starting puppetmaster:
  Port:8141                                                
  Port:8142                                                
  Port:8143                                                
  Port:8144                                                
  Port:8145                                                
  8 总结
  本文旨在讲述通过基于网络七层的软件负载均衡技术Nginx,结合puppet的多端口设置来构建puppet的load balancing网络,达到优化puppet吞吐,缓解解析压力的目的。后续文章将着力于puppet report相关的内容。
  ——RangoChen
页: [1]
查看完整版本: Puppet系列之四:Puppet利用Nginx多端口实现负载均衡