关于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 ] [-s] [-l] [-dk]
[-ds] [-de] [-dp] [-db] [-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 程序启动后向pidlist 里的进程发送FINISH 信号,这个参数放在命令行的最后
-st 程序启动后向pidlist 里的进程发送TERMINATE 信号,这个参数放在命令行的最后
附录02 :balance 算法
负载均衡可以有多种方式:
-- 简单的轮询:balance roundrobin ;
-- 根据请求的源IP :balance source ;
-- 根据请求的uri :balance uri ;
-- 根据请求RUL 中的参数:balance url_param
不同的需求选择不同的负载均衡方式对于整体性能的提升很有帮助。有兴趣的可以自己深入的研究在什么架构情况下选择怎样的负载均衡算法。
这只是一篇基本的安装配置说明,不过通过后续的应用,以后应该会有比较深入的分析。敬请期待。。。(为什么是应该会有呢?看时间啦、、、万一没时间写呢。o_O!! )
运维网声明
1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网 享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com