1 工作在ISO 七层 根据http协议(或者工作在ISO四层 根据tcp协议) 提供web服务的负载均衡调度器
1
2
3
4
5
6
7
8
9
10
11
12
13
|
负载均衡调度器分类
工作在四层:
工作在七层:
以上程序都可以实现服务的向外拓展;
|
haproxy特性
haproxy 当前版本为1.3 1.4,下面我们介绍1.4版本的特性
1
2
3
4
5
6
7
8
9
10
11
12
|
|
官网站点:haproxy.1wt.eu
haproxy 架构图

haproxy.cfg 配置文件详解
1
2
3
4
|
安装
配置文件路径
/etc/haproxy/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
|
(1)配置由两部分组成
defaults
frontend
backend
listen
其中defaults为proxies提供默认属性,frontend接受客户端的请求,backend连接后端的上游服务器(类似于nginx的upstream),listen是特定的frontend与backend的组合
(2)定义一个完整的代理的方式:
frontend
backend
listen
(3)defaults段分析
|
负载均衡调度方法
1
|
格式:balance roundrobin| static-rr| leastconn | source | uri | uri_param | hdr(<name>) | rdp-cookie(name)
|
1
2
3
4
5
6
7
8
9
10
11
12
|
调度方法的使用总结
leastconn
roundrobin
source 或者 cookie
uri
hash - type :
map-based (默认的静态的 hash 表)
consistent(动态的一致性 hash ) ---------在后端的cache服务器上使用,否则会导致服务器的加入或者退出时 服务器群瘫痪
|
haproxy 的工作模式 (使用mode参数)
1
2
3
4
5
6
7
|
http :http协议 --------haproxy的价值体现于此
tcp :haproxy在客户端和upstream server之间建立一个全双工的连接
|
指定haproxy日志
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
实例:在frontend中定义一个日志
(1)编辑rsyslog.conf
local3.* /var/log/hawebsrv .log
(2)编辑haproxy.cfg
frontend websrv
log 127.0.0.1 local3
bind *:80
default_backend webservers
(3)haproxy服务器测试
|
haproxy中的ACL
1
2
3
4
5
6
|
格式:acl <aclname> <criterion> [flags] [oprator] <value>
value: 支持整数或者整数范围
支持字符串
支持正则表达式
支持ip地址和网络地址
|
1
2
3
|
实现访问控制
http-request:7层过滤 (借助于定义好的acl实现)
tcp-request: 4层过滤 (借助于定义好的acl实现)
|
haproxy 动静分离的实现
架构图

1、 环境配置
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
|
haproxy服务器配置
外网网卡
内网网卡
两台上游服务器配置
server1 配置
提供页面
<h1>node1.linux.com< /h1 >
server2配置
提供页面
<h1>node2.linux.com<h1>
|
2、 安装配置haproxy
1
2
3
4
5
6
7
8
|
frontend websrv *:80
default_backend webservers
backend webservers
balance roundrobin
server node1 192.168.20.11:80 check
server node2 192.168.20.12:80 check
|
3、 客户端测试


此时说明 haproxy服务器将客户的请求以roundrobin算法 反向代理给后端的服务器!
4、启用全局日志功能
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
(一)编辑rsyslog.conf
$ModLoad imudp
$UDPServerRun 514
$ModLoad imtcp
$InputTCPServerRun 514
添加日志
local2.* /var/log/haproxy .log
(二)编辑haproxy.cfg
log 127.0.0.1 local2
(三)日志查看
|

接下来让我们来拓展haproxy的功能
5 、调度算法 uri的实现
1
2
3
4
5
6
7
8
9
|
1)后端服务器 server1 与 server2 同时创建多个页面
server1
server2 方法同 server1
2)更改haproxy.cfg的配置文件中的调度算法
balance uri
3)客户端测试
|

基于172.16.13.2/test1.html 该uri,haproxy服务器反向代理至后台服务器至同一台服务器server2
6、基于cookie实现会话绑定
1
2
3
4
5
6
7
8
9
10
11
12
|
1)编辑haproxy.cfg配置文件
frontend websrv
bind *:80
default_backend webservers
backend webservers
cookie node insert nocache
balance roundrobin
server node1 192.168.20.11:80 check cookie node1
server node2 192.168.20.12:80 check cookie node2
2)客户端测试
|

上图可见,基于cookie实现了客户端的请求与后端服务器server2的会话绑定。
7、haproxy管理界面---stats enable
1
2
3
4
5
6
7
8
|
listen statspage
bind *:8009 -------侦听端口
stats enable -------开启stats
stats hide-version -----隐藏版本
stats auth admin:admin ----登录验证信息
stats admin if TRUE ----实现在管理界面上对所有backend服务器管理
stats uri /admin ?stats ----登录的uri路径
|

8、haproxy动静分离的实现
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
|
1)server2 服务器安装php
提供php动态页面
<h1>node2.linux.com< /h1 >
<?php
phpinfo();
?>
2)重新配置haproxy配置文件
frontend websrvs
bind *:80
acl url_static path_beg -i /static /images /javascript /stylesheets
acl url_static path_end -i .jpg .gif .png .css .js .html
acl host_static hdr_beg(host) -i img. video. download. ftp . imags. videos.
acl url_php path_end -i .php
use_backend static if url_static or host_static
use_backend dynamic if url_php
default_backend dynamic
backend static
balance roundrobin
server node1 192.168.20.11:80 check maxconn 30000
backend dynamic
balance roundrobin
server node2 192.168.20.12:80 check maxconn 1000
3) 客户端测试 动静分离
|


如图所示:静态页面由server1服务器显示,动态页面由server2 显示。
本文转自 dengaosky 51CTO博客,原文链接:http://blog.51cto.com/dengaosky/2050317,如需转载请自行联系原作者