HAProxy是一款免费、快速并且可靠的一种代理解决方案,支持高可用性、负载均衡特性,同时适用于做基于TCP和HTTP的应用的代理。对于一些负载较大的Web站点,使用HAProxy特别合适。HAProxy能够支撑数以万计的并发连接。它的配置简单,能够很容易整合大我们现有的应用架构之中。
下面,我们在CentOS 6.4上进行安装配置HAProxy。
安装配置
按照如下步骤进行安装:
2 | tar xvzf haproxy-1.4.24. tar .gz |
默认安装,HAProxy对应的配置文件的存放路径为/etc/haproxy/haproxy.cfg。
我们看一下,默认安装的配置文件内容,如下所示:
01 | #--------------------------------------------------------------------- |
02 | # Example configuration for a possible web application. See the |
03 | # full configuration options online. |
07 | #--------------------------------------------------------------------- |
09 | #--------------------------------------------------------------------- |
11 | #--------------------------------------------------------------------- |
13 | # to have these messages end up in /var/log/haproxy.log you will |
16 | # 1) configure syslog to accept network log events. This is done |
17 | # by adding the '-r' option to the SYSLOGD_OPTIONS in |
18 | # /etc/sysconfig/syslog |
20 | # 2) configure local2 events to go to the /var/log/haproxy.log |
21 | # file. A line like the following can be added to |
22 | # /etc/sysconfig/syslog |
24 | # local2.* /var/log/haproxy.log |
28 | chroot /var/lib/haproxy |
29 | pidfile /var/run/haproxy.pid |
35 | # turn on stats unix socket |
36 | stats socket /var/lib/haproxy/stats |
38 | #--------------------------------------------------------------------- |
39 | # common defaults that all the 'listen' and 'backend' sections will |
40 | # use if not designated in their block |
41 | #--------------------------------------------------------------------- |
47 | option http-server-close |
48 | option forwardfor except 127.0.0.0/8 |
51 | timeout http-request 10s |
56 | timeout http-keep-alive 10s |
60 | #--------------------------------------------------------------------- |
61 | # main frontend which proxys to the backends |
62 | #--------------------------------------------------------------------- |
64 | acl url_static path_beg -i /static /images /javascript /stylesheets |
65 | acl url_static path_end -i .jpg .gif .png .css .js |
67 | use_backend static if url_static |
70 | #--------------------------------------------------------------------- |
71 | # static backend for serving up images, stylesheets and such |
72 | #--------------------------------------------------------------------- |
75 | server static 127.0.0.1:4331 check |
77 | #--------------------------------------------------------------------- |
78 | # round robin balancing between the various backends |
79 | #--------------------------------------------------------------------- |
82 | server app1 127.0.0.1:5001 check |
83 | server app2 127.0.0.1:5002 check |
84 | server app3 127.0.0.1:5003 check |
85 | server app4 127.0.0.1:5004 check |
我们对上面配置文件的内容,适当的扩展,做简单的解释:
global段用于配置进程级的参数。官网文档基于参数的功能,将global配置参数分为3组:
- 进程管理和安全
- 性能调优
- 调试
具体内容可以参考文档详细介绍。
defaults段主要是代理配置的默认配置段,设置默认参数,这些默认的配置可以在后面配置的其他段中使用。如果其他段中想修改默认的配置参数,只需要覆盖defaults段中的出现配置项内容。
关于defaults段可以配置的参数,可以参考官网文档的详细介绍。
frontend段主要配置前端监听的Socket相关的属性,也就是接收请求链接的虚拟节点。这里除了配置这些静态的属性,还可以根据一定的规则,将请求重定向到配置的backend上,backend可能配置的是一个服务器,也可能是一组服务器(集群)。
backend段主要是配置的实际服务器的信息,通过frontend配置的重定向请求,转发到backend配置的服务器上。
listen段是将frontend和backend这两段整合在一起,直接将请求从代理转发到实际的后端服务器上。
启动HAProxy代理
启动非常简单,执行如下命令即可:
1 | sudo haproxy -f /etc/haproxy/haproxy.cfg |
我们简单修改一下配置文件内容,配置一个用来均衡后端SolrCloud搜索集群服务器,如下所示:
01 | #--------------------------------------------------------------------- |
03 | #--------------------------------------------------------------------- |
07 | chroot /var/lib/haproxy |
08 | pidfile /var/run/haproxy.pid |
14 | # turn on stats unix socket |
15 | stats socket /var/lib/haproxy/stats |
17 | #--------------------------------------------------------------------- |
18 | # common defaults that all the 'listen' and 'backend' sections will |
19 | # use if not designated in their block |
20 | #--------------------------------------------------------------------- |
26 | option http-server-close |
27 | option forwardfor except 127.0.0.0/8 |
30 | timeout http-request 10s |
35 | timeout http-keep-alive 10s |
39 | #--------------------------------------------------------------------- |
40 | # main frontend which proxys to the backends |
41 | #--------------------------------------------------------------------- |
42 | frontend haproxy-lbserver |
44 | acl url_solr_path path_beg /solr-cloud |
45 | acl url_static path_beg -i /static /images /javascript /stylesheets |
46 | acl url_static path_end -i .jpg .gif .png .css .js |
48 | use_backend static if url_static |
49 | use_backend solr-cloud if url_solr_path |
50 | default_backend static |
52 | #--------------------------------------------------------------------- |
53 | # static backend for serving up images, stylesheets and such |
54 | #--------------------------------------------------------------------- |
57 | server static 127.0.0.1:4331 check |
59 | #--------------------------------------------------------------------- |
60 | # round robin balancing between the various backends |
61 | #--------------------------------------------------------------------- |
64 | server solr-1 slave1:8888 check |
65 | server solr-2 slave2:8888 check |
66 | server solr-3 slave3:8888 check |
67 | server solr-4 slave4:8888 check |
frontend的名称为haproxy-lbserver,实际上映射为具体服务IP地址,绑定到80端口,然后请求Path设置为/solr-cloud,也就是前端接收到类似以“http://haproxy-lbserver/solr-cloud”开始的链接,后面可以加上具体的其他请求参数。
在frontend中使用use_backend指令指定了一个转发至的backend,名称为solr-cloud,可以在use_backend指令后面使用过滤条件指令if来指定转发的backend名称。
backend中指定了实际集群服务器的配置,对其进行负载均衡,一共指定了4台Solr搜索服务器,使用roundrobin负载均衡策略。
我们将默认配置文件拷贝到目录/home/hadoop/shiyanjun/haproxy-1.4.24/conf下面,然后启动haproxy:
1 | sudo haproxy -f /home/hadoop/shiyanjun/haproxy-1.4.24/conf/haproxy.cfg |
启动成功以后,可以访问类似如下的请求链接:
1 | 北京&fl=*&fq=building_type:1&start=0&rows=10 |
HAProxy会将请求转发至backend端的集群服务器上去,执行实际的请求处理。
HAProxy的官网文档相当详细,推荐参考官网文档,了解对应的配置选项和使用方法。
转载地址:https://blog.csdn.net/weixin_34077371/article/details/90664062 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!