haproxy配置详解

来源:互联网 发布:网络机顶盒如何更新 编辑:程序博客网 时间:2024/05/22 13:47

一:haproxy简介:

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

搭建环境:

node1.magedu.com

eth0:172.16.2.1

eth1:192.168.10.10

node2.magedu.com eth0:192.168.10.11

node3.magedu.com eth0:192.168.10.12

注意:node2和node3的默认网关指向192.168.10.10命令为route add default gw 192.168.10.10

其次:在node1节点上要打开网卡之间的转发功能

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


安装haproxy:

首先在node1上安装haproxy,查看生成的文件列表

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


其次:分析haproxy的配置文件/etc/haproxy/haproxy.cfg

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


defaults

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#默认最大的连接数


二:haproxy负载均衡实例:

修改配置文件/etc/haproxy/haproxy.cfg

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
    server      node3.magedu.com 192.168.10.12:80 check

然后重启haproxy即:service haproxy restart

也可以直接定义一个listen

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

listen和frontend,backend可以同时使用,基于不同端口的虚拟主机

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


启用状态:stats enable

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

进行访问172.16.2.1/haproxy?stats

202842725.png

haproxy的状态页面一般不允许别人随意查看,因此通过认证进行访问是必要的

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

203007175.png

203051352.png

基于页面的管理功能:

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

203218313.png

203253758.png

支持权重:

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

调度方法:

roundrobin 动态

static-rr 静态

source取决于hash-type

hash-type:map-based 静态调度

hash-type:consistent 动态调度

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

使用balance uri可以使访问固定到一个后端的服务器上

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


转发功能redir

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

203532530.png

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

203709744.png

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

只有当node2和node3都停掉时才使用backup

203824292.png

203845980.png

性能相关参数中maxconn的使用

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


三:haproxy中ACL的使用

定义ACL的语法格式为:

acl <aclname> <criterion> [flags] [operator] <value> ...

<aclname>:ACL名称,区分字符大小写,且其只能包含大小写字母、数字、-(连接线)、_(下划线)、.(点号)和:(冒号);haproxy中,acl可以重名,这可以把多个测试条件定义为一个共同的acl;

<criterion>:测试标准,即对什么信息发起测试;测试方式可以由[flags]指定的标志进行调整;而有些测试标准也可以需要为其在<value>之前指定一个操作符[operator];

[flags]:目前haproxy的acl支持的标志位有3个:

-i:不区分<value>中模式字符的大小写;

-f:从指定的文件中加载模式;

--:标志符的强制结束标记,在模式中的字符串像标记符时使用;

<value>:acl测试条件支持的值有以下四类:

整数或整数范围:如1024:65535表示从1024至65535;仅支持使用正整数(如果出现类似小数的标识,其为通常为版本测试),且支持使用的操作符有5个,分别为eq、ge、gt、le和lt;

字符串:支持使用“-i”以忽略字符大小写,支持使用“\”进行转义;如果在模式首部出现了-i,可以在其之前使用“--”标志位;

正则表达式:其机制类同字符串匹配;

IP地址及网络地址

同一个acl中可以指定多个测试条件,这些测试条件需要由逻辑操作符指定其关系。条件间的组合测试关系有三种:“与”(默认即为与操作)、“或”(使用“||”操作符)以及“非”(使用“!”操作符)。


用例:

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

204130644.png


204148842.png

0 0
原创粉丝点击