HAProxy是免费、高效、可靠的高可用及负载均衡解决方案。该软件非常适合处理高负载站点的七层数据请求。HAProxy的工作模式使其可以非常容易且安全地集成到我们现有的站点结构中。使用类似的代理软件还可以对外屏蔽内部的真实web服务器,防止内部服务器遭受外部攻击。终端用户通过访问HAProxy代理服务器获得站点页面,而代理服务器在收到客户端请求后会根据自身规则将数据请求转发给后端真实服务器。为了让同一客户端访问服务器时可以保持会话(同一客户端第二次访问网站时可以被转发至相同的后端真实服务器),HAProxy有三种解决方案:客户端IP,Cookie,以及Session。第一种方式中,HAProxy将客户端IP进行保存,以此确保当相同的IP访问代理服务器时可以转发至固定的真实服务器上。第二种方式中,HAProxy依靠真实服务器发送给客户端Cookie信息进行会话保持。第三种方式中,HAProxy将保存真实服务器的Session机服务器标识,实现会话保持功能。
HAProxy应用案例
本例将listen定义一个监控端口;使用frontend定义一个前端80端口;通过backend分别定义为inside_servers和external_servers的服务器组;使用default_backend定义默认服务器组为external_servers;定义ACL规则时,如果内网(192.168.85.0/24)访问Web服务,则由inside_servers服务器提供Web页面。
external_servers服务器组中包含web1.example.com和web2.example.com两台服务器,inside_servers服务器组中仅包含web3.example.com一台服务器。
服务器名称 网络配置
haproxy.example.com eth0:192.168.88.101
eth1:192.168.85.100
web1.example.com eth0:192.168.85.101
web2.example.com eth0:192.168.85.102
web3.example.com eth0:192.168.85.103
三台web服务器基本采用相同的设置,下面以web1为例,演示HAProxy可以轮询访问后端服务器。
vi /etc/sysconfig/network-scripts/ifcfg-eth0
1 2 3 4 5 6 7 8 9 10 | DEVICE=eth0 HWADDR=00:0C:29:D4:80:73 TYPE=Ethernet UUID=60861e68-3e9b-459f-803a-2e7118ba8ab5 ONBOOT=yes NM_CONTROLLED=yes BOOTPROTO=static IPADDR=192.168.85.101 NETMASK=255.255.255.0 GATEWAY=192.168.85.2 |
service network restart
yum install -y httpd;echo “rs1” > /var/www/html/index.html;service httpd start
service iptables stop
HAProxy代理服务器配置如下:
vi /etc/sysconfig/network-scripts/ifcfg-eth0
1 2 3 4 5 6 7 8 9 10 | DEVICE=eth0 HWADDR=00:0C:29:E8:EC:D9 TYPE=Ethernet UUID=aed0a4d3-0c22-476a-9a80-ec3fffeff195 ONBOOT=yes NM_CONTROLLED=yes BOOTPROTO=static IPADDR=192.168.88.101 NETMASK=255.255.255.0 GATEWAY=192.168.88.2 |
vi /etc/sysconfig/network-scripts/ifcfg-eth1
1 2 3 4 5 6 7 8 | DEVICE=eth1 TYPE=Ethernet ONBOOT=yes NM_CONTROLLED=yes BOOTPROTO=static IPADDR=192.168.85.100 NETMASK=255.255.255.0 GATEWAY=192.168.85.2 |
service network restart
service iptables stop
vi /etc/security/limits.conf
1 2 | * soft nofile 65535 * hard nofile 65535 |
yum -y install gcc
wget haproxy-1.4.24.tar.gz
tar -zxf haproxy-1.4.24.tar.gz -C /usr/src
cd /usr/src/haproxy-1.4.24/
make TARGET=linux26
make install
mkdir /var/haproxy
vi /etc/haproxy.cfg
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 | global #log 127.0.0.1 local0 log 127.0.0.1 local3 info #log loghost local0 info maxconn 4096 chroot /var/haproxy uid 99 gid 99 daemon nbproc 1 pidfile /var/run/haproxy.pid ulimit-n 65535 stats socket /var/tmp/stats #debug #quiet defaults log global mode http maxconn 20480 option httplog option httpclose option dontlognull option forwardfor option redispatch option abortonclose stats refresh 30 retries 3 balance roundrobin cookie SRV timeout check 2000ms timeout connect 5000ms timeout server 50000ms timeout client 50000ms listen admin_status bind 0.0.0.0:6553 mode http log 127.0.0.1 local3 info stats enable stats refresh 5s stats realm Haproxy\ Statistics stats uri /admin?stats stats auth admin1:AdMiN123 stats hide-version frontend web_service bind 0.0.0.0:80 mode http log global option httplog option httpclose option forwardfor acl inside_src src 192.168.85.0/24 use_backend inside_servers if inside_src default_backend external_servers backend external_servers mode http balance roundrobin option httpchk GET /index.html server web1 192.168.85.101:80 cookie web1 check inter 2000 rise 2 fall 3 weight 1 server web2 192.168.85.102:80 cookie web2 check inter 2000 rise 2 fall 3 weight 1 backend inside_servers mode http balance roundrobin option httpchk GET /index.html server web1 192.168.85.103:80 cookie web3 check inter 1500 rise 3 fall 3 weight 1 |
修改rsyslog:vi /etc/rsyslog.conf
1 2 3 | $ModLoad imudp $UDPServerRun 514 local3.* /var/log/haproxy.log |
重启rsyslog: service rsyslog restart
启动haproxy -f /etc/haproxy.cfg
通过访问http://192.168.88.101:6553/admin?stats查看代理服务器状态统计信息,配置客户端地址为192.168.88.200,访问192.168.88.101看是否能均衡到web1和web2上。配置客户端地址为192.168.85.0/24网络内IP,访问192.168.85.100,服务器返回页面应永远都是web3。