Puppet利用Nginx多端口实现负载均衡

原创
运维 系统运维
随着公司应用需求的增加,需要不断的扩展,服务器数量也随之增加,当服务器数量不断增加,我们会发现一台puppetmaster压力大,解析缓慢,而且时不时出现"time out"之类的报错,那这时有什么优化的办法吗?其实不然,我们可以利用Nginx多端口实现负载均衡,这样在很大程度上优化了puppet的处理能力。

  【51CTO原创稿件】随着公司应用需求的增加,需要不断的扩展,服务器数量也随之增加,当服务器数量不断增加,我们会发现一台puppetmaster压力大,解析缓慢,而且时不时出现"time out"之类的报错,那这时有什么优化的办法吗?我们在Puppet官网上找寻解决方案,发现puppetmaster可以配置多端口,结合WEB代理(推荐Nginx),这样puppetmaster承受能力至少可以提升数倍以上,相当于在很大程度上优化了puppet的处理能力。

  1.遵循前面的环境设定,我们这里的服务器环境及软件版本分别为:

  服务器系统:CentOS5.8 x86_64

  Ruby版本:ruby-1.8.5

  Puppet版本:puppet-2.7.9

  Nginx版本:nginx-0.8.46

  2.Mongrel安装

  要使用puppet多端口配置,需要指定mongrel类型,默认没有安装,需要安装:

  yum install -y rubygem-mongrel

  3.配置puppetmaster

  在/etc/sysconfig/puppetmaster文件末尾添加如下两行,分别代表多端口、mongrel类型,内容如下所示:

  PUPPETMASTER_PORTS=(8141 8142 8143 8144 8145)

  PUPPETMASTER_EXTRA_OPTS="--servertype=mongrel --ssl_client_header=HTTP_X_SSL_SUBJECT"

  4.安装Nginx服务

  安装之前请确保系统已经安装pcre-devel正则库,然后再编译安装Nginx,需要添加SSL模块参数支持,Nginx的安装过程如下所示:

  yum -y install pcre-devel

  cd /usr/local/src

  wget http://nginx.org/download/nginx-0.8.46.tar.gz

  tar zxvf nginx-0.8.46.tar.gz

  cd nginx-0.8.46

  ./configure --prefix=/usr/local/nginx --with-http_ssl_module

  make && make install && cd ../

  添加www用户组及用户,命令如下所示:

  groupadd www

  useradd -g www www

  5.我们依据puppet需求来修改配置文件nginx.conf,内容如下所示:

  1. user www; 
  2. worker_processes  8; 
  3. events { 
  4. worker_connections  65535; 
  5. http { 
  6. include       mime.types; 
  7. default_type  application/octet-stream; 
  8. sendfile        on; 
  9. tcp_nopush     on; 
  10. keepalive_timeout  65; 
  11. #定义puppet客户端访问puppet-server端日志格式 
  12. log_format main '$remote_addr - $remote_user [$time_local] "$request" $request_length $request_time $time_local' 
  13. '$status $body_bytes_sent $bytes_sent $connection $msec "$http_referer" ' 
  14. '"$http_user_agent" $http_x_forwarded_for $upstream_response_time $upstream_addr $upstream_status '; 
  15. access_log  /usr/local/nginx/logs/access.log  main; 
  16. upstream puppetmaster { 
  17. server 127.0.0.1:8141; 
  18. server 127.0.0.1:8142; 
  19. server 127.0.0.1:8143; 
  20. server 127.0.0.1:8144; 
  21. server 127.0.0.1:8145; 
  22. server { 
  23. listen 8140; 
  24. root /etc/puppet; 
  25. ssl on; 
  26. ssl_session_timeout 5m; 
  27. #如下为puppetmaster服务器端证书地址 
  28. ssl_certificate /var/lib/puppet/ssl/certs/server.cn7788.com.pem; 
  29. ssl_certificate_key /var/lib/puppet/ssl/private_keys/server.cn7788.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_verify_client optional; 
  33. #File sections 
  34. location /production/file_content/files/ { 
  35. types { } 
  36. default_type application/x-raw; 
  37. #定义puppet推送路径别名 
  38. alias /etc/puppet/files/; 
  39. # Modules files sections 
  40. location ~ /production/file_content/modules/.+/ { 
  41. root /etc/puppet/modules; 
  42. types { } 
  43. default_type application/x-raw; 
  44. rewrite ^/production/file_content/modules/(.+)/(.+)$ /$1/files/$2 break; 
  45. location / { 
  46. ##设置跳转到puppetmaster负载均衡 
  47. proxy_pass http://puppetmaster; 
  48. proxy_redirect off; 
  49. proxy_set_header Host $host; 
  50. proxy_set_header X-Real-IP $remote_addr; 
  51. proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; 
  52. proxy_set_header X-Client-Verify $ssl_client_verify; 
  53. proxy_set_header X-SSL-Subject $ssl_client_s_dn; 
  54. proxy_set_header X-SSL-Issuer $ssl_client_i_dn; 
  55. proxy_buffer_size 10m; 
  56. proxy_buffers 1024 10m; 
  57. proxy_busy_buffers_size 10m; 
  58. proxy_temp_file_write_size 10m; 
  59. proxy_read_timeout 120; 

 

  6.修改完nginx.conf文件以后,我们要启动nginx及puppet-server,这时应该如何操作呢?

  1.我们首先关闭puppetmaster进程,然后先启动nginx,不然nginx是会启动失败的,命令如下所示:

  /usr/local/nginx/sbin/nginx

  nginx占用puppetmaster默认的8140端口后,我们可以用如下命令来检查8140端口是否被nginx接管,如下所示:

  lsof -i:8140

  此命令显示结果表明8140被nginx进程接管,如下所示:

  COMMAND  PID  USER   FD   TYPE DEVICE SIZE/OFF NODE NAME

  nginx   4121  root    6u  IPv4  20668      0t0  TCP *:8140 (LISTEN)

  nginx   4122  www  6u  IPv4  20668      0t0  TCP *:8140 (LISTEN)

  我们再启动puppetmaster,命令如下所示:

  service puppetmaster start

  如果ruby版本为1.8.5的话,等会运行puppetmaster会有如下警告,如下所示:

   Starting puppetmaster:

  Port: 8141** Ruby version is not up-to-date; loading cgi_multipart_eof_fix

  [  OK  ]

  Port: 8142** Ruby version is not up-to-date; loading cgi_multipart_eof_fix

  [  OK  ]

  Port: 8143** Ruby version is not up-to-date; loading cgi_multipart_eof_fix

  [  OK  ]

  Port: 8144** Ruby version is not up-to-date; loading cgi_multipart_eof_fix

  [  OK  ]

  Port: 8145** Ruby version is not up-to-date; loading cgi_multipart_eof_fix

  [  OK  ]

  这段警告值的意思为:

  It's just a warning. Mongrel wants a Ruby version of at least 1.8.6.

  But it still runs just fine with previous versions. Just ignore the warning.

  翻译为中文的意思是:

  Mongrel需要ruby至少是1.8.6以上的版本,但它仍然在当前版本运行,请忽咯当前警告,为了保证整个puppet运行环境的稳定,我这里选择还是沿用1.8.5版本的ruby。

本文作者:余洪春(抚琴煮酒),英文名Andrew.Yu。

 个人博客地址:http://andrewyu.blog.51cto.com/

 Sina微博地址:http://weibo.com/yuhongchun027

责任编辑:黄丹 来源: 51CTO.com
相关推荐

2018-03-14 11:13:35

Web服务器Nginx

2013-04-22 11:29:14

Nginx

2012-07-31 09:25:42

nginx负载均衡反向代理

2009-07-22 10:25:37

2020-01-14 09:40:00

Nginx负载均衡正向代理

2019-11-12 13:56:15

NginxTomcat负载均衡

2018-02-01 10:31:12

Nginx负载均衡软件

2015-04-13 09:44:14

Nginxkeepalived负载均衡

2020-04-20 20:27:59

Nginx动静分离负载均衡

2010-03-24 10:35:02

Nginx负载均衡器

2019-03-13 12:04:41

Nginx负载均衡动静分离

2014-08-22 10:36:37

nginx负载均衡

2023-12-07 12:29:49

Nginx负载均衡策略

2011-12-02 22:51:46

Nginx负载均衡

2018-10-12 08:43:54

2010-05-07 12:23:23

nginx负载均衡

2011-01-07 11:14:17

Nginx负载均衡负载均衡

2014-07-28 11:37:49

NginxTomcat

2013-08-27 13:48:12

Nginx stickNginx负载均衡

2017-12-13 15:33:02

LinuxNginxTomcat
点赞
收藏

51CTO技术栈公众号