配置 haproxy 4层代理

官网下载 http://www.haproxy.org/
目前最新稳定版: http://www.haproxy.org/download/1.7/src/haproxy-1.7.9.tar.gz

解压安装:

[root@localhost ~]# yum install -y make gcc gcc-c++
[root@localhost ~]# tar zxvf haproxy-1.7.9
[root@localhost ~]# cd haproxy-1.7.9
[root@localhost haproxy-1.7.9]# make TARGET=linux2628 PREFIX=/usr/local/haproxy
[root@localhost haproxy-1.7.9]# make TARGET=linux2628 PREFIX=/usr/local/haproxy install

linux2628 是for Linux内核大于 2.6.28, 3.x的,可以通过readme查看。

配置文件(百度了个现成的):

[root@localhost ~]# mkdir /usr/local/haproxy/conf
[root@localhost ~]# vi /usr/local/haproxy/conf/haproxy.cfg
###########全局配置#########
global
chroot /usr/local/haproxy
daemon
nbproc 1
group nobody
user nobody
pidfile /usr/local/haproxy/haproxy.pid
ulimit-n 65536
#spread-checks 5m
#stats timeout 5m
#stats maxconn 100

########默认配置############
defaults
mode tcp #默认的模式mode { tcp|http|health },tcp是4层,http是7层,health只会返回OK
retries 3 #两次连接失败就认为是服务器不可用,也可以通过后面设置
option redispatch #当serverId对应的服务器挂掉后,强制定向到其他健康的服务器
option abortonclose #当服务器负载很高的时候,自动结束掉当前队列处理比较久的链接
maxconn 65535 #默认的最大连接数
timeout connect 5000ms #连接超时
timeout client 30000ms #客户端超时
timeout server 30000ms #服务器超时
#timeout check 2000 #心跳检测超时
log 127.0.0.1 local0 err #[err warning info debug]

########mysql代理配置#################
listen mysql
bind 0.0.0.0:3306
mode tcp # 4层代理
balance roundrobin
server s1 172.31.4.48:3306 weight 1 maxconn 60000 check inter 3s
server s2 172.31.4.49:3306 weight 1 maxconn 60000 check inter 3s
server s3 172.31.4.50:3306 weight 1 maxconn 60000 check inter 3s

########rabbitmq代理配置##############
listen rabbitmq
bind 0.0.0.0:5672
mode tcp # 4层代理
balance roundrobin
server s1 172.31.4.48:5672 weight 1 maxconn 60000 check inter 3s
server s2 172.31.4.49:5672 weight 1 maxconn 60000 check inter 3s
server s3 172.31.4.50:5672 weight 1 maxconn 60000 check inter 3s


########统计页面配置########
listen admin
bind 0.0.0.0:8888 #监听端口
mode http #http代理
option httplog #采用http日志格式
#log 127.0.0.1 local0 err
maxconn 10
stats refresh 3s #统计页面自动刷新时间
stats uri /stats #统计页面url
stats realm RootopHaproxy #统计页面密码框上提示文本
stats auth admin:admin #统计页面用户名和密码设置
stats hide-version #隐藏统计页面上HAProxy的版本信息

启动服务:

[root@localhost ~]# /usr/local/haproxy/sbin/haproxy -f /usr/local/haproxy/conf/haproxy.cfg

用浏览器访问 http://ip:8888/stats 就可以看到类似下面的监控图。

haproxy安装配置及测试

系统版本:centos6.5_x64
软件版本:haproxy-1.5.9.tar.gz
后端服务器IP:10.10.10.5、10.10.10.6

这里提供haproxy的本站下载地址:wget -c https://www.rootop.org/rs/haproxy-1.5.9.tar.gz

安装:

[root@rootop-haproxy haproxy-1.5.9]# make TARGET=linux26 PREFIX=/usr/local/haproxy install

这里为什么是linux26,可以去看下README文档。

创建配置文件:
[root@rootop-haproxy ~]# mkdir /usr/local/haproxy/conf
[root@rootop-haproxy ~]# vi /usr/local/haproxy/conf/haproxy.cfg

global
    log 127.0.0.1 local0 info
    maxconn 1000
    chroot /usr/local/haproxy
    uid nobody
    gid nobody
    daemon
    nbproc 1
    pidfile /tmp/haproxy.pid

defaults
    log global
    mode http
    option httplog
    retries 3
    option httpclose
    option dontlognull
    option forwardfor
    option redispatch
    log 127.0.0.1 local3
    balance roundrobin
    maxconn 20480
    timeout connect 5000
    timeout client 50000
    timeout server 50000
    timeout check 2000

    stats enable
    stats refresh 30s
    stats uri /ha_check
    stats hide-version
    stats auth admin:admin

frontend http-in
    bind *:80
    mode http
    option httplog
    log global
    default_backend http_pool

backend http_pool
    balance roundrobin
    option httpchk HEAD /index.html HTTP/1.0
    cookie SERVERID insert indirect
    server WEBSRV1 10.10.10.5:80 maxconn 1500 cookie SRV1 check inter 2000 rise 2 fall 3 weight 1
    server WEBSRV2 10.10.10.6:80 maxconn 1500 cookie SRV2 check inter 2000 rise 2 fall 3 weight 1

启动服务:
[root@rootop-haproxy ~]# /usr/local/haproxy/sbin/haproxy -f /usr/local/haproxy/conf/haproxy.cfg

stats uri /status #监控页面
stats auth admin:admin #监控页身份认证

提供一个管理脚本:

#!/bin/bash
PROXY_CONF=/usr/local/haproxy/conf/haproxy.cfg
PROXY_BIN=/usr/local/haproxy/sbin/haproxy
CHECK_PROXY=$(ps aux | grep "haproxy -f" | grep -v grep | wc -l)

case $1 in

        start)
        if [ $CHECK_PROXY -ge 1 ]; then
                echo "already running , exit"
                exit
        else
        $PROXY_BIN -f $PROXY_CONF
                if [ $? == 0 ]; then
                        echo "start ok"
                else
                        echo "start failed"
                fi
        fi
        ;;

        stop)
        kill -9 $(cat /tmp/haproxy.pid)
                if [ $? == 0 ]; then
                        rm -f /tmp/haproxy.pid && echo "stop ok"
                else
                        echo "stop failed"
                fi
        ;;

        restart)
        kill -9 $(cat /tmp/haproxy.pid)
                if [ $? == 0 ]; then
                        rm -f /tmp/haproxy.pid && echo "stop ok"
                else
                        echo "stop failed"
                fi

        $PROXY_BIN -f $PROXY_CONF
                if [ $? == 0 ]; then
                        echo "start ok"
                else
                        echo "start failed"
                fi
        ;;

        *)
        echo "only start/stop/restart"
esac

exit

然后通过多个客户端访问haproxy的ip地址可以看到后端不同服务器提供的内容。

cookie SERVERID insert indirect  其中这句设置了会话保持,通过插入cookie方式,测试效果。

访问到服务器1:

访问到服务器2:

浏览器关闭后,会话断开。