一 haproxy 简介
HAProxy提供高可用性、负载均衡以及基于TCP和HTTP应用的代理,支持虚拟主机,它是免费、快速并且可靠的一种解决方案。HAProxy特别适用于那些负载特大的web站点,这些站点通常又需要会话保持或七层处理。HAProxy运行在当前的硬件上,完全可以支持数以万计的并发连接。并且它的运行模式使得它可以很简单安全的整合进您当前的架构中,同时可以保护你的web服务器不被暴露到网络上.
二 haproxy 的配置
haproxy 配置中分成五部分内容,分别如下:
1 global:参数是进程级的,通常是和操作系统相关。这些参数一般只设置一次,如果配置无误,就不需要再次进行修改
2 defaults:配置默认参数,这些参数可以被用到frontend,backend,Listen组件
3 frontend:接收请求的前端虚拟节点,Frontend可以更加规则直接指定具体使用后端的backend
4 backend:后端服务集群的配置,是真实服务器,一个Backend对应一个或者多个实体服务器
5 Listen Fronted和backend的组合体
三 haproxy 安装配置
1 安装
1
2
3
4
5
6
7
8
9
10
11
12
13
|
# wget http://haproxy.1wt.eu/download/1.4/src/haproxy-1.4.24.tar.gz
# tar xf haproxy-1.4.24.tar.gz
#uname -r
2.6
.
18
-
274.el5PAE
#make TARGET=linux26PREFIX=/usr/local/haproxy
#make install PREFIX=/usr/local/haproxy
#mkdir /usr/local/haproxy/{etc,logs,run}
#cd examples/
#cp haproxy.cfg /usr/local/haproxy/etc
#cp haproxy.init/etc/init.d/haproxy
#chmod 700 /etc/init.d/haproxy
#chkconfig --add haproxy
#chkconfig haproxy on
|
#cat haproxy.cfg
##全局配置信息###
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
global
log
127.0
.
0.1
local3
#[error warringinfo debug]#定义haproxy 日志级别
# log 127.0.0.1 local1 notice
#log loghost local0 info
maxconn
20480
#默认最大连接数
chroot
/
usr
/
local
/
haproxy
#chroot运行路径
uid
99
#运行haproxy 用户 UID
gid
99
#运行haproxy 用户组gid
daemon
#以后台形式运行harpoxy
nbproc
1
#设置进程数量
pidfile
/
usr
/
local
/
haproxy
/
run
/
haproxy.pid
#haproxy 进程PID文件
ulimit
-
n
819200
#ulimit 的数量限制
#debug #haproxy 调试级别,建议只在开启单进程的时候调试
#quiet
|
####默认配置选项#######
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
|
defaults
log
global
mode http
#所处理的类别(7层代理http,4层代理tcp)
maxconn
50000
#最大连接数
option httplog
#日志类别为http日志格式
option httpclose
#每次请求完毕后主动关闭http通道
option dontlognull
#不记录健康检查日志信息
option forwardfor
#如果后端服务器需要获得客户端的真实ip,需要配置的参数,可以从http header 中获取客户端的IP
retries
3
#3次连接失败就认为服务器不可用,也可以通过后面设置
option redispatch
#serverID 对应的服务器挂掉后,强制定向到其他健康的服务器
stats refresh
30
# 设置统计页面刷新时间间隔
option abortonclose
#当服务器负载很高的时候,自动结束掉当前队列处理比较久的连接
balance roundrobin
#设置默认负载均衡方式,轮询方式
#balance source # 设置默认负载均衡方式,类似于nginx的ip_hash
#balnace leastconn #设置默认负载均衡方式,最小连接数
contimeout
5000
#设置连接超时时间
clitimeout
50000
#设置客户端超时时间
srvtimeout
50000
#设置服务器超时时间
timeout check
2000
#设置心跳检查超时时间
#timeout http-request 10s #默认http请求超时时间
#timeoutqueue 1m #默认队列超时时间
#timeoutconnect 10s #默认连接超时时间
#timeoutclient 1m #默认客户端超时时间
#timeoutserver 1m #默认服务器超时时间
#timeout http-keep-alive10s #默认持久连接超时时间
|
#########设置监控页面######
1
2
3
4
5
6
7
8
9
10
11
|
listen admin_status
bind
0.0
.
0.0
:
81
#设置Frontend和Backend的组合体,监控组的名称,按需要自定义名称
mode http
#设置http的7 层模式层
log
127.0
.
0.1
local3 err
#错误日志记录
stats refresh
30s
#设置监控页面刷新时间:5s
stats uri
/
haproxy
-
stats
# 设置监控页面的url
stats realm Frank \Frank
#设置页面提示信息
stats auth admin:admin
#设置监控页面的用户和密码:admin,可以设置多个用户名
stats auth Frank:Frank
#设置监控页面的用户和密码:Frank
stats hide
-
version
#隐藏统计页面的HAproxy版本信息
stats admin
if
TRUE
#设置手工启动/禁用,后端服务器(haproxy-1.4.9以后版本)
|
########设置haproxy 错误页面#####
1
2
3
4
5
|
errorfile
403
/
usr
/
local
/
haproxy
/
errorfiles
/
403.http
errorfile
500
/
usr
/
local
/
haproxy
/
errorfiles
/
500.http
errorfile
502
/
usr
/
local
/
haproxy
/
errorfiles
/
502.http
errorfile
503
/
usr
/
local
/
haproxy
/
errorfiles
/
503.http
errorfile
504
/
usr
/
local
/
haproxy
/
errorfiles
/
504.http
|
##### 设置frontend#########
1
2
3
4
5
6
7
|
frontend http_80_in
bind
0.0
.
0.0
:
80
#设置监听端口,即haproxy提供的web服务端口,和lvs的vip 类似
mode http
# http 的7层模式
log
global
#应用全局的日志设置
option httplog
#启用http的log
option httpclose
#每次请求完毕后主动关闭http通道,HA-proxy不支持keep-alive模式
option forwardfor
#如果后端服务器需要获得客户端的真实IP需要配置此参数,将可以从HttpHeader中获得客户端IP
|
####acl 策略配置######
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
acl frank_web hdr_reg(host)
-
i ^(www.test.com.sh|news.test.com.sh)$
#如果请求的域名满足正则表达式中的2个域名返回true -i 是忽略大小写
# acl frank_fund hdr_dom(host) -i fund.test.com.sh
#如果请求的域名满足fund.test.com.sh返回true -i是忽略大小写
acl frank hdr(host)
-
i test.com.sh
#如果请求的域名满足test.com.sh返回true -i是忽略大小写
#acl file_req url_sub -i killall=
#在请求url中包含killall=,则此控制策略返回true,否则为false
# acl dir_req url_dir -i allow
#在请求url中存在allow作为部分地址路径,则此控制策略返回true,否则返回false
acl missing_cl hdr_cnt(Content
-
length)eq
0
#当请求的header中Content-length等于0时返回true
#### Manage interface ####
acl Frank_Manage path_dir
/
Frank
/
manage
/
acl Frank_Network src
192.168
.
151.189
192.168
.
152.0
/
24
## deny lb.html###
acl Frank_lb path
/
lb.html
|
########acl策略匹配相应#############
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
block
if
Frank_lb
block
if
Frank_Manage !Frank_Network
#block if missing_cl
#当请求中header中Content-length等于0阻止请求返回403
#block if !file_req || dir_req
#block表示阻止请求,返回403错误,当前表示如果不满足策略file_req,或者满足策略dir_req,则阻止请求
redirect prefix http:
/
/
192.168
.
151.249code
301
if
frank
#当访问test.com.sh的时候,用http的301挑转到http://192.168.151.249
use_backend server_web
if
frank_web
#当满足frank_web的策略时使用server_web的backend
#use_backend server_blog if frank_fund
#当满足frank_fund的策略时使用server_blog的backend
default_backend server_web
#以上都不满足的时候使用默认server_bbs的backend
|
##########backend的设置##############
####################backend server_web#########################
1
2
3
4
5
6
7
8
9
10
11
12
|
backendserver_web
mode http
#http的7层模式
balance roundrobin
#负载均衡的方式,roundrobin平均方式
cookie etnetchinaid insert indirectnocache domain .test.com.sh maxidle
20s
maxlife
30s
#允许插入serverid到cookie中,serverid后面可以定义
# cookie SERVERID insert indirect nocache
# appsession JSESSIONID len 64 timeout 300s request-learn
option httpchk GET
/
lb.html HTTP
/
1.0
#心跳检测的文件
server
192.168
.
51.78
192.168
.
151.78
:
80cookie
cookie78 check inter
1500
rise
3
fall
3
weight
1
#服务器定义,cookie 1表示serverid为web1,check inter1500是检测心跳频率rise 3是3次正确认为服务器可用,
#fall 3是3次失败认为服务器不可用,weight代表权重
server
192.168
.
151.79
192.168
.
151.79
:
80cookie
cookie79 check inter
1500
rise
3
fall
3
weight
1
#服务器定义,cookie 1表示serverid为web2,check inter1500是检测心跳频率rise 3是3次正确认为服务器可用, #fall 3是3次失败认为服务器不可用,weight代表权重
|
参考blog:http://itnihao.blog.51cto.com/1741976/915537
http://www.google.com.hk