CentOS 6.4上HAProxy-1.4.24安装配置
发布日期:2021-08-31 13:48:49 浏览次数:6 分类:技术文章

本文共 6671 字,大约阅读时间需要 22 分钟。

HAProxy是一款免费、快速并且可靠的一种代理解决方案,支持高可用性、负载均衡特性,同时适用于做基于TCP和HTTP的应用的代理。对于一些负载较大的Web站点,使用HAProxy特别合适。HAProxy能够支撑数以万计的并发连接。它的配置简单,能够很容易整合大我们现有的应用架构之中。

下面,我们在CentOS 6.4上进行安装配置HAProxy。

安装配置

按照如下步骤进行安装:

1 wget tar.gz
2 tar xvzf haproxy-1.4.24.tar.gz
3 cd haproxy-1.4.24
4 make TARGET=linux26
5 make install

默认安装,HAProxy对应的配置文件的存放路径为/etc/haproxy/haproxy.cfg。

我们看一下,默认安装的配置文件内容,如下所示:

01 #---------------------------------------------------------------------
02 # Example configuration for a possible web application. See the
03 # full configuration options online.
04 #
05 #
06 #
07 #---------------------------------------------------------------------
08
09 #---------------------------------------------------------------------
10 # Global settings
11 #---------------------------------------------------------------------
12 global
13 # to have these messages end up in /var/log/haproxy.log you will
14 # need to:
15 #
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
19 #
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
23 #
24 # local2.* /var/log/haproxy.log
25 #
26 log 127.0.0.1 local2
27
28 chroot /var/lib/haproxy
29 pidfile /var/run/haproxy.pid
30 maxconn 4000
31 user haproxy
32 group haproxy
33 daemon
34
35 # turn on stats unix socket
36 stats socket /var/lib/haproxy/stats
37
38 #---------------------------------------------------------------------
39 # common defaults that all the 'listen' and 'backend' sections will
40 # use if not designated in their block
41 #---------------------------------------------------------------------
42 defaults
43 mode http
44 log global
45 option httplog
46 option dontlognull
47 option http-server-close
48 option forwardfor except 127.0.0.0/8
49 option redispatch
50 retries 3
51 timeout http-request 10s
52 timeout queue 1m
53 timeout connect 10s
54 timeout client 1m
55 timeout server 1m
56 timeout http-keep-alive 10s
57 timeout check 10s
58 maxconn 3000
59
60 #---------------------------------------------------------------------
61 # main frontend which proxys to the backends
62 #---------------------------------------------------------------------
63 frontend main *:5000
64 acl url_static path_beg -i /static /images /javascript /stylesheets
65 acl url_static path_end -i .jpg .gif .png .css .js
66
67 use_backend static if url_static
68 default_backend app
69
70 #---------------------------------------------------------------------
71 # static backend for serving up images, stylesheets and such
72 #---------------------------------------------------------------------
73 backend static
74 balance roundrobin
75 server static 127.0.0.1:4331 check
76
77 #---------------------------------------------------------------------
78 # round robin balancing between the various backends
79 #---------------------------------------------------------------------
80 backend app
81 balance roundrobin
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段用于配置进程级的参数。官网文档基于参数的功能,将global配置参数分为3组:

  1. 进程管理和安全
  2. 性能调优
  3. 调试

具体内容可以参考文档详细介绍。

  • defaults段

defaults段主要是代理配置的默认配置段,设置默认参数,这些默认的配置可以在后面配置的其他段中使用。如果其他段中想修改默认的配置参数,只需要覆盖defaults段中的出现配置项内容。

关于defaults段可以配置的参数,可以参考官网文档的详细介绍。

  • frontend段

frontend段主要配置前端监听的Socket相关的属性,也就是接收请求链接的虚拟节点。这里除了配置这些静态的属性,还可以根据一定的规则,将请求重定向到配置的backend上,backend可能配置的是一个服务器,也可能是一组服务器(集群)。

  • backend段

backend段主要是配置的实际服务器的信息,通过frontend配置的重定向请求,转发到backend配置的服务器上。

  • listen段

listen段是将frontend和backend这两段整合在一起,直接将请求从代理转发到实际的后端服务器上。

启动HAProxy代理

启动非常简单,执行如下命令即可:

1 sudo haproxy -f /etc/haproxy/haproxy.cfg

