一、Haproxy的简介
Haproxy的官网站点:http://haproxy.com/
HAProxy提供高可用性、负载均衡以及基于TCP和HTTP应用的代 理,支持虚拟主机,它是免费、快速并且可靠的一种解决方案。HAProxy特别适用于那些负载特大的web站点,这些站点通常又需要会话保持或七层处理。HAProxy运行在当前的硬件上,完全可以支持数以万计的并发连接。并且它的运行模式使得它可以很简单安全的整合进您当前的架构中, 同时可以保护你的web服务器不被暴露到网络上。
HAProxy实现了一种事件驱动, 单一进程模型,此模型支持非常大的并发连接数。多进程或多线程模型受内存限制 、系统调度器限制以及无处不在的锁限制,很少能处理数千并发连接。事件驱动模型因为在有更好的资源和时间管理的用户端(User-Space) 实现所有这些任务,所以没有这些问题。此模型的弊端是,在多核系统上,这些程序通常扩展性较差。这就是为什么他们必须进行优化以 使每个CPU时间片(Cycle)做更多的工作。
摘自[百度百科]
二、Haproxy的工作模式
![1.png wKioL1RwGirTYixDAAENO0C4xYo238.jpg]()
大致的意思就是:可以分为两部分,
第一部分是互联网,包括互联网上众多的路由设备、Firewall等;
第二部分是本地的网络架构,包括Firewall,proxy、Web server,Database等
Haproxy的注释Local balanceer in proxy mode -- 工作在代理模下是一个负载均衡的功能
通过Haproxy代理后端的Web server群集,如果需要与数据库交互,有Web server与Database建立连接。
三、Haproxy的安装和配置说明
CentOS 6.4系统中yum源提供的Haproxy的版本为1.5.2,在这里我们直接用yum安装
1、安装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
|
/etc/haproxy
/etc/haproxy/haproxy
.cfg
/etc/logrotate
.d
/haproxy
/etc/rc
.d
/init
.d
/haproxy
/usr/bin/halog
/usr/bin/iprange
/usr/sbin/haproxy
/usr/share/doc/haproxy-1
.5.2
/usr/share/doc/haproxy-1
.5.2
/acl-content-sw
.cfg
/usr/share/doc/haproxy-1
.5.2
/acl
.fig
/usr/share/doc/haproxy-1
.5.2
/haproxy
.cfg
/usr/share/doc/haproxy-1
.5.2
/internals
/usr/share/haproxy
/usr/share/haproxy/400
.http
/usr/share/haproxy/403
.http
/usr/share/haproxy/408
.http
/usr/share/haproxy/500
.http
/usr/share/haproxy/502
.http
/usr/share/haproxy/503
.http
/usr/share/haproxy/504
.http
/usr/share/man/man1/halog
.1.gz
/usr/share/man/man1/haproxy
.1.gz
/var/lib/haproxy
.....
|
2、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
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
|
**********************************************************************
global
log 127.0.0.1 local2
chroot
/var/lib/haproxy
pidfile
/var/run/haproxy
.pid
maxconn 4000
user haproxy
group haproxy
daemon
stats socket
/var/lib/haproxy/stats
defaults
mode http
log global
option httplog
option dontlognull
option http-server-close
option forwardfor except 127.0.0.0
/8
option redispatch
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
maxconn 3000
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
backend static
balance roundrobin
server static 127.0.0.1:4331 check check:启动对后端server的健康状态检测
backend app
balance roundrobin
server app1 127.0.0.1:5001 check
server app2 127.0.0.1:5002 check
server app3 127.0.0.1:5003 check
server app4 127.0.0.1:5004 check
|
3、haproxy的命令详解
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
|
HA-Proxy version 1.5.2 2014
/07/12
Copyright 2000-2014 Willy Tarreau <w@1wt.eu>
Usage : haproxy [-f <cfgfile>]* [ -vdVD ] [ -n <maxconn> ] [ -N <maxpconn> ]
[ -p <pidfile> ] [ -m <max megs> ] [ -C <
dir
> ]
-
v
displays version ; -vv shows known build options.
-d enters debug mode ; -db only disables background mode.
-dM[<byte>] poisons memory with <byte> (defaults to 0x50)
-V enters verbose mode (disables quiet mode)
-D goes daemon ; -C changes to <
dir
> before loading files.
-q quiet mode : don't display messages
-c check mode : only check config files and
exit
-n sets the maximum total
-m limits the usable amount of memory (
in
MB)
-N sets the default, per-proxy maximum
-L
set
local
peer name (default to
hostname
)
-p writes pids of all children to this
file
-de disables epoll() usage even when available
-dp disables poll() usage even when available
-dS disables splice usage (broken on old kernels)
-dV disables SSL verify on servers side
-sf
/-st
[pid ]* finishes
/terminates
old pids. Must be last arguments.
|
4、配置Haproxy的日志
|
1
2
3
4
5
6
7
8
9
10
11
12
13
|
默认情况下,Haproxy没有启用日志文件,但是我们可以根据haproxy的配置文件做修改。
(1)修改系统日志的配置文件
SYSLOGD_OPTIONS=
"-c 2 -r"
(2)增加日志设备
local2.*
/var/log/haproxy
.log
(3)重启一下日志服务
关闭系统日志记录器: [确定]
启动系统日志记录器: [确定]
(4)查看日志记录信息
|
四、Haproxy负载均衡web
1、一个配置示例:
Haproxy-Server:192.168.0.105
Apache1-Client:192.168.0.102
Apache2-Client:192.168.0.106
|
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
76
77
78
79
|
**********************************************************************
global
log 127.0.0.1 local2
chroot
/var/lib/haproxy
pidfile
/var/run/haproxy
.pid
maxconn 4000
user haproxy
group haproxy
daemon
defaults
mode http
log global
option httplog
option dontlognull
option http-server-close
option forwardfor except 127.0.0.0
/8
option redispatch
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
maxconn 30000
listen stats
mode http
bind 0.0.0.0:1080
stats
enable
stats hide-version
stats uri
/haproxyadmin
?stats
stats realm Haproxy\ Statistics
stats auth admin:123456
stats admin
if
TRUE
frontend http-
in
bind *:80
mode http
log global
option httpclose
option logasap
option dontlognull
capture request header Host len 20
capture request header Referer len 60
default_backend servers
frontend healthcheck
bind :1099
mode http
option httpclose
option forwardfor
default_backend servers
backend servers
balance roundrobin
server websrv1 192.168.0.102:80 check maxconn 2000
server websrv2 192.168.0.106:80 check maxconn 2000
**********************************************************************
|
2、启动Haproxy服务
3、浏览器访问
单独访问Apache1:192.168.0.102
![2.png wKioL1RwMNjRicJhAADYLGQ6DcA929.jpg]()
单独访问Apache2:192.168.0.106
![3.png wKiom1RwMF2wp7cVAADauWnSFrk034.jpg]()
多次刷新访问Haproxy:192.168.0.105
多次刷新Haproxy的website,会切换到后端不同的webserver主机上。
![4.png wKioL1RwMNiQBz_SAADWTNm43hg230.jpg]()
![5.png wKiom1RwMF2DCah8AADjp38HsUI779.jpg]()
五、Haproxy的状态监控
1、Haproxy的监控页面
|
1
2
3
4
5
6
7
8
9
10
|
listen stats
mode http
bind 0.0.0.0:1080
stats
enable
stats refresh 3s
stats hide-version
stats uri
/haproxyadmin
?stats
stats realm Haproxy\ Statistics
stats auth admin:123456
stats admin
if
TRUE
|
2、访问监控页面
(1)登陆监控页面
admin/123456
![6.png wKioL1RwM3eilyGVAAFLazQ1qcw644.jpg]()
(2)查看监控页面
![7.png wKioL1RwNAHiKSM3AAk3jcCY9WM150.jpg]()
(3)模拟一下故障
(4)在次查看监控页面
![8.png wKiom1RwNFDy-iQ0AAJkwsA7ySE639.jpg]()
六、Haproxy的动静分离
![30.png wKioL1RwRGXCBmjGAAQN4elUoAE138.jpg]()
注释说明:
在这里说明动静分离的演示流程,后端分别三台负载均衡主机上放上同样的页面,静态页面index.html,动态页面a.php;然后访问haproxy主机的静态页面和动态页面,来判断访问的是那台主机
1、动静分离的主机分配
Haproxy:192.168.0.105 proxy
Apache1:192.168.0.102 static
Apache2:192.168.0.106 static
Nginx01:192.168.0.107 dynamic
2、浏览器分别访问各个主机
Apache1:192.168.0.102 static
![11.png wKioL1RwPmfzyADHAADlewFBd-U290.jpg]()
Apache1:192.168.0.102 dynamic
![12.png wKiom1RwPezDweGEAAI6yhvw3ks953.jpg]()
Apache1:192.168.0.106 static
![13.png wKioL1RwPmejLKdTAAD9CAERs3o326.jpg]()
Apache1:192.168.0.106 dynamic
![14.png wKiom1RwPezRRj0AAAFMOcYx8w0873.jpg]()
Nginx01:192.168.0.107 static
![15.png wKioL1RwPmiwb7H-AAHHny5ngjA254.jpg]()
Nginx01:192.168.0.107 dynamic
![16.png wKiom1RwPezgaVq_AAKd8OyTKFs632.jpg]()
3、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
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
76
77
|
global
log 127.0.0.1 local2
chroot
/var/lib/haproxy
pidfile
/var/run/haproxy
.pid
maxconn 4000
user haproxy
group haproxy
daemon
defaults
mode http
log global
option httplog
option dontlognull
option http-server-close
option forwardfor except 127.0.0.0
/8
option redispatch
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
maxconn 30000
listen stats
mode http
bind 0.0.0.0:1080
stats
enable
stats hide-version
stats uri
/haproxyadmin
?stats
stats realm Haproxy\ Statistics
stats auth admin:123456
stats admin
if
TRUE
frontend http-
in
bind *:80
mode http
log global
option httpclose
option logasap
option dontlognull
capture request header Host len 20
capture request header Referer len 60
acl url_static path_end -i .html .jpg .gif
acl url_dynamic path_end -i .php
default_backend servers
use_backend lnmmp
if
url_dynamic
backend servers
balance roundrobin
server websrv1 192.168.0.102:80 check rise 2 fall 1 weight 2 maxconn 2000
server websrv2 192.168.0.106:80 check rise 2 fall 1 weight 2 maxconn 2000
backend lnmmp
balance
source
server websrv3 192.168.0.107:80 check rise 2 fall 1 maxconn 2000
|
4、动静分离访问测试
![21.png wKioL1RwP5WDSfjjAAD4W8GHZqY597.jpg]()
![22.png wKiom1RwPxqgc-ybAAD-q3Vo2Lg725.jpg]()
![23.png wKioL1RwP5WD-NZPAAJ87xn_AF0588.jpg]()
七、近期网络架构
![近期网络架构规划.png wKioL1RwS_Ky77adAAYSgiqjz2Y603.jpg]()
期望网友能够指点架构的优缺点,后期将会发布此博文
本文转自zys467754239 51CTO博客,原文链接:http://blog.51cto.com/467754239/1581237,如需转载请自行联系原作者