HAPorxy 集群配置 <?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

 

                                              

 

  HAProxy 介绍

       反向代理服务器 , 支持双机热备支持虚拟主机,但其配置简单,拥有非常不错的服务器健康检查功能,当其代理的后端服务器出现故障 , HAProxy 会自动将该服务器摘除,故障恢复后再自动将该服务器加入。有 Web 图形化的界面,可以查看集群的状态。

新的 1.3 版本后,引入了 frontend,backend,frontend 根据任意 HTTP 请求头内容做规则匹配,然后把请求定向到相关的 backend.

 

 

  网络架构

 

一般采用的网络结构 ( 反向代理 )

<?xml:namespace prefix = v ns = "urn:schemas-microsoft-com:vml" />

 

 

 

 

 

 

 

  配置实例

 

角色

操作系统

IP 地址及对应域名

服务端口

HAproxy

Redhat Linux AS4

eth0    <?xml:namespace prefix = st1 ns = "urn:schemas-microsoft-com:office:smarttags" />10.0.0.60/24  dms.sds.com

eth0    10.0.0.70/24  help.sds.com

eth1    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 ( 该服务器上有帮助网站 )

 

配置步骤

 

1.      http://haproxy.1wt.eu 下载 HAproxy 最新稳定版

2.      将下载的文件 haproxy-1.3.15.1.tar.gz 存放到 Linux 服务器 /usr/share 目录。

3.      解压 tar –zxvf  haproxy-1.3.15.1.tar.gz

4.      重命名解压后的文件夹 mv haproxy-1.3.15.1 haproxy

5.    cd haproxy

6.      make TARGET=linux26  # 本例 Redhat 的内核是 2.6 ,请先查看 HAproxy README

7.      make install

8.      安装好后就可以配置了。

9.      vi haproxy.cfg

global

       maxconn 5120

       chroot /usr/share/haproxy    #  haproxy 安装目录

       uid 99

       gid 99

       daemon

       quiet

        # 通过 nbproc 多设置几个 haproxy 并发进程,这样每个进程的 task_queue 相对就会短很多,性能自然就能提高不少

       nbproc   2  

       #pidfile /var/run/haproxy-private.pid

 

defaults

 

       log     global

       mode http

       option   httplog

       option   dontlognull

       log 127.0.0.1 local3

       retries 3

       option redispatch

       maxconn 2000

       contimeout    5000

       clitimeout    50000

       srvtimeout    50000

 

listen SDS.DMS.COM 10.0.0.60:80   # 监听 IP 及端口,域名是在 Web 界面显示的标识

 

   mode http

   stats uri /haproxy   # 监控 haproxy 状态虚拟目录

   stats realm Haproxy\statistics

   stats auth gao:gao    # 设置状态监控的用户名为 gao 密码为 gao

   balance roundrobi n   # 负载均衡算法

   cookie SERVERID insert indirect

   option httpclose

   option forwardfor 

   option 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 5

server APP02 10.0.1.11:80 cookie app1inst2 check inter 2000 rise 2 fall 5

server APP03 10.0.1.12:80 cookie app1inst3 check inter 2000 rise 2 fall 5

server APP04 10.0.1.13:80 cookie app1inst4 check inter 2000 rise 2 fall 5

server APP05 10.0.1.14:80 cookie app1inst5 check inter 2000 rise 2 fall 5

server APP06 10.0.1.15:80 cookie app1inst6 check inter 2000 rise 2 fall 5

 

 

listen Help.SDS.Com  10.0.0.70:80  # 监听 IP 及端口,域名是在 Web 界面显示的标识

 

   mode http

   stats uri /haproxy       # 监控 haproxy 状态虚拟目录

   stats realm Haproxy\statistics

   stats auth gao:gao    # 设置状态监控的用户名为 gao 密码为 gao

   balance roundrobin        # 负载均衡算法

   cookie SERVERID insert indirect

   option httpclose 

   option forwardfor   

   option 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 5

 

9. 配置文件写好后就可以启动了。

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://help.sds.com 也能显示 10.0.0 .15 上的帮助网站。

   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.d

Vi haproxy

#!/bin/sh

# description: Auto Start and Stop Haproxy Software

# chkconfig: 2345 99 10

 

start ()

{

cd /usr/share/haproxy

./haproxy -f haproxy.cfg

}

 

stop ()

{

pid=`ps -ef | grep -v grep | grep haproxy | awk '{print $2}'`

for ps in $pid

do

  kill -9 $ps

done

}

 

 

case $1 in

 

   start)    start ;;

   stop)     stop ;;

    *)   echo "Use ./haproxy {start|stop}" ;;

esac

 

exit 0

 

2. chmod 755 haproxy

 

3. 添加自启动功能,在 /etc/rc.d/rc.local 文件中添加如下内容

Vi /etc/rc.d/rc.local

ifcfg eth0 add 10.0.0.70/24   # eth0 添加第 2 IP

cd /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