关于haproxy,多年前曾经安装配置过。当时的感觉就是很简单,三两下就能使用了。不过随着阅历的积累,越发觉得想把haproxy用到出神入化还是很不容易的。不止是haproxy,任何一项技术想使用到巅峰状态都是一件很困难的事情。用到什么程度能够最大限度的提升性价比?多开启一项功能,性能就有可能大幅度下降;少开启一项功能,性能达不到预想的峰值。高深啊,高深。。。好了,废话也不多说了。今天就先把haproxy基本的安装配置贴出来,不是为了什么共同学习(安装这么个东西也没有什么好学习的),实在是为了给自己备忘一下,省的再需要安装的时候到处翻腾。
Part 1 HAProxy简介
来自haproxy官网的介绍 http://haproxy.1wt.eu/
HAProxy is a free, very fast and reliable solution offerin high availability,load balancing, and proxying for TCP and HTTP-based applications. It is particularly suited for web sites crawling under very high loads while needing persistence or Layer7 processing. Supporting tens of thousands of connections is clearly realistic with todays hardware. Its mode of operation makes its integration into existing architectures very easy and riskless, while still offering the possibility not to expose fragile web servers to the Net, such as below :
For a long time, HAProxy was used only by a few hundreds of people around the world running very big sites serving several millions hits and between several tens of gigabytes to several terabytes per day to hundreds of thousands of clients, who needed 24x7 availability and who had internal skills to risk to maintain a free software solution. Over the years, things have changed a bit, HAProxy has become the de-facto standard load balancer, and it's often installed by default in cloud environments. Since it does not advertise itself, we only know it's used when the admins report it :-)
大概的意思是:HAProxy是一款提供高可用性、负载均衡 以及基于TCP和HTTP应用的代理软件,HAProxy是完全免费的、借助HAProxy可以快速并且可靠的提供基于TCP和HTTP应用的代理解决方案。HAProxy最主要的特点是性能,HAProxy特别适用于那些负载特大的web站点,这些站点通常又需要会话保持或七层处理。 HAProxy完全可以支持数以万计的并发连接。并且HAProxy的运行模式使得它可以很简单安全的整合进您当前的架构中, 同时可以保护你的web服务器不被暴露到网络上。
还有一些关于HAProxy架构应用和日志方面的介绍(来自http://17drupal.com/node/1403):
HAProxy是支持L4及L7交换的开源软件,高性能,高稳定性。在做L7交换时,可以根据http header/cookie等定制各种交换策略。 可以对http request/response header/cookie等进行修改。 易 于查看各种统计信息。
通过引入7层交换,更有效利用squid资源。
对可缓存的请求,使用url一致性hash将请求分为几组,每组由固定后端squid处理。这样每台squid由原先的要缓存所有内存变为只缓 存一部分内容,可以进一步提高squid的缓存命中率,特别是内存缓存命中率;
承担连接管理功能,客户端不再保持与squid的连接,而是保持与haproxy的连接,这样squid上的并发连接数可以减少到极低的水平,降低squid负载。一方面提高squid吞吐能力,一方面减少内存开销,可以有更多的内存用作缓存;
对纯动态请求,可以不再通过squid,由haproxy直接转发给后端web服务器,减少squid负担及所需机器数量;
通过7层交换功能及header修改等功能,可以更灵活的制定各种请求转发策略;
将不同域名下的同类请求合并处理;
将同一域名下的不同应用分散到不同的专用服务器上处理;
将请求量特别大的某些资源定向到专用服务器处理,如网站/内嵌首页等;
利用haproxy的强大日志能力,采集更多的信息以便细化分析
haproxy的日志对连接处理情况(建立连接到发送完请求时间,排队时间,连接后端服务器时间,后端响应时间,连接断开时间),连接断开情况 (各种断开原因)等记录更为详细,便于对网络状况做详细分析;
haproxy的日志可以捕获各种http请求/响应的header,在出现问题时便于采集信息进行排查;
haproxy还可以在日志中记录cookie(限一个),可以记录MAC以便对用户进行深度跟踪。
Prat 2 HAProxy的安装配置
在安装之前,首先确定一下结构。我在这次试验中用的是最最几本的一个结构,只是为了体验。
HAProxy节点:192.168.1.200
说明:我用了单节点是为了体验功能,应用到运营环境的话单节点这样的事情一定要避免。否则出现单点故障够大家喝一壶的。这里建议可以使用keepalived配置HAProxy的另一节点以作备份。
Web节点01:192.168.1.149
Web节点02:192.168.1.153
说明:我在web节点中yum安装的apache,其他啥都没干。
2.1 下载安装HAProxy:
登陆到haproxy机器上
- mkdir -p /data/softwares/
- cd /data/softwates/
- wget http://haproxy.1wt.eu/download/1.4/src/haproxy-1.4.20.tar.gz
- tar xzvf haproxy-1.4.20.tar.gz -C /usr/local/
- cd /usr/local/
- mv haproxy-1.4.20 haproxy
- cd haproxy
- make TARGET=linux26 //2.4内核版本 make TARGET=linux24,freebsd makeTARGET=freebsd,solaris系统:make TARGET=solaris CPU=ultrasparc REGEX=static-pcre
- make install
2.2 创建配置文件
- mkdir /etc/haproxy
- cp /usr/local/haproxy/examples/haproxy.cfg /etc/haproxy/
- #模板里面的配置改吧改吧也能用,不过还是自己写进去比较清楚明白
- vi /etc/haproxy/haproxy.cfg
- echo '' > /etc/haproxy/haproxy.cfg
在配置文件中添加如下内容(http7层代理)
- global
- maxconn 30720 #默认最大连接数
- log 127.0.0.1 local3 #[err warning info debug]
- chroot /var/haproxy #chroot运行的路径
- uid 99
- gid 99
- daemon
- nbproc 1 #进程数量(可以设置多个进程提高性能)
- pidfile /var/run/haproxy.pid #haproxy的pid存放路径,启动进程的用户必须有权限访问此文件
- ulimit-n 65535 #ulimit的数量限制
- defaults
- log global
- mode http
- option httplog
- option dontlognull
- option forwardfor #如果后端服务器需要获得客户端真实ip需要配置的参数,可以从Http Header中获得客户端ip
- option abortonclose #当服务器负载很高的时候,自动结束掉当前队列处理比较久的连接
- option redispatch
- #balance roundrobin #默认的负载均衡的方式,轮询方式
- #balance source #默认的负载均衡的方式,类似nginx的ip_hash
- balance leastconn #默认的负载均衡的方式,最小连接
- retries 3
- maxconn 30720
- contimeout 5000
- clitimeout 50000
- srvtimeout 50000
- listen test-webservers 0.0.0.0:80
- stats uri /check-status
- stats realm Haproxy\ statistics
- stats auth admin:admin
- option httpchk *
- cookie SERVERID insert indirect nocache
- server web1 192.168.1.149:80 cookie web1 check inter 1500 rise 3 fall 3 weight 1
- server web2 192.168.1.153:80 cookie web1 check inter 1500 rise 3 fall 3 weight 2
- capture cookie ASPSESSION len 32
- srvtimeout 20000
- capture request header Host len 40
- capture request header Content-Length len 10
- capture request header Referer len 200
- capture response header Server len 40
- capture response header Content-Length len 10
- capture response header Cache-Control len 8
在配置文件中添加如下内容(tcp 4层代理)
- global
- maxconn 30720 #默认最大连接数
- log 127.0.0.1 local3 #[err warning info debug]
- chroot /var/haproxy #chroot运行的路径
- uid 99
- gid 99
- daemon
- nbproc 1 #进程数量(可以设置多个进程提高性能)
- pidfile /var/run/haproxy.pid #haproxy的pid存放路径,启动进程的用户必须有权限访问此文件
- ulimit-n 65535 #ulimit的数量限制
- defaults
- log global
- mode tcp
- balance leastconn #默认的负载均衡的方式,最小连接
- retries 3
- maxconn 30720
- contimeout 5000
- clitimeout 50000
- srvtimeout 50000
- listen check 0.0.0.0:10000
- mode http
- option httplog
- stats uri /check-status
- stats realm Haproxy\ statistics
- stats auth admin:admin
- capture request header Host len 40
- capture request header Content-Length len 10
- capture request header Referer len 200
- capture response header Server len 40
- capture response header Content-Length len 10
- capture response header Cache-Control len 8
- #frontend http-80-in
- #bind *:80
- #mode tcp #http的7层模式
- #log global #应用全局的日志配置
- #acl jt_s0_80 hdr_dom(host) -i s0.jt.yeyou.com
- #frontend http-4949-in
- #bind *:4949
- #mode tcp
- #log global
- #acl jt_s0_4949 hdr_dom(host) -i s0.jt.yeyou.com
- #frontend http-3749-in
- #bind *:3749
- #mode tcp
- #log global
- #acl jt_s0_3749 hdr_dom(host) -i s0.jt.yeyou.com
- #
- #frontend http-843-in
- #bind *:843
- #mode tcp
- #log global
- #acl jt_s0_843 hdr_dom(host) -i s0.jt.yeyou.com
- #
- #backend jt_s0_80
- #mode tcp
- #balance roundrobin
- #server s0_gs_80 10.127.65.110:80 check inter 1500 rise 3 fall 3 weight 1
- #
- #backend jt_s0_4949
- #mode tcp
- #balance roundrobin
- #server s0_gs_4949 10.127.65.110:4949 check inter 1500 rise 3 fall 3 weight 1
- #
- #backend jt_s0_3749
- #mode tcp
- #balance roundrobin
- #server s0_gs_3749 10.127.65.110:3749 check inter 1500 rise 3 fall 3 weight 1
- #
- #
- #backend jt_s0_843
- #mode tcp
- #balance roundrobin
- #server s0_gs_843 10.127.65.110:843 check inter 1500 rise 3 fall 3 weight 1
- #
- #
- listen http-80-in 0.0.0.0:80
- mode tcp
- acl jt_s0_80y hdr_dom(host) -i s0.jt.yeyou.com
- balance roundrobin
- server jt_s0_80 10.127.65.110:80 check inter 1500 rise 3 fall 3 weight 1
- listen http-4949-in 0.0.0.0:4949
- mode tcp
- acl jt_s0_4949 hdr_dom(host) -i s0.jt.yeyou.com
- balance roundrobin
- server jt_s0_4949 10.127.65.110:4949 check inter 1500 rise 3 fall 3 weight 1
- listen http-3749-in 0.0.0.0:3749
- mode tcp
- acl jt_s0_3749 hdr_dom(host) -i s0.jt.yeyou.com
- balance roundrobin
- server jt_s0_3749 10.127.65.110:3749 check inter 1500 rise 3 fall 3 weight 1
- listen http-843-in 0.0.0.0:843
- mode tcp
- acl jt_s0_843 hdr_dom(host) -i s0.jt.yeyou.com
- balance roundrobin
- server jt_s0_843 10.127.65.110:843 check inter 1500 rise 3 fall 3 weight 1
2.3 启动haproxy进程 (更多启动参数可通过/usr/local/haproxy/haproxy -help 命令来查看)
- /usr/local/sbin/haproxy -f /etc/haproxy/haproxy.cfg -st `cat /var/run/haproxy.pid`
- ps -ef | grep haproxy
2.4 查看状态监视页面
浏览器访问http://192.168.1.200/check-status,输入用户名haproxy和密码haproxy@200,即可查看状态。
2.5 日志配置 (仅限于CentOS 5,CentOS 6使用的是rsyslog,与syslog的配置区别很大)
- vim /etc/syslog.conf
- 添加 local3.* /var/log/haproxy.log
- vi /etc/sysconfig/syslog
- 把SYSLOGD_OPTIONS="-m 0"
- 改成 SYSLOGD_OPTIONS="-r -m 0" #enables logging from remote machines
- 重启syslogd: /etc/init.d/syslog restart
- tail -f -n 30 /var/log/haproxy.log 应该可以看到日志输出了。
Part 3 web节点的配置
什么复杂的东西都不用做,把两个节点的apache装上,开起来,创建index.html文件(两台机器的index文件内容一定要不同)
- yum install httpd -y
- service httpd start
这样,web节点的配置就完成了。
Prat 4 测试
4.1开启节点01的web服务,访问http://192.168.1.200,查看结果。
4.2 关闭节点01web服务,开启节点02web服务,访问http://192.168.1.200,查看结果。
4.3 调整配置文件中的balance 算法,同时开启两个web节点,在不保留cookie的情况下多次访问,查看结果有什么不同。
附录01:haproxy启动的参数说明
/usr/local/haproxy/haproxy -help #haproxy相关命令参数介绍.
haproxy -f <配置文件> [-n 最大并发连接总数] [-N 每个侦听的最大并发数] [-d] [-D] [-q] [-V] [-c] [-p
[-ds] [-de] [-dp] [-db] [-m <内存限制M>] [{-sf|-st} pidlist...]
-d 前台,debug模式
-D daemon模式启动
-q 安静模式,不输出信息
-V 详细模式
-c 对配置文件进行语法检查
-p pid文件
-s 显示统计数据
-l 显示详细统计数据
-dk 不使用kqueue
-ds 不使用speculative epoll
-de 不使用epoll
-dp 不使用poll
-db 禁用后台模式,程序跑在前台
-sf
-st
附录02:balance算法
负载均衡可以有多种方式:
--简单的轮询:balance roundrobin;
--根据请求的源IP:balance source;
--根据请求的uri:balance uri;
--根据请求RUL中的参数:balance url_param
不同的需求选择不同的负载均衡方式对于整体性能的提升很有帮助。有兴趣的可以自己深入的研究在什么架构情况下选择怎样的负载均衡算法。
这只是一篇基本的安装配置说明,不过通过后续的应用,以后应该会有比较深入的分析。敬请期待。。。(为什么是应该会有呢?看时间啦、、、万一没时间写呢。o_O!!)