您的位置:首页 > 其它

HAProxy实现负载均衡

2009-04-28 18:10 246 查看
[align=center]HAProxy实现负载均衡 [/align]
一, HAProxy 介绍
反向代理服务器,支持双机热备支持虚拟主机,但其配置简单,拥有非常不错的服务器健康检查功能,当其代理的后端服务器出现故障, HAProxy会自动将该服务器摘除,故障恢复后再自动将该服务器加入。新的1.3引入了frontend,backend,frontend根据任意HTTP请求头内容做规则匹配,然后把请求定向到相关的backend.
二,利用HAProxy实现负载均衡
1. 利用HAProxy实现负载均衡
192.168.1.222 (haproxy)---------负载均衡----------(192.168.1.100)
安装配置HAproxy
wget http://haproxy.1wt.eu/download/1.3/src/haproxy-1.3.14.2.tar.gz
tar zxvf haproxy-1.3.14.2.tar.gz
mv haproxy-1.3.14.2 haproxy
cd haproxy
make
2. 创建配置文件
# vi haproxy.cfg
内容:
# this config needs haproxy-1.1.28 or haproxy-1.2.2
global
log 127.0.0.1 local0
log 127.0.0.1 local1 notice
#log loghost local0 info
maxconn 5555
chroot /usr/share/haproxy
uid 99
gid 99
daemon
#debug
#quiet
defaults
log global
mode http
option httplog
option dontlognull
retries 3
redispatch
maxconn 2000
contimeout 5000
clitimeout 50000
srvtimeout 50000
listen app1 0.0.0.0:8080
mode http
option httplog
option dontlognull
cookie SERVERID rewrite
balance roundrobin
option httpchk
server app1_1 192.168.1.222:80 cookie app1inst1 check inter 2000 rise 2 fall 5
server app1_2 192.168.1.100:80 cookie app1inst2 check inter 2000 rise 2 fall 5
# server app1_3 192.168.0.108:80 cookie app1inst3 check inter 2000 rise 2 fall 5
# server app1_4 220.181.6.6:80 cookie app1inst4 check inter 2000 rise 2 fall 5
stats uri /my_stats # 访问时:http://192.168.1.222:8080/my_stats
stats realm Statistics\ for\ MyApp1-2
stats auth admin:uplooking # 帐号:密码
stats scope .
stats scope app2
listen app2 0.0.0.0:8443
mode tcp
option ssl-hello-chk
balance source
server inst1 192.168.1.222:443 check inter 2000 fall 3
server inst2 192.168.1.100:443 check inter 2000 fall 3
# server back1 192.168.0.254:443 backup
stats uri /my_stats
stats realm Statistics\ for\ MyApp2
stats scope .

errorloc 502 http://192.168.1.222/error502.html
errorfile 503 /site/haproxy/errors/503.http
# ./haproxy –f examples/haproxy.cfg 启动服务.
监控状态图示 http://192.168.0.222:8080/my_stats,输入用户名密码查看状态。
三.相关介绍
#./haproxy –help //haproxy相关命令参数介绍.
haproxy -f <配置文件> [-n 最大并发连接总数] [-N 每个侦听的最大并发数] [-d] [-D] [-q] [-V] [-c] [-p <pid文件>] [-s] [-l] [-dk]
[-ds] [-de] [-dp] [-db] [-m <内存限制M>] [{-sf|-st} pidlist...]
-d 前台,debug模式
-D daemon模式启动
-q 安静模式,不输出信息
-V 详细模式
-c 对配置文件进行语法检查
-s 显示统计数据
-l 显示详细统计数据
-dk 不使用kqueue
-ds 不使用speculative epoll
-de 不使用epoll
-dp 不使用poll
-db 禁用后台模式,程序跑在前台
-sf <pidlist>
程序启动后向pidlist里的进程发送FINISH信号,这个参数放在命令行的最后
-st <pidlist>
程序启动后向pidlist里的进程发送TERMINATE信号,这个参数放在命令行的最后
四,更高级的应用参考相关文档
官方参考手册: http://haproxy.1wt.eu/download/1.3/doc/haproxy-en.txt
http://www.howtoforge.com/high-a
http://cn.haproxy.org/
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: