HAProxy介绍

本文介绍了如何利用haproxy和keepalived搭建高可用的负载均衡解决方案。haproxy作为反向代理服务器,提供双机热备和服务器健康检查功能,当后端服务器故障时,能自动进行故障转移。新版本的haproxy引入了frontend和backend概念,允许通过HTTP请求头内容进行路由规则匹配。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

haproxy+keepalived实现高可用负载均衡

  我的环境:
haproxy keepalived 主:192.168.1.192
haproxy keepalived 备:192.168.1.193

vip:192.168.1.200
web:192.168.1.187:80 192.168.1.187:8000



一:安装过程,在192.168.1.192上:
keepalived的安装:
#tar -zxvf keepalived-1.1.17.tar.gz
#ln -s /usr/src/kernels/2.6.18-128.el5-i686/ /usr/src/linux
#cd keepalived-1.1.17
#./configure --prefix=/ --mandir=/usr/local/share/man/ --with-kernel-dir=/usr/src/kernels/2.6.18-128.el5-i686/
#make && make install
#cd /etc/keepalived/
#mv keepalived.conf keepalived.conf.default
#vi keepalived.conf
! Configuration File for keepalived

vrrp_script chk_http_port {
script "/etc/keepalived/check_haproxy.sh"
interval 2
weight 2

global_defs {
router_id LVS_DEVEL
}
vrrp_instance VI_1 {
state MASTER #192.168.1.193上改为BACKUP
interface eth0
virtual_router_id 51 
priority 150 #192.168.1.193上改为120

advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}

track_script {
chk_http_port
}

virtual_ipaddress {
192.168.1.200 

}
}
}

#vi /etc/keepalived/check_haproxy.sh
#!/bin/bash
A=`ps -C haproxy --no-header |wc -l`
if [ $A -eq 0 ];then
/usr/local/haproxy/sbin/haproxy -f /usr/local/haproxy/conf/haproxy.cfg
sleep 3
if [ `ps -C haproxy --no-header |wc -l` -eq 0 ];then
/etc/init.d/keepalived stop
fi
fi
#chmod 755 /etc/keepalived/check_haproxy.sh

haproxy的安装(主备都一样):
#tar -zxvf haproxy-1.4.9.tar.gz
#cd haproxy-1.4.9

#make TARGET=linux26 PREFIX=/usr/local/haproxy install
#cd /usr/local/haproxy/
#mkdir conf logs
#cd conf
#vi haproxy.cfg
global
log 127.0.0.1 local3 info
maxconn 4096
user nobody
group nobody
daemon
nbproc 1
pidfile /usr/local/haproxy/logs/haproxy.pid

defaults
maxconn 2000
contimeout 5000
clitimeout 30000
srvtimeout 30000
mode http
log global
log 127.0.0.1 local3 info
stats uri /admin?stats
option forwardfor

frontend http_server
bind :80
log global
default_backend info_cache
acl test hdr_dom(host) -i test.domain.com
use_backend cache_test if test

backend info_cache
#balance roundrobin
balance source
option httpchk HEAD /haproxy.txt HTTP/1.1\r\nHost:192.168.1.187
server inst2 192.168.1.187:80 check inter 5000 fall 3



backend cache_test
balance roundrobin
#balance source
option httpchk HEAD /haproxy.txt HTTP/1.1\r\nHost:test.domain.com
server inst1 192.168.1.187:8000 check inter 5000 fall 3


二:再两台机器上都分别启动:
/etc/init.d/keepalived start (这条命令会自动把haproxy启动)

三:测试:
1.再两台机器上分别执行ip add
主: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast qlen 1000
link/ether 00:0c:29:98:cd:c0 brd ff:ff:ff:ff:ff:ff
inet 192.168.1.192/24 brd 192.168.1.255 scope global eth0
inet 192.168.1.200/32 (vip)scope global eth0  
inet6 fe80::20c:29ff:fe98:cdc0/64 scope link
valid_lft forever preferred_lft forever

备: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast qlen 1000
link/ether 00:0c:29:a6:0c:7e brd ff:ff:ff:ff:ff:ff
inet 192.168.1.193/24 brd 255.255.255.254 scope global eth0
inet6 fe80::20c:29ff:fea6:c7e/64 scope link
valid_lft forever preferred_lft forever

2.停掉主上的haproxy,3秒后keepalived会自动将其再次启动
3.停掉主的keepalived,备机马上接管服务

备: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast qlen 1000
link/ether 00:0c:29:a6:0c:7e brd ff:ff:ff:ff:ff:ff
inet 192.168.1.193/24 brd 255.255.255.254 scope global eth0
inet 192.168.1.200/32 scope global eth0
inet6 fe80::20c:29ff:fea6:c7e/64 scope link
valid_lft forever preferred_lft forever

4.更改hosts
192.168.1.200 test.com
192.168.1.200 test.domain.com
通过IE测试,可以发现
test.com的请求发向了192.168.1.187:80
test.domain.com的请求发向了192.168.1.187:8000

反向代理服务器,支持双机热备支持虚拟主机,但其配置简单,拥有非常不错的服务器健康检查功能,当其代理的后端服务器出现故障, HAProxy会自动将该服务器摘除,故障恢复后再自动将该服务器加入。新的1.3引入了frontend,backend;frontend根据任意 HTTP请求头内容做规则匹配,然后把请求定向到相关的backend.





测试环境描述:
网络拓扑结构如下图:
软负载均衡之haproxy(二)
共涉及四台服务器、一台客户端设备。
分别为:
主haproxy+主keepalived  192.168.71.128   虚拟IP:192.168.71.200
辅haproxy+辅keepalived  192.168.71.138   虚拟IP:192.168.71.200

www.bobo365.com   192.168.71.200(DNS服务器192.168.71.138兼)

后端服务器:
server01:192.168.71.135  (Windows下 apache+php+mysql)
server02: 192.168.71.136 (centos 6.2下nginx+php+mysql)

客户端:192.168.71.1

所要达到的目的:

1、主辅haproxy服务器为实际后端服务器提供负载均衡。
2、server01或者server02任何一台宕机,不影响用户访问。
3、主辅haproxy任何一台宕机,不影响用户访问。


安装配置:
1、实际服务器分别能单独访问:
http://192.168.71.135
http://192.168.71.136
http://192.168.71.138:8080
2、分别在主辅haproxy服务器上安装配置haproxy和keepalived。
(1)192.168.71.128
haproxy.cfg配置(/usr/local/haproxy):

global
        log 127.0.0.1   local0
        maxconn 4096
        chroot /usr/local/haproxy
        uid 501
        gid 501
        daemon
        nbproc 1
        pidfile /usr/local/haproxy/haproxy.pid
#       debug
#      quiet


defaults
        log     127.0.0.1       local3
        mode    http
        option httplog
        option httpclose
        option dontlognull
        option forwardfor
        option redispatch
        retries 2
        maxconn 2000
       # balance roundrobin
        balance leastconn
        stats   uri     /haproxy-stats
 stats realm Haproxy\ statistics
        stats auth admin:bobo365
        stats hide-version
        contimeout      5000
        clitimeout      50000
        srvtimeout      50000

listen web_proxy www.bobo365.com:80
        option httpchk GET /index.php  HTTP/1.1\r\nHOST:www.bobo365.com
        #option httpchk HEAD /index.php  HTTP/1.1\r\nHOST:\www.bobo365.com
        #option httpchk HEAD /index.php  HTTP/1.0/r/nHOST:/www.bobo365.com
        server web1_192.168.71.138  192.168.71.138:8080 cookie app1inst1 check inter 2000 rise 2 fall 5
        server web2_192.168.71.136  192.168.71.136:80 cookie app1inst2 check inter 2000 rise 2 fall 5
        server web3_192.168.71.135  192.168.71.135:80 cookie app1inst3 check inter 2000 rise 2 fall 5



keepalived.conf配置(/etc/keepalived):

! Configuration File for keepalived

global_defs {
   notification_email {
     acassen@firewall.loc
     failover@firewall.loc
     sysadmin@firewall.loc
   }
   notification_email_from Alexandre.Cassen@firewall.loc
   smtp_server 192.168.200.1
   smtp_connect_timeout 30
   router_id LVS_DEVEL
}

vrrp_instance VI_1 {
    state MASTER
    interface eth0
    virtual_router_id 51
    priority 100
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
        192.168.71.200
    }
}



(2)192.168.71.138

haproxy.cfg配置(/usr/local/haproxy):

global
        log 127.0.0.1   local0
        maxconn 4096
        chroot /usr/local/haproxy
        uid 501
        gid 501
        daemon
        nbproc 1
        pidfile /usr/local/haproxy/haproxy.pid
#       debug
#      quiet


defaults
        log     127.0.0.1       local3
        mode    http
        option httplog
        option httpclose
        option dontlognull
        option forwardfor
        option redispatch
        retries 2
        maxconn 2000
       # balance roundrobin
        balance leastconn
        stats   uri     /haproxy-stats
 stats realm Haproxy\ statistics
        stats auth admin:bobo365
        stats hide-version
        contimeout      5000
        clitimeout      50000
        srvtimeout      50000