我们简单修改一下配置文件内容,配置一个用来均衡后端SolrCloud搜索集群服务器,如下所示:

01 #---------------------------------------------------------------------
02 # Global settings
03 #---------------------------------------------------------------------
04 global
05 log 127.0.0.1 local2
06
07 chroot /var/lib/haproxy
08 pidfile /var/run/haproxy.pid
09 maxconn 4000
10 user haproxy
11 group haproxy
12 daemon
13
14 # turn on stats unix socket
15 stats socket /var/lib/haproxy/stats
16
17 #---------------------------------------------------------------------
18 # common defaults that all the 'listen' and 'backend' sections will
19 # use if not designated in their block
20 #---------------------------------------------------------------------
21 defaults
22 mode http
23 log global
24 option httplog
25 option dontlognull
26 option http-server-close
27 option forwardfor except 127.0.0.0/8
28 option redispatch
29 retries 3
30 timeout http-request 10s
31 timeout queue 1m
32 timeout connect 10s
33 timeout client 1m
34 timeout server 1m
35 timeout http-keep-alive 10s
36 timeout check 10s
37 maxconn 3000
38
39 #---------------------------------------------------------------------
40 # main frontend which proxys to the backends
41 #---------------------------------------------------------------------
42 frontend haproxy-lbserver
43 bind 0.0.0.0:80
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
47
48 use_backend static if url_static
49 use_backend solr-cloud if url_solr_path
50 default_backend static
51
52 #---------------------------------------------------------------------
53 # static backend for serving up images, stylesheets and such
54 #---------------------------------------------------------------------
55 backend static
56 balance roundrobin
57 server static 127.0.0.1:4331 check
58
59 #---------------------------------------------------------------------
60 # round robin balancing between the various backends
61 #---------------------------------------------------------------------
62 backend solr-cloud
63 balance roundrobin
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 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:数据库篇之存储过程[bsp_getuserordercount]-BrnShop1.9升级至2.1升级说明(非官方版本)...
下一篇:《Netty 权威指南》—— NIO创建的TimeServer源码分析

发表评论

最新留言

感谢大佬
[***.8.128.20]2024年03月01日 17时37分25秒

关于作者

    喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!

推荐文章

唱好铁血丹心谐音正规_趙贤典:打好“感情牌” 唱好“大合唱” 2019-04-21
aix系统vi修改命令_Linux基础知识必备:利用vi编辑器创建和编辑正文文件 2019-04-21
天涯明月刀开发_玩家被天涯明月刀手游“冷落”?六大门派角色竟不带正眼看人... 2019-04-21
this指向undefined uiapp_一个this都没有,真好 2019-04-21
add p4 多个文件_2-3【微信小程序全栈开发课程】index页面完善--vue文件代码解析... 2019-04-21
5w2h原则指的是什么_什么是5W2H分析法?一首小诗带入进入大门。 2019-04-21
技校毕业是什么学历_中等职业学校是什么_中等职业学校毕业是什么学历 2019-04-21
2压缩备份数据库_MySQL数据备份与恢复(二) xtrabackup工具 2019-04-21
英特尔cpu发布时间表_被嘲讽的英特尔核显,强大能力其实超乎你的想象 2019-04-21
chi2inv函数 matlab_MATLAB概率和统计(2) 2019-04-21
lisp修改上一个图素_在Windows上安装Haskell 2019-04-21
ad19 导出step 没有pcb_几款主流PCB软件哪个最好用,你用过几款? 2019-04-21
json mysql 字段 默认值_Newtonsoft.Json 六个超简单又实用的特性,值得一试 【上篇】... 2019-04-21
ocdma相干非相干_《Acconeer 60GHz脉冲相干雷达芯片:A111》 2019-04-21
修改表格字体颜色_Excel技巧:Excel如何修改字体颜色 2019-04-21
native react 变颜色 点击_React Native主动更改StackNavigator标头颜色 2019-04-21
prism项目搭建 wpf_WPF MVVM使用prism4.1搭建 2019-04-21
python发微信红包群_用Python实现微信自动化抢红包,再也不用担心抢不到红包了... 2019-04-21
python中func自定义函数_Python函数之自定义函数&作用域&闭包 2019-04-21
wget连接指定端口_端口通不通 telnet wget ssh 2019-04-21