1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
[root@node1 ~]# sysctl -a | grep ip_forward
[root@node1 ~]# vim /etc/sysctl.conf 将
net.ipv4.ip_forward =
1
[root@node1 ~]# sysctl -p
net.ipv4.ip_forward =
1
net.ipv4.conf.
default
.rp_filter =
1
net.ipv4.conf.
default
.accept_source_route =
0
kernel.sysrq =
0
kernel.core_uses_pid =
1
net.ipv4.tcp_syncookies =
1
net.bridge.bridge-nf-call-ip6tables =
0
net.bridge.bridge-nf-call-iptables =
0
net.bridge.bridge-nf-call-arptables =
0
kernel.msgmnb =
65536
kernel.msgmax =
65536
kernel.shmmax =
68719476736
kernel.shmall =
4294967296
|
1
2
3
4
5
6
7
|
[root@node1 ~]# rpm -ql haproxy
/etc/haproxy
/etc/haproxy/haproxy.cfg #配置文件
/etc/logrotate.d/haproxy #日志轮转
/etc/rc.d/init.d/haproxy #运行脚本
/usr/bin/halog #日志分析工具
/usr/sbin/haproxy
|
1
2
3
4
5
6
7
8
9
|
global#全局配置,定义haproxy进程的工作特性以及全局配置
log
127.0
.
0.1
local2
chroot /
var
/lib/haproxy #chroot运行路径,增加安全性
pidfile /
var
/run/haproxy.pid#haproxy的pid存放路径
maxconn
4000
#默认的最大连接数
user haproxy #运行haproxy的用户
group haproxy#运行haproxy用户所属的组
daemon#以守护进程的方式工作于后台
stats socket /
var
/lib/haproxy/stats
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
mode http #默认使用协议,可以为{http|tcp|health}http:是七层协议,tcp是四层, health:只返回ok
log global
option httplog #详细记录http日志
option dontlognull#不记录健康检查的日志信息
option http-server-close
option forwardfor except
127.0
.
0.0
/
8
option redispatch#ServerID对应的服务器宕机后,强制定向到其他运行正常的服务器
retries
3
#
3
次连接失败则认为服务不可用
timeout http-request 10s #默认http请求超时时间
timeout queue 1m#默认队列超时时间
timeout connect 10s#默认连接超时时间
timeout client 1m#默认客户端超时时间
timeout server 1m#默认服务器端超时时间
timeout http-keep-alive 10s#默认持久连接超时时间
timeout check 10s#心跳检测超时
maxconn
3000
#默认最大的连接数
|
1
2
3
4
5
6
7
8
9
10
11
12
|
#---------------------------------------------------------------------
# main frontend which proxys to the backends
#---------------------------------------------------------------------
frontend webserver
bind *:
80
default_backend appservs
#---------------------------------------------------------------------
#
static
backend
for
serving up images, stylesheets and such
#---------------------------------------------------------------------
backend appservs
server node2.magedu.com
192.168
.
10.11
:
80
check #该机器的80端口状态
server node3.magedu.com
192.168
.
10.12
:
80
check
|
1
2
3
4
5
6
7
|
#---------------------------------------------------------------------
# main frontend which proxys to the backends
#---------------------------------------------------------------------
listen webservers
bind *:
80
server node2.magedu.com
192.168
.
10.11
:
80
check
server node3.magedu.com
192.168
.
10.12
:
80
check
|
1
2
3
4
5
6
7
8
9
10
11
|
#---------------------------------------------------------------------
# main frontend which proxys to the backends
#---------------------------------------------------------------------
listen webservers
bind *:
80
server node2.magedu.com
192.168
.
10.11
:
80
check
frontend imgservers
bind *:
8080
default_backend imgservs
backend imgservs
server node3.magedu.com
192.168
.
10.12
:
80
check
|
1
2
3
4
5
6
7
8
9
10
11
12
|
#---------------------------------------------------------------------
# main frontend which proxys to the backends
#---------------------------------------------------------------------
listen webservers
bind *:
80
server node2.magedu.com
192.168
.
10.11
:
80
check
stats enable
frontend imgservers
bind *:
8080
default_backend imgservs
backend imgservs
server node3.magedu.com
192.168
.
10.12
:
80
check
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
#---------------------------------------------------------------------
# main frontend which proxys to the backends
#---------------------------------------------------------------------
listen webservers
bind *:
80
server node2.magedu.com
192.168
.
10.11
:
80
check
stats enable#显示状态页面
stats hide-version #隐藏haproxy的版本号
stats realm HAProxy\ Stats #提示信息
stats auth admin:admin #登录状态页面的帐号和密码
frontend imgservers
bind *:
8080
default_backend imgservs
backend imgservs
server node3.magedu.com
192.168
.
10.12
:
80
check
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
#---------------------------------------------------------------------
# main frontend which proxys to the backends
#---------------------------------------------------------------------
listen webservers
bind *:
80
server node2.magedu.com
192.168
.
10.11
:
80
check
listen stats
bind *:
1088
#伪装的端口号
stats enable
stats hide-version
stats realm HAProxy\ Stats
stats auth admin:admin
stats admin
if
TRUE #状态页面出现管理功能
stats uri /admin?admin #访问入口
frontend imgservers
bind *:
8080
default_backend imgservs
backend imgservs
server node3.magedu.com
192.168
.
10.12
:
80
check
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
#---------------------------------------------------------------------
# main frontend which proxys to the backends
#---------------------------------------------------------------------
frontend webservers
bind *:
80
default_backend webservs
listen stats
bind *:
1088
stats enable
stats hide-version
stats realm HAProxy\ Stats
stats auth admin:admin
stats admin
if
TRUE
stats uri /admin?admin
backend webservs
server node3.magedu.com
192.168
.
10.12
:
80
check weight
3
server node2.magedu.com
192.168
.
10.11
:
80
check weight
1
|
HAProxy 負載均衡算法介紹
HAProxy的算法有如下8种:
一、roundrobin,表示简单的轮询,这个不多说,这个是负载均衡基本都具备的;
二、static-rr,表示根据权重,建议关注;
三、leastconn,表示最少连接者先处理,建议关注;
四、source,表示根据请求源IP,建议关注;
五、uri,表示根据请求的URI;
六、url_param,表示根据请求的URl参数'balance url_param' requires an URL parameter name
七、hdr(name),表示根据HTTP请求头来锁定每一次HTTP请求;
八、rdp-cookie(name),表示根据据cookie(name)来锁定并哈希每一次TCP请求
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
#---------------------------------------------------------------------
# main frontend which proxys to the backends
#---------------------------------------------------------------------
frontend webservers
bind *:
80
default_backend webservs
listen stats
bind *:
1088
stats enable
stats hide-version
stats realm HAProxy\ Stats
stats auth admin:admin
stats admin
if
TRUE
stats uri /admin?admin
backend webservs
balance source
hash-type consistent
server node3.magedu.com
192.168
.
10.12
:
80
check weight
3
server node2.magedu.com
192.168
.
10.11
:
80
check weight
1
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
frontend webservers
bind *:
80
default_backend webservs
listen stats
bind *:
1088
stats enable
stats hide-version
stats realm HAProxy\ Stats
stats auth admin:admin
stats admin
if
TRUE
stats uri /admin?admin
backend webservs
balance uri
hash-type consistent
server node3.magedu.com
192.168
.
10.12
:
80
check weight
3
server node2.magedu.com
192.168
.
10.11
:
80
check weight
1
|
1
2
3
4
|
backend webservs
balance roundrobin
server node3.magedu.com
192.168
.
10.12
:
80
check weight
3
redir http:
//172.16.0.1
server node2.magedu.com
192.168
.
10.11
:
80
check weight
1
|
1
2
3
4
|
backend webservs
balance roundrobin
server node3.magedu.com
192.168
.
10.12
:
80
check weight
3
backup
server node2.magedu.com
192.168
.
10.11
:
80
check weight
1
|
1
2
3
4
5
|
backend webservs
balance roundrobin
server node3.magedu.com
192.168
.
10.12
:
80
check weight
3
server node2.magedu.com
192.168
.
10.11
:
80
check weight
1
server backup.magedu.com
127.0
.
0.1
:
8008
check weight
1
backup
|
1
2
3
4
5
|
backend webservs
balance roundrobin
server node3.magedu.com
192.168
.
10.12
:
80
check weight
3
maxconn
3000
server node2.magedu.com
192.168
.
10.11
:
80
check weight
1
maxconn
2000
server backup.magedu.com
127.0
.
0
:
8008
check weight
1
backup
|
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
|
用法一:
将源IP为
172.16
.
253.254
的用户禁止、将
403
的错误重定向到其他服务器;
frontend webservers
bind *:
80
default_backend webservs
acl badguy src
172.16
.
253.254
block
if
badguy
errorloc
403
http:
//www.baidu.com
用法二:
当用户访问地址为
172.16
.
2.1
时,将访问页面重定向http:
//www.baidu.com
frontend webservers
bind *:
80
default_backend webservs
acl dstipaddr hdr(Host)
172.16
.
2.1
redirect location http:
//www.baidu.com if dstipaddr
用法三:
acl中path的使用
frontend webservers
bind *:
80
default_backend webservs
acl badguy src
172.16
.
253.254
acl denyfile path /
1
.html
http-request deny
if
badguy denyfile
用法四:
读写分离:
acl read method GET
acl read method HEAD
acl write method PUT
acl write method POST
use_backend imgservers
if
read
use_backend uploadservers
if
write
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
|
#---------------------------------------------------------------------
# main frontend which proxys to the backends
#---------------------------------------------------------------------
frontend webservers
bind *:
80
acl
static
path_end .html
use_backend staticservs
if
static
default_backend appservs
listen stats
bind *:
1088
stats enable
stats hide-version
stats realm HAProxy\ Stats
stats auth admin:admin
stats admin
if
TRUE
stats uri /admin?admin
backend staticservs
balance roundrobin
server node2.magedu.com
192.168
.
10.11
:
80
check weight
1
maxconn
3000
server backup.magedu.com
127.0
.
0
:
8008
check weight
1
backup
backend appservs
balance roundrobin
server node3.magedu.com
192.168
.
10.12
:
80
check maxconn
2000
|