一。什么是HAPROXY


HAProxy提供高可用性、负载均衡以及基于TCP和HTTP应用的代 理,支持虚拟主机,它是免费、快速并且可靠的一种解决方案。HAProxy特别适用于那些负载特大的web站点,这些站点通常又需要会话保持或七层处理。HAProxy运行在当前的硬件上,完全可以支持数以万计的并发连接。并且它的运行模式使得它可以很简单安全的整合进您当前的架构中, 同时可以保护你的web服务器不被暴露到网络上。

HAProxy实现了一种事件驱动, 单一进程模型,此模型支持非常大的并发连接数。多进程或多线程模型受内存限制 、系统调度器限制以及无处不在的锁限制,很少能处理数千并发连接。事件驱动模型因为在有更好的资源和时间管理的用户端(User-Space) 实现所有这些任务,所以没有这些问题。此模型的弊端是,在多核系统上,这些程序通常扩展性较差。这就是为什么他们必须进行优化以 使每个CPU时间片(Cycle)做更多的工作。

----百度百科

关键词:反向代理 4层代理;7层代理

负载均衡

弹性2叉树

事件驱动

单进程

用户空间


二。HAPROXY的相关应用

1.反向代理

2.mysql负载均衡

3.RS健康状态检测

三。HAPROXY配置

haproxy接受2个类型的参数

1.命令行参数,可用# haprxoy -h 查看

2.配置文件,默认在/etc/haproxy/haproxy.conf

四。HAPROXY安装

官网:www.haproxy.1wt.eu

1.rpm包安装

2.通用2进制包,链接glibc2.2

3.源码安装,最新稳定版为1.4


五。HAPROXY配置文件

默认配置文件为/etc/haproxy/haproxy.conf

包含2个大区域:global和proxy

下面贴出一个示例配置文件

#---------------------------------------------------------------------

# 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 (定义日志发送的ip,级别,配合rsyslog使用,

在/etc/rsyslog.conf里添加 local2.* /var/log/haproxy.log)


chroot /var/lib/haproxy (保持默认)

pidfile /var/run/haproxy.pid

maxconn 4000 (最大连接数,超过此数,后续连接需排队)

user haproxy

group haproxy

daemon (以服务方式运行)


# turn on stats unix socket

stats socket /var/lib/haproxy/stats (stats的套接字位置)

#debug (调试模式,注意不能跟daemon一起使用)


#---------------------------------------------------------------------

# common defaults that all the 'listen' and 'backend' sections will

# use if not designated in their block

#---------------------------------------------------------------------

defaults (proxy默认配置,对任何backend,frontend,listen都生效,除非重复定义)

mode http (运行模式,还可以是tcp)

log global(遵从全局日志的设定)

option httplog(http格式的log信息)

option dontlognull (不记录空日志)

option http-server-close (服务器端关闭)

option forwardfor except 127.0.0.0/8(除了本地回环,对请求添加x-forwarded-for 头部)

option redispatch (分配的RS挂掉时,强制重定向到其他RS)

retries 3

timeout http-request 10s

timeout queue 1m

timeout connect 10s(连接超时时间)

timeout client 1m

timeout server 1m

timeout http-keep-alive 10s(http持久连接超时时间)

timeout check 10s(健康状态检测超时时间)

maxconn 3000(最大连接数)


#---------------------------------------------------------------------

# main frontend which proxys to the backends

#---------------------------------------------------------------------

frontend www.test.com (定义一个前端借口)

bind *:80(监听本机上任意地址的80端口)

acl url_static path_beg -i /static /p_w_picpaths /javascript /stylesheets

acl url_static path_end -i .jpg .gif .png .css .js (定义'url_static'控制列表,包含2种匹配规则)


use_backend static if url_static (如果匹配'url_static’规则,则使用static后端

default_backend url_static(默认后端)


#---------------------------------------------------------------------

# static backend for serving up p_w_picpaths, stylesheets and such

#---------------------------------------------------------------------

backend static

balance roundrobin (负载均衡算法)

server s1 127.0.0.1:8080 check weight 3

server s2 172.16.23.12:80 check weight 2


listen stats (规定一个listen,自带前后端)

mode http

bind 0.0.0.0:1080

stats enable (开启haproxy的stats功能

stats hide-version (隐藏haproxy的版本号,增加安全性)

stats uri /haproxy (访问stats的url路径)

stats realm Haproxy\ Statistics (访问受限区域的提示语,空格用'\'转义)

stats auth admin:admin(规定一个用户,并不是管理员)

stats admin if TRUE(规定是否拥有管理权限, 建议配合 if LOCALHOST)


#---------------------------------------------------------------------

# round robin balancing between the various backends

#---------------------------------------------------------------------

#backend app

# balance roundrobin

# server app1 127.0.0.1:5001 check

# server app2 172.16.23.12 check


其他重要的变量



六。haproxy负载均衡算法

1.round-robin ,后端RS权重可动态调整,最大连接数有4128的上限

2.static-rr ,后端RS调整权重无效,无最大连接数限制

3.leastconn,适用于长连接的会话,如mysql,ldap,rdp等

4.source,cip-hash/all-weight, 如果总权重发生改变,导向会改变,默认为静态,受hash-type影响

5.uri ,uri-path-hash/all-weight,同上,用于代理缓存

6.url_param, param-hash/all-weight 同上,用于追踪用户标识

7.hdr(<name>) 检索http的特定头部信息用作hash

附:hash-type

1.map-based 定义用于将hash码映射至后端服务器的方法,将后端服务器抽象为一个固定位置的数组,用来分配请求(静态)

2.consistent 将后端服务器抽象成一个树,跟请求的hash码最近的服务器将被选中,会受到总权重变化的影响(动态)


七。haproxy均衡mysql负载

frontend mysql

bind *:3306

mode tcp

log global

default_backend mysqlservers

backend mysqlservers

balance leastconn

server dbsrv1 192.168.10.11:3306 check port 3306 intval 2 rise 1 fall 2 maxconn 300

server dbsrv2 192.168.10.12:3306 check port 3306 intval 2 rise 1 fall 2 maxconn 300


八。acl的相关写法


九。uri重定向



十。haproxy动静分离

由于haproxy不理解fastcgi协议,因此不能直接反向代理php-fpm

可以代理jsp,但连接方式只能是http

十一。haproxy高可用

建议配合keepalived。