在生产环境中,在7层处理上使用HAProxy作为昂贵的高端硬件负载均衡设备故障故障时的紧急解决方案也时长可见。硬件负载均衡设备在“报文”级别处理请求,这在支持跨报文请求(request across multiple packets)有着较高的难度,并且它们不缓冲任何数据,因此有着较长的响应时间。对应地,软件负载均衡设备使用TCP缓冲,可建立极长的请求,且有着较大的响应时间。
HAProxy的优点:
1、HAProxy是支持虚拟主机的,可以工作在4、7层(支持多网段);
2、能够补充Nginx的一些缺点比如Session的保持,Cookie的引导等工作;
3、支持url检测后端的服务器;
4、它跟LVS一样,本身仅仅就只是一款负载均衡软件;单纯从效率上来讲HAProxy更会比Nginx有更出色的负载均衡速度,在并发处理上也是优于Nginx的;
5、HAProxy可以对Mysql读进行负载均衡,对后端的MySQL节点进行检测和负载均衡,不过在后端的MySQL slaves数量超过10台时性能不如LVS;
6、HAProxy的算法较多,达到8种; 六、HAProxy的安装
HAProxy的安装可以进行源码编译,也可以使用base源中的rpm包,因为yum源中的软件包版本已经很新,我这边建议如果没有特殊需求,还是使用yum源中的软件包进行安装,简单方便。
# yum install haproxy -y
安装好之后可以通过rpm -ql haproxy查看安装了哪些文件,我们主要操作的也就是他的配置文件,如果需要特殊可以下载源码包,解压查看README进行编译安装,这里不再介绍。 七、HAProxy的配置详解
请查看 ◆ 配置参数详解指南
我的实验环境是一台服务器安装了三个tomcat容器,前端用haproxy对三个tomcat负载均衡,配置如下:
#---------------------------------------------------------------------
# Example configuration for a possible web application. See the
# full configuration options online.
#
# http://haproxy.1wt.eu/download/1.4/doc/configuration.txt
#
#---------------------------------------------------------------------
#---------------------------------------------------------------------
# Global settings
#---------------------------------------------------------------------
global
# to have these messages end up in /var/log/haproxy.log you will
# need to:
#
# 1) configure syslog to accept network log events. This is done
# by adding the '-r' option to the SYSLOGD_OPTIONS in
# /etc/sysconfig/syslog
#
# 2) configure local2 events to go to the /var/log/haproxy.log
# file. A line like the following can be added to
# /etc/sysconfig/syslog
#
# local2.* /var/log/haproxy.log
#
log 127.0.0.1 local2 warning #设定日志级别为警告
chroot /var/lib/haproxy
pidfile /var/run/haproxy.pid
maxconn 400000 #设定每个进程最大连接数
# nbproc 3 #启动haproxy进程数,需要绑定stats
user haproxy
group haproxy
daemon
# turn on stats unix socket
stats socket /var/lib/haproxy/stats
#---------------------------------------------------------------------
# common defaults that all the 'listen' and 'backend' sections will
# use if not designated in their block
#---------------------------------------------------------------------
defaults
mode http #启用http模式
log global
option httplog
option dontlognull
option http-server-close
option forwardfor except 127.0.0.0/8
option redispatch
option httpclose
option http-pretend-keepalive
option forceclose
retries 3
timeout http-request 10s
timeout queue 1m
timeout connect 10s
timeout client 1m
timeout server 1m
timeout http-keep-alive 10s
timeout check 10s
listen stats
mode http
bind 0.0.0.0:1080
stats enable
stats hide-version
stats uri /haproxy?status
stats realm Haproxy\ Statistics
stats auth admin:admin
# stats admin if TRUE
#---------------------------------------------------------------------
# main frontend which proxys to the backends
#---------------------------------------------------------------------
#frontend main *:5000
# acl url_static path_beg -i /static /images /javascript /stylesheets
# acl url_static path_end -i .jpg .gif .png .css .js
# use_backend static if url_static
# default_backend app
frontend adscans *:80
default_backend adscan
#---------------------------------------------------------------------
# static backend for serving up images, stylesheets and such
#---------------------------------------------------------------------
#backend static
# balance roundrobin
# server static 127.0.0.1:4331 check
backend web
balance roundrobin
server web01 127.0.0.1:8080 check inter 15000 rise 2 fall 4
server web02 127.0.0.1:8081 check inter 15000 rise 2 fall 4
server web03 127.0.0.1:8082 check inter 15000 rise 2 fall 4 inter 15000 每隔15秒检测一次 rise 监测2次OK添加到负载均衡队列 监测4次失败移除队列。 八、启动
# service haproxy start
# netstat -tlnp
Active Internet connections (only servers)
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 5038/haproxy
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 5038/haproxy 九、状态介绍
Queue
Cur: current queued requests //当前的队列请求数量
Max:max queued requests //最大的队列请求数量
Limit: //队列限制数量
Session rate(每秒的连接回话)列表:
scur: current sessions //每秒的当前回话的限制数量
smax: max sessions //每秒的新的最大的回话量
slim: sessions limit //每秒的新回话的限制数量 Sessions
Total: //总共回话量
Cur: //当前的回话
Max: //最大回话
Limit: //回话限制
Lbtot: total number of times a server was selected //选中一台服务器所用的总时间 Bytes
In: //网络的字节数输入总量
Out: //网络的字节数输出总量 Denied
Req: denied requests//拒绝请求量
Resp:denied responses //拒绝回应 Errors
Req:request errors //错误请求
Conn:connection errors //错误的连接
Resp: response errors (among which srv_abrt) ///错误的回应 Warnings
Retr: retries (warning) //重新尝试
Redis:redispatches (warning) //再次发送
Server列表:
Status:状态,包括up(后端机活动)和down(后端机挂掉)两种状态
LastChk: 持续检查后端服务器的时间
Wght: (weight) : 权重
Act: server is active (server), number of active servers (backend) //活动链接数量
Bck: server is backup (server), number of backup servers (backend) //backup:备份的服务器数量
Down: //后端服务器连接后都是down的数量
Downtime: downtime: total downtime (in seconds) //总的downtime 时间
Throttle: warm up status //设备变热状态 十、总结