listen web_proxy www.bobo365.com:80
        option httpchk GET /index.php  HTTP/1.1\r\nHOST:www.bobo365.com
        #option httpchk HEAD /index.php  HTTP/1.1\r\nHOST:\www.bobo365.com
        #option httpchk HEAD /index.php  HTTP/1.0/r/nHOST:/www.bobo365.com
        server web1_192.168.71.138  192.168.71.138:8080 cookie app1inst1 check inter 2000 rise 2 fall 5
        server web2_192.168.71.136  192.168.71.136:80 cookie app1inst2 check inter 2000 rise 2 fall 5
        server web3_192.168.71.135  192.168.71.135:80 cookie app1inst3 check inter 2000 rise 2 fall 5



keepalived.conf配置(/etc/keepalived):

! Configuration File for keepalived

global_defs {
   notification_email {
     acassen@firewall.loc
     failover@firewall.loc
     sysadmin@firewall.loc
   }
   notification_email_from Alexandre.Cassen@firewall.loc
   smtp_server 192.168.200.1
   smtp_connect_timeout 30
   router_id LVS_DEVEL
}

vrrp_instance VI_1 {
    state BACKUP
    interface eth0
    virtual_router_id 51
    priority 80
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
        192.168.71.200
    }
}



 其他:
1、检测keepalvied安装是否成功:(ip a命令)
192.168.71.128正常提供服务时:
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 16436 qdisc noqueue 
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
    inet6 ::1/128 scope host 
       valid_lft forever preferred_lft forever
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast qlen 1000
    link/ether 00:0c:29:e1:9a:1e brd ff:ff:ff:ff:ff:ff
    inet 192.168.71.128/24 brd 192.168.71.255 scope global eth0
     inet 192.168.71.200/32 scope global eth0
    inet6 fe80::20c:29ff:fee1:9a1e/64 scope link 
       valid_lft forever preferred_lft forever
3: sit0: <NOARP> mtu 1480 qdisc noop 
    link/sit 0.0.0.0 brd 0.0.0.0

192.168.71.128宕机时192.168.71.138状态:

1: lo: <LOOPBACK,UP,LOWER_UP> mtu 16436 qdisc noqueue 
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
    inet6 ::1/128 scope host 
       valid_lft forever preferred_lft forever
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast qlen 1000
    link/ether 00:0c:29:6d:6b:18 brd ff:ff:ff:ff:ff:ff
    inet 192.168.71.138/24 brd 192.168.71.255 scope global eth0
     inet 192.168.71.200/32 scope global eth0
    inet6 fe80::20c:29ff:fe6d:6b18/64 scope link 
       valid_lft forever preferred_lft forever
3: sit0: <NOARP> mtu 1480 qdisc noop 
    link/sit 0.0.0.0 brd 0.0.0.0

2、启动关闭命令:
关闭:killall -9 haproxy
启动:/usr/local/haproxy/sbin/haproxy -f /usr/local/haproxy/haproxy.cfg

/etc/init.d/keepalived stop|start|restart

3、检测页面:http://www.bobo365.com/haproxy-stats
软负载均衡之haproxy(二)




------------------------
新增配置(20120614补充)
haproxy.cfg:(www.bobo365.com/bobo365.com均可访问,主辅之间无缝切换)

global
            log 127.0.0.1    local0
            maxconn 51200
            chroot /usr/local/haproxy
            uid 501
            gid 501
            daemon
            nbproc 1
            pidfile /usr/local/haproxy/haproxy.pid
#          debug
#          quiet


defaults
            log       global
            log       127.0.0.1          local3   notice
            mode      http
            option httplog
            option httpclose
            option dontlognull
            option forwardfor
            option redispatch
            retries 3
            maxconn 51200
           # balance roundrobin
            balance leastconn
            stats    uri       /haproxy-stats
            stats realm Haproxy\ statistics
            stats auth admin:bobo365
            stats hide-version
            contimeout         5000
            clitimeout         50000
            srvtimeout         50000

frontend            http-in
bind      *:80
            acl host_www hdr_beg(host) -i www.
            use_backend www_bobo365 if host_www
            acl bobo365_com hdr_beg(host) -i bobo365.com
            use_backend bobo365_com if bobo365_com

backend www_bobo365                    
     balance leastconn
     option forwardfor
     option httpchk HEAD /index.php HTTP/1.1\r\nHost:\ www.bobo365.com
server web1_71.136_80 192.168.71.136:80 check inter 5s rise 2 fall 5
#server web2_71.136_8000 192.168.71.136:8000 check inter 5s rise 2 fall 5


backend bobo365_com
     balance leastconn
     option forwardfor
     option httpchk HEAD /index.php HTTP/1.1\r\nHost:\ bobo365.com
server web1_71.136_80 192.168.71.136:80 check inter 5s rise 2 fall 5
#server web2_71.136 192.168.71.136:8000 check inter 5s rise 2 fall 5


检测状态截图:
软负载均衡之haproxy(二)

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值