简介:
软件负载均衡一般通过两种方式来实现:基于操作系统的软负载实现和基于第三方应用的软负载实现。LVS就是基于Linux操作系统实现的一种软负载,HAProxy就是开源的并且基于第三应用实现的软负载。
HAProxy支持两种主要的代理模式:"tcp"也即4层(大多用于邮件服务器、内部协议通信服务器等),和7层(HTTP)。在4层模式 下,HAproxy仅在客户端和服务器之间转发双向流量。7层模式下,HAProxy会分析协议,并且能通过允许、拒绝、交换、增加、修改或者删除请求 (request)或者回应(response)里指定内容来控制协议,这种操作要基于特定规则。
特点:
haproxy支持四七层,而LVS是四层应用,nginx是七层应用
支持双机热备,高可用,负载均衡、虚拟主机,应用代理、
基于TCP和HTTP的 图形界面查看信息
服务器节点健康检查功能
一般是lvs在前面做4层,haproxy在后面做七层。
HAproxy配置文件可以分为五部分:
1 2 3 | global : 全局配置参数段,主要用来控制Haproxy启动前的进程及系统相关设置 defaults: 配置一些默认参数,如果frontend,backend,listen等段未设置则使用defaults段设置 |
1 | listen: <br>frontend: 用来匹配接收客户所请求的域名,uri等,并针对不同的匹配,做不同的请求处理 <br>backend: 定义后端服务器集群,以及对后端服务器的一些权重、队列、连接数等选项的设置 |
1.安装
1 2 3 4 5 6 7 8 9 | wget http: / / haproxy. 1wt .eu / download / 1.3 / src / haproxy - 1.3 . 20.tar .gz tar zcvf haproxy - 1.3 . 20.tar .gz cd haproxy - 1.3 . 20 make TARGET = linux26 PREFIX = / usr / local / haproxy make install PREFIX = / usr / local / haproxy (注意,TARGET后面根据本机操作系统内核版本来填写,PREFIX是要安装到的目录) net.ipv4.ip_forward = 1 #基于NAT模式的负载均衡器都要打开系统转发功能。 sysctl - p #使修改生效 |
2.配置
安装完毕后,进入安装目录配置文件,默认情况下目录里是没有.cfg配置文件的,可以回到安装文件目录下将examples下的haproxy.cfg拷贝到usr/local/haproxy下。
cd /usr/local/haproxy
vi 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 70 71 72 73 74 75 | 根据实际需求,更改配置文件,我的配置如下
global #全局设置
log 127.0 . 0.1 local0 #日志输出配置,所有日志都记录在本机,通过local0输出
#log loghost local0 info
maxconn 4096 #最大连接数
chroot / usr / local / haproxy
uid 99 #所属运行的用户uid
gid 99 #所属运行的用户组
daemon #以后台形式运行haproxy
nbproc 2 #启动2个haproxy实例
pidfile / usr / local / haproxy / haproxy.pid #将所有进程写入pid文件
#debug
#quiet defaults #默认设置
#log global
log 127.0 . 0.1 local3 #日志文件的输出定向
mode http #默认的模式mode { tcp|http|health },tcp是4层,http是7层,health只会返回OK
option httplog #日志类别,采用httplog
option dontlognull
option forwardfor #如果后端服务器需要获得客户端真实ip需要配置的参数,可以从Http Header中获得客户端ip
option httpclose #每次请求完毕后主动关闭http通道,haproxy不支持keep-alive,只能模拟这种模式的实现
retries 3 #3次连接失败就认为服务器不可用,主要通过后面的check检查
option redispatch #当serverid对应的服务器挂掉后,强制定向到其他健康服务器
maxconn 2000 #最大连接数
stats uri / stats #haproxy 监控页面的访问地址
浏览器中输入haproxy监控地址:http: / / 10.10 . 100.39 / stats查看状态
contimeout 5000 #连接超时时间
clitimeout 50000 #客户端连接超时时间
srvtimeout 50000 #服务器端连接超时时间
stats auth admin:admin #设置监控页面的用户和密码:Frank
stats hide - version #隐藏统计页面的HAproxy版本信息 frontend http - in #前台
bind * : 80
mode http
option httplog
log global
acl web1 hdr(host) - i www. 9888.cn 9888.cn
#acl后面是规则名称,-i是要访问的域名,如果访问www.9888.cn 这个域名就分发到下面的webserver1 的作用域。
use_backend webserver1 if web1
backend webserver1 #后台
mode http
balance roundrobin #负载均衡算法
option httpchk / index.html #健康检查
server web01 10.10 . 100.41 : 80 check cookie 1 inter 2000 fall 3 weight 30
server web02 10.10 . 100.18 : 80 check cookie 2 inter 2000 fall 3 weight 30 #cookie 1表示serverid为1,check inter 1500 是检测心跳频率 #rise 2是2次正确认为服务器可用,fall 3是3次失败认为服务器不可用,weight代表权重. |
启动haproxy:
/usr/local/haproxy/sbin/haproxy -f /usr/local/haproxy/conf/haproxy.cfg
实现haproxy301域名跳转:
1 | redirect prefix http: / / oldboy.blog. 51.com code 301 if www. 9888.cn #当访问www.9888.cn时跳转至http://oldboy.blog.51.com |
实现haproxy基于URL地址目录做7层跳转
比如根据目录进行过滤转发:
1 2 3 4 5 | acl oldboy_java path_beg / java / acl oldboy_php path_beg / php /
use_backend webserver if oldboy_java #如果是java就找webserver这个池 use_backend webserver if oldboy_php |
实现haproxy基于扩展名做7层跳转:
1 2 | acl oldboy_pic path_end .gif .png .jpg .css .js use_backend nginxpools if olboy_static or oldboy_pic |
实现haproxy基于user_agent做7层跳转
1 2 3 4 5 6 7 | acl iphone_users hdr_sub(user - agent) - i iphone redirect prefix http: / / www. 51cto .com if iphone users acl android_users hdr_sub(user - agent) - i android redirect prefix http: / / www.baidu.com if android_users |
实现haproxy基于ip和端口过滤
1 2 3 | acl valid_ip src 192.168 . 1.0 / 24 block if !valid_ip #如果不符合valid_ip的规则就block拒绝掉 |
让haproxy错误页面优雅的显示
1 | errorfile 403 / tec / haproxy / errorfiles / 403forbild .html |
基于HTTP的直接IP URL方式的健康检查:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | 1. >第一种HEAD配置方法
option httpchk HEAD / check.html HTTP / 1.0
这种检测方式就相当于通过curl - i http: / / 127.0 . 0.1 / check.html
或者 wget http: / / 127.0 . 0.1 / check.html访问地址。
* * check.html文件必须在网站根目录下创建
健康检查的频率、时间等参数:
maxconn 控制节点的并发连接的
weight 12 权重,权重越大,请求越多
2. >第二种GET配置方式 GET后端server的web页面 option httpchk GET / index.html HTTP / 1.0 |
backup 和allbackups参数:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | server web1 10.10 . 100.66 : 80 check inter 2000 fall 3 weight 30 server web2 10.10 . 100.67 : 80 check inter 2000 fall 3 weight 30 server web3 10.10 . 100.68 : 80 check inter 2000 fall 3 weight 30 backup 当web1和web2服务停止后,web3再提供服务,这样可以达到高可用的目的 option allbackups server web1 10.10 . 100.66 : 80 check inter 2000 fall 3 weight 30 server web2 10.10 . 100.67 : 80 check inter 2000 fall 3 weight 30 server web3 10.10 . 100.68 : 80 check inter 2000 fall 3 weight 30 backup server web4 10.10 . 100.69 : 80 check inter 2000 fall 3 weight 30 backup 加上 option allbackups后,当web1和web2挂掉后,web3和web4都启动起来提供服务,不加allbackups则只有一台提供服务. |
haproxy下的RS无法记录客户端真实ip的问题
1 2 3 4 5 6 7 | 在haproxy配置文件里加入如下参数: listen www
option forwardfor
提示:参数最好放在listen www里面
然后在nginx日志格式中加 "$http_x_forwarded_for" |
###################################################################################
关于haproxy日志输出的问题:
CentoS6.5下HAProxy日志配置详解:
1 2 3 4 5 6 7 8 9 10 11 | syslog这个服务,在Centos5.x中的目录为: / etc / init.d / syslog 而到了Centos6.x中变成了: / etc / init.d / rsyslog 在配置前,我们先来了解下日志的level: local0~local7 16 ~ 23 保留为本地使用 emerg 0 系统不可用 alert 1 必须马上采取行动的事件 crit 2 关键的事件 err 3 错误事件 warning 4 警告事件 notice 5 普通但重要的事件 info 6 有用的信息 debug 7 调试信息 |
vim haproxy.conf(在default处添加如下信息)
1 2 3 4 5 6 | ######################################## defaults log global option httplog log 127.0 . 0.1 local3 ######################################## |
vim /etc/rsyslog.conf(添加如下内容)
1 | local3. * / var / log / haproxy.log |
vim /etc/sysconfig/rsyslog
1 2 | 把SYSLOGD_OPTIONS = "-m 0" 改成 SYSLOGD_OPTIONS = "-r -m 0" |
相关解释说明:
-r: 打开接受外来日志消息的功能,其监控514 UDP端口;
-x: 关闭自动解析对方日志服务器的FQDN信息,这能避免DNS不完整所带来的麻烦;
-m: 修改syslog的内部mark消息写入间隔时间(0为关闭),例如240为每隔240分钟写入一次"--MARK--"信息;
-h: 默认情况下,syslog不会发送从远端接受过来的消息到其他主机,而使用该选项,则把该开关打开,所有接受到的信息都可根据syslog.conf中定义的@主机转发过去.
配置完毕后关闭sellinux然后重启rsyslog和haproxy 即可.
1 | / etc / init.d / rsyslog restart |
haproxy实现负载均衡的方式:
haproxy +heartbeat
haproxy + keepalive