CentOS5.6 部署haproxy代理做负载均衡


Haproxy服务器:192.168.1.112  
Apache服务器:192.168.1.12:80(此为自己配置的一个简单apache网页服务器)  
百度服务器:  115.239.210.26:80(此为百度网页ip地址)  
  
由于访问百度网页的网友很多,会造成拥堵延时访问。现假设百度有2个网页服务器(一样),服务器ip地址分别为:192.168.1.12:80,115.239.210.26:80。当请求www.baidu.com 的时候,haproxy代理服务会平均分配请求次数到两个服务器中,达到了负载均衡的效果。若把192.168.1.12:80中的apache暂停(偶然破坏),则此服务不再被访问,所有访问都将请求115.239.210.26:80。

Haproxy服务器安装在虚拟机centos5.6中,apache网页服务在虚拟机window2003中,虚拟机都以桥接方式与主机连接,都设置成静态IP地址。因此两台虚拟机和主机形成了简单局域网。  
  
Window2003域名和IP绑定:  
在文件C:\WINDOWS\system32\drivers\etc\hosts 添加192.168.1.12 www.baidu.com  
  
CentOS域名和IP绑定:  
编辑文件vi /etc/hosts 添加 192.168.1.112 www.baidu.com  
  
如果想在主机访问测试:  
在文件C:\WINDOWS\system32\drivers\etc\hosts 添加192.168.1.112 www.baidu.com  


1.安装

cd /usr/local/
wget http://haproxy.1wt.eu/download/1.3/src/haproxy-1.3.20.tar.gz
tar zxvf haproxy-1.3.20.tar.gz
cd haproxy-1.3.20
make TARGET=linux26 PREFIX=/usr/local/haproxy   #将haproxy安装到/usr/local/haproxy
make install PREFIX=/usr/local/haproxy

2.配置

cd /usr/local/haproxy-1.3.20/examples/
cp haproxy.cfg/usr/local/haproxy/
cd usr/local/haproxy
vi haproxy.cfg

#修改前(默认配置):
# this config needs haproxy-1.1.28 or haproxy-1.2.1

global
        log 127.0.0.1   local0
        log 127.0.0.1   local1 notice
        #log loghost    local0 info
        maxconn 4096
        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  appli1-rewrite 0.0.0.0:10001
        cookie  SERVERID rewrite
        balance roundrobin
        server  app1_1 192.168.34.23:8080 cookie app1inst1 check inter 2000 rise 2 fall 5
        server  app1_2 192.168.34.32:8080 cookie app1inst2 check inter 2000 rise 2 fall 5
        server  app1_3 192.168.34.27:8080 cookie app1inst3 check inter 2000 rise 2 fall 5
        server  app1_4 192.168.34.42:8080 cookie app1inst4 check inter 2000 rise 2 fall 5

listen  appli2-insert 0.0.0.0:10002
        option  httpchk
        balance roundrobin
        cookie  SERVERID insert indirect nocache
        server  inst1 192.168.114.56:80 cookie server01 check inter 2000 fall 3
        server  inst2 192.168.114.56:81 cookie server02 check inter 2000 fall 3
        capture cookie vgnvisitor= len 32

        option  httpclose               # disable keep-alive
        rspidel ^Set-cookie:\ IP=       # do not let this cookie tell our internal IP address

listen  appli3-relais 0.0.0.0:10003
        dispatch 192.168.135.17:80

listen  appli4-backup 0.0.0.0:10004
        option  httpchk /index.html
        option  persist
        balance roundrobin
        server  inst1 192.168.114.56:80 check inter 2000 fall 3
        server  inst2 192.168.114.56:81 check inter 2000 fall 3 backup

listen  ssl-relay 0.0.0.0:8443
        option  ssl-hello-chk
        balance source
        server  inst1 192.168.110.56:443 check inter 2000 fall 3
        server  inst2 192.168.110.57:443 check inter 2000 fall 3
        server  back1 192.168.120.58:443 backup

listen  appli5-backup 0.0.0.0:10005
        option  httpchk *
        balance roundrobin
        cookie  SERVERID insert indirect nocache
        server  inst1 192.168.114.56:80 cookie server01 check inter 2000 fall 3
        server  inst2 192.168.114.56:81 cookie server02 check inter 2000 fall 3
        server  inst3 192.168.114.57:80 backup check inter 2000 fall 3
        capture cookie ASPSESSION len 32
        srvtimeout      20000

        option  httpclose               # disable keep-alive
        option  checkcache              # block response if set-cookie & cacheable

        rspidel ^Set-cookie:\ IP=       # do not let this cookie tell our internal IP address

        errorloc        502     http://192.168.114.58/error502.html
        errorfile       503     /etc/haproxy/errors/503.http

