HAproxy
2009-11-30 14:24:37
原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 、作者信息和本声明。否则将追究法律责任。http://nails.blog.51cto.com/640492/236878HAPorxy 集群配置一 HAProxy 介绍反向代理服务器,支持双机热备支持虚拟主机,但其配置简单,拥有非常不错的服务器健康检查功能,当其代理的后端服务器出现故障, HAProxy会自动将该服务器摘除,故障恢复后再自动将该服务器加入。有Web图形化的界面,可以查看集群的状态。新的1.3版本后,引入了frontend,backend,frontend根据任意HTTP请求头内容做规则匹配,然后把请求定向到相关的backend.二 网络架构一般采用的网络结构(反向代理)三 配置实例
角色 操作系统 IP地址及对应域名 服务端口 HAproxy Redhat Linux AS4 eth0 10.0.0.60/24 dms.sds.cometh0 10.0.0.70/24 help.sds.cometh1 10.0.1.60/24 80 APP Server Windows 10.0.1.10—15/24 80需要:1. 访问 dms.sds.com 时,自动分发到集群节点服务器10.0.1.10-15/24 这6台服务器上。2. 访问 help.sds.com 时,自动分发到服务器 10.0.1.15/24 上(该服务器上有帮助网站)。四 配置步骤2. 将下载的文件haproxy-1.3.15.1.tar.gz存放到Linux服务器/usr/share目录。3. 解压tar –zxvf haproxy-1.3.15.1.tar.gz4. 重命名解压后的文件夹 mv haproxy-1.3.15.1 haproxy5. cd haproxy6. make TARGET=linux26 #本例Redhat 的内核是2.6,请先查看HAproxy 的README7. make install8. 安装好后就可以配置了。9. vi haproxy.cfgglobalmaxconn 5120chroot /usr/share/haproxy # haproxy安装目录uid 99gid 99daemonquiet# 通过nbproc多设置几个haproxy并发进程,这样每个进程的task_queue相对就会短很多,性能自然就能提高不少nbproc 2#pidfile /var/run/haproxy-private.piddefaultslog globalmode httpoption httplogoption dontlognulllog 127.0.0.1 local3retries 3option redispatchmaxconn 2000contimeout 5000clitimeout 50000srvtimeout 50000listen SDS.DMS.COM 10.0.0.60:80 # 监听IP及端口,域名是在Web界面显示的标识mode httpstats uri /haproxy # 监控haproxy状态虚拟目录stats realm Haproxy\statisticsstats auth gao:gao # 设置状态监控的用户名为gao密码为gaobalance roundrobin # 负载均衡算法cookie SERVERID insert indirectoption httpcloseoption forwardforoption httpchk HEAD /welcome.htm HTTP/1.0 # 健康检测 每一台的IIS根目录存放# weblocme.htm文件# 下面是节点服务器server APP01 10.0.1.10:80 cookie app1inst1 check inter 2000 rise 2 fall 5server APP02 10.0.1.11:80 cookie app1inst2 check inter 2000 rise 2 fall 5server APP03 10.0.1.12:80 cookie app1inst3 check inter 2000 rise 2 fall 5server APP04 10.0.1.13:80 cookie app1inst4 check inter 2000 rise 2 fall 5server APP05 10.0.1.14:80 cookie app1inst5 check inter 2000 rise 2 fall 5server APP06 10.0.1.15:80 cookie app1inst6 check inter 2000 rise 2 fall 5listen Help.SDS.Com 10.0.0.70:80 # 监听IP及端口,域名是在Web界面显示的标识mode httpstats uri /haproxy # 监控haproxy状态虚拟目录stats realm Haproxy\statisticsstats auth gao:gao # 设置状态监控的用户名为gao密码为gaobalance roundrobin # 负载均衡算法cookie SERVERID insert indirectoption httpcloseoption forwardforoption httpchk HEAD /welcome.htm HTTP/1.0 # 健康检测IIS根目录存放有# weblocme.htm文件# 下面是节点服务器server APP01 10.0.1.15:80 cookie app1inst1 check inter 2000 rise 2 fall 59. 配置文件写好后就可以启动了。Ifcfg eth0 add 10.0.0.70/24 为eth0 添加第二个IP地址./haproxy –f haproxy.cfg 即可启动程序.用IE测试 http://sds.dms.com/welcome.htm ,不断刷新是不会显示其它APP服务器的welcome.htm页面,需要我们关掉IE,再次访问 http://sds.dms.com/welcome.htm即显示另一台,如此反复即可看到每台APP上的welcome.htm用IE输入: http://sds.dms.com/haproxy 或 http://help.sds.com/haproxy 输入用户名:gao 密码:gao即可看到haproxy的集群状态,如下图五 配置haproxy 随Linux系统自启动配置1.建立一个haproxy 的shell文件,用于控制haproxy的启动与关闭Cd /etc/rc.d/init.dVi haproxy#!/bin/sh# description: Auto Start and Stop Haproxy Software# chkconfig: 2345 99 10start (){cd /usr/share/haproxy./haproxy -f haproxy.cfg}stop (){pid=`ps -ef | grep -v grep | grep haproxy | awk '{print $2}'`for ps in $piddokill -9 $psdone}case $1 instart) start ;;stop) stop ;;*) echo "Use ./haproxy {start|stop}" ;;esacexit 02. chmod 755 haproxy3. 添加自启动功能,在/etc/rc.d/rc.local文件中添加如下内容Vi /etc/rc.d/rc.localifcfg eth0 add 10.0.0.70/24 # 为 eth0 添加第2个IPcd /etc/rc.d/init.d./haproxy start # 启动haproxy 程序4. 操作haproxy 的相关命令cd /etc/rc.d/init.d./haproxy start # 启动haproxy./haproxy stop # 停止haproxy./haproxy restart # 重启动haproxy

浙公网安备 33010602011771号