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

简介:

随着公司应用需求的增加,需要不断的扩展,服务器数量也随之增加,当服务器数量不断增加,我们会发现一台puppetmaster压力大,解析缓慢,而且时不时出“Timeout”之类的报错,那这时有什么优化的办法吗?我们在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类型,默认没有安装,需要安装:

1
yum  install  -yrubygem-mongrel

3.配置puppetmaster

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

1
2
PUPPETMASTER_PORTS=(8141 8142 8143 81448145)
PUPPETMASTER_EXTRA_OPTS= "--servertype=mongrel--ssl_client_header=HTTP_X_SSL_SUBJECT"

4.安装Nginx服务

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

1
2
3
4
5
6
7
yum -y  install  pcre-devel
cd  /usr/local/src
wgethttp: //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用户组及用户,命令如下所示:

1
2
groupadd www
useradd  -g www www

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
user www;
worker_processes    8;
events {
        worker_connections    65535;
}
http {
        include             mime.types;
        default_type   application /octet-stream ;
        sendfile                on;
        tcp_nopush         on;
        keepalive_timeout    65;
        #定义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 .log   main;
        upstream puppetmaster {
                 server 127.0.0.1:8141;
                 server 127.0.0.1:8142;
                 server 127.0.0.1:8143;
                 server 127.0.0.1:8144;
                 server 127.0.0.1:8145;
                 }
        server {
        listen 8140;
        root  /etc/puppet ;
        ssl on;
        ssl_session_timeout 5m;
        #如下为puppetmaster服务器端证书地址
        ssl_certificate  /var/lib/puppet/ssl/certs/server .cn7788.com.pem;
        ssl_certificate_key /var/lib/puppet/ssl/private_keys/server .cn7788.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;
     }
}
}

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

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

1
/usr/local/nginx/sbin/nginx

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

1
lsof  -i:8140

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

1
2
3
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,命令如下所示:

1
service puppetmaster start

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

1
2
3
4
5
6
7
8
9
10
11
Starting puppetmaster:
Port: 8141** Ruby version is not up-to- date ;loading cgi_multipart_eof_fix
                                                                                                                                                                                                                                         [        OK        ]
Port: 8142** Ruby version is notup-to- date ; loading cgi_multipart_eof_fix
                                                                                                                                                                                                                                       [        OK        ]
Port: 8143** Ruby version is notup-to- date ; loading cgi_multipart_eof_fix
                                                                                                                                                                                                                                         [        OK        ]
Port: 8144** Ruby version is notup-to- date ; loading cgi_multipart_eof_fix
                                                                                                                                                                                                                                        [        OK        ]
Port: 8145** Ruby version is notup-to- date ; loading cgi_multipart_eof_fix
                                                                                                                                                                                                                                       [        OK        ]

这段警告值的意思为

1
2
It's just a warning. Mongrel wants a Rubyversion of at least 1.8.6.
But it still runs just fine with previousversions. Just ignore the warning.

翻译为中文的意思是:

Mongrel需要ruby至少是1.8.6以上的版本,但它仍然在当前版本运行,请忽咯当前警告,为了保证整个puppet运行环境的稳定,我这里选择还是沿用1.8.5版本的ruby,大家有任何疑问,欢迎随时通过此技术博客或电子邮箱yuhongchun027@163.com与抚琴煮酒进行交流。









本文转自 抚琴煮酒 51CTO博客,原文链接:http://blog.51cto.com/yuhongchun/1291908,如需转载请自行联系原作者
相关实践学习
部署高可用架构
本场景主要介绍如何使用云服务器ECS、负载均衡SLB、云数据库RDS和数据传输服务产品来部署多可用区高可用架构。
负载均衡入门与产品使用指南
负载均衡(Server Load Balancer)是对多台云服务器进行流量分发的负载均衡服务,可以通过流量分发扩展应用系统对外的服务能力,通过消除单点故障提升应用系统的可用性。 本课程主要介绍负载均衡的相关技术以及阿里云负载均衡产品的使用方法。
目录
相关文章
|
4天前
|
应用服务中间件 网络安全 nginx
【Nginx】Nginx启动显示80端口占用问题的解决方案
【Nginx】Nginx启动显示80端口占用问题的解决方案
155 1
|
2天前
|
负载均衡 应用服务中间件 nginx
解决nginx配置负载均衡时invalid host in upstream报错
在Windows环境下,配置Nginx 1.11.5进行负载均衡时遇到问题,服务无法启动。错误日志显示“invalid host in upstream”。检查发现上游服务器列表中,192.168.29.128的主机地址无效。负载均衡配置中,两个服务器地址前误加了"http://"。修正方法是删除上游服务器列表和proxy_pass中的"http://"。问题解决后,Nginx服务应能正常启动。
32 4
解决nginx配置负载均衡时invalid host in upstream报错
|
4天前
|
负载均衡 网络协议 应用服务中间件
【亮剑】在Linux中构建高可用性和高性能网络服务的负载均衡工具HAProxy、Nginx和Keepalived。
【4月更文挑战第30天】本文介绍了在Linux中构建高可用性和高性能网络服务的负载均衡工具HAProxy、Nginx和Keepalived。HAProxy是一个高性能的开源TCP和HTTP负载均衡器,适合处理大量并发连接;Nginx是一个多功能Web服务器和反向代理,支持HTTP、HTTPS和TCP负载均衡,同时提供缓存和SSL功能;Keepalived用于监控和故障切换,通过VRRP实现IP热备份,保证服务连续性。文中详细阐述了如何配置这三个工具实现负载均衡,包括安装、配置文件修改和启动服务,为构建可靠的负载均衡系统提供了指导。
|
4天前
|
负载均衡 算法 网络协议
LVS、Nginx和HAProxy负载均衡器对比总结
LVS、Nginx和HAProxy负载均衡器对比总结
|
4天前
|
负载均衡 应用服务中间件 PHP
使用nginx-haproxy实现七层负载均衡
【4月更文挑战第13天】使用nginx实现动静分离的负载均衡集群
38 4
|
4天前
|
负载均衡 应用服务中间件 nginx
Nginx 负载均衡
Nginx 负载均衡
26 2
|
4天前
|
负载均衡 算法 应用服务中间件
解密Nginx负载均衡:实现流量分发与故障转移
解密Nginx负载均衡:实现流量分发与故障转移
29 1
|
4天前
|
缓存 负载均衡 应用服务中间件
nginx的各种负载均衡策略与各种负载均衡策略如何配置
Nginx支持多种负载均衡策略,如轮询、加权轮询、IP哈希、最少连接、URL哈希和fair策略。轮询是默认策略,每个请求按顺序分发;加权轮询根据权重分配请求;IP哈希确保相同IP的请求始终发送到同一服务器;最少连接将请求发送给连接数最少的服务器;URL哈希(需额外工具或模块)和fair策略则依据URL和响应时间分配请求。配置变更需更新nginx.conf并重新加载或重启服务,具体配置应参照官方文档。
49 0
|
安全 Linux 网络协议
puppet yum模块、配置仓储、mount模块
转载:http://blog.51cto.com/ywzhou/1577335 作用:自动为客户端配置YUM源,为使用yum安装软件包提供便捷。 1、服务端配置yum模块 (1)模块清单 [root@puppet ~]# tree /etc/puppe...
1072 0
|
网络协议 安全 网络安全

推荐镜像

更多