#修改后:
# this config needs haproxy-1.1.28 or haproxy-1.2.1

global
        log 127.0.0.1   local0
        #log loghost    local0 info
        maxconn 4096
        chroot /usr/local/haproxy
        nbproc 2
        uid 99
        gid 99
        daemon
        #debug
        #quiet
defaults
        log     global
        log     127.0.0.1       local0
        mode    http
        option  httplog
        option  dontlognull
        option  redispatch
        retries 3
        maxconn 2000
        contimeout      5000
        clitimeout      10000
        srvtimeout      10000
listen stats
        mode http
        bind 0.0.0.0:1080
        stats enable
        stats hide-version
        stats uri /haproxy-stats
        stats auth admin:admin
frontend http-in
        bind *:80
        mode http
        log global
        option httpclose
        option logasap
        option dontlognull
        option forwardfor
        default_backend servers
backend servers
        balance roundrobin
        server  s1 192.168.1.12:80 weight 3 check
        server  s2 115.239.210.26:80 weight 3 check


3.添加日志

在最下边增加日志:
vi /etc/syslog.conf
local0.*		/var/log/haproxy.log

修改日记参数:
vi /etc/sysconfig/syslog
 SYSLOGD_OPTIONS="-r -m 0"

重启日志服务:
service syslog restart

4.设置开机启动,为了方便系统在开机时加载,还可以创建启动脚本:

vi /etc/rc.d/init.d/haproxy
#!/bin/sh
set -e

PATH=/sbin:/bin:/usr/sbin:/usr/bin:/usr/local/haproxy/sbin
PROGDIR=/usr/local/haproxy
PROGNAME=haproxy
DAEMON=$PROGDIR/sbin/$PROGNAME
CONFIG=$PROGDIR/$PROGNAME.conf
PIDFILE=$PROGDIR/$PROGNAME.pid
DESC="HAProxy daemon"
SCRIPTNAME=/etc/init.d/$PROGNAME

# Gracefully exit if the package has been removed.
test -x $DAEMON || exit 0

start()
{
       echo -n "Starting $DESC: $PROGNAME"
       $DAEMON -f $CONFIG
       echo "."
}

stop()
{
       echo -n "Stopping $DESC: $PROGNAME"
       haproxy_pid=cat $PIDFILE
       kill $haproxy_pid
       echo "."
}

restart()
{
       echo -n "Restarting $DESC: $PROGNAME"
       $DAEMON -f $CONFIG -p $PIDFILE -sf $(cat $PIDFILE)
       echo "."
}

case "$1" in
 start)
       start
       ;;
 stop)
       stop
       ;;
 restart)
       restart
       ;;
 *)
       echo "Usage: $SCRIPTNAME {start|stop|restart}" >&2
       exit 1
       ;;
esac
exit 0

5.执行检查:

保存后赐予可执行权限:
chmod +x /etc/rc.d/init.d/haproxy
chmod +x /usr/local/haproxy/haproxy.cfg


启动 haproxy:(ntsysv)
cd /usr/local/haproxy/sbin
./haproxy -f /usr/local/haproxy/haproxy.cfg

6.常见问题:

[ALERT]109/203959 (6743) : Starting proxy stats: cannot bind socket

查看端口:

netstat –ntpl

Proto Recv-Q Send-Q Local Address  Foreign Address  State  PID/Program name

tcp  0  0 0.0.0.0:1080    0.0.0.0:*        LISTEN      5659/haproxy

 

1080或80端口被占用,删除:

kill 5659

 

再启动;  

./haproxy -f /usr/local/haproxy/haproxy.cfg


7.测试:

打开网页(成功):http://localhost/haproxy-stats



现在Haproxy服务器中打开百度网页 www.baidu.com,不停的用F5刷新,会看到网页时而

打开apache页面,时而打开百度页面(apache页面假设为百度的另一个服务页面)。





模拟一台服务器宕机,停止apache服务,此网页不可访问,因此打开的百度网页都请求到另一个服务中。



相关的haproxy代理统计信息可浏览http://localhost:1080/haproxy-stats查看




资料参考:

http反向代理之haproxy详解

Haproxy 配置项\配置实例

CentOS6下Haproxy的安装配置

Haproxy安装及配置

HAproxy均衡负载部署和配置文件详解










评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值