作者:吴业亮
博客:http://blog.csdn.net/wylfengyujiancheng
一、简介
1、Haproxy是什么
HAProxy提供高可用性、负载均衡以及基于TCP和HTTP应用的代理,支持虚拟主机,它是免费、快速并且可靠的一种解决方案。HAProxy特别适用于那些负载特大的web站点,这些站点通常又需要会话保持或七层处理。HAProxy运行在当前的硬件上,完全可以支持数以万计的并发连接。并且它的运行模式使得它可以很简单安全的整合进您当前的架构中, 同时可以保护你的web服务器不被暴露到网络上。
HAProxy实现了一种事件驱动, 单一进程模型,此模型支持非常大的并发连接数。多进程或多线程模型受内存限制 、系统调度器限制以及无处不在的锁限制,很少能处理数千并发连接。事件驱动模型因为在有更好的资源和时间管理的用户端(User-Space) 实现所有这些任务,所以没有这些问题。此模型的弊端是,在多核系统上,这些程序通常扩展性较差。这就是为什么他们必须进行优化以 使每个CPU时间片(Cycle)做更多的工作。
2、官网
http://www.haproxy.com/
3、三种负载工具比较
二、haproxy配置
1、global
global
log 127.0.0.1 local2 #打印日志方式
chroot /var/lib/haproxy
pidfile /var/run/haproxy.pid #定义Haproxy的IP
maxconn 4000 #每个进程的最大连接数
user haproxy #用户
group haproxy #组
daemon #以守护进程的方式
nbproc 16 # 启动的进程数,默认是1
# turn on stats unix socket
stats socket /var/lib/haproxy/stats
2、defaults
defaults
mode http #设置 mode的语法(http|tcp|health),http是七层模式,tcp是四层模式,health是健康检查,返回ok
log global #采取全军global的日志
option httplog # 启用日志记录http请求,默认haproxy不记录http的请求
option dontlognull
option http-server-close
option redispatch #当serverId对应的服务器挂掉后,强制定向到其他健康的服务器
option abortonclose #当服务器负载很高的时候,自动结束掉当前队列处理比较久的链接
option forwardfor except 127.0.0.0/8
option redispatch
retries 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 #默认最大连接数
3、frontend
frontend test.com #定义前端服务器(haproxy)
bind *:80 #监听地址
acl web-client path_beg -i /vsphere-client
acl bbs hdr_reg(host) -i ^(bbs.test.com|shequ.test.com|forum)
acl monitor hdr_beg(host) -i monitor.test.com #定义ACL名称,对应的请求的主机头是monitor.test.com
acl www hdr_beg(host) -i www.test.com
use_backend cache.test.com if static
use_backend monitor.test.com if bbs or monitor
use_backend www.test.com if www
use_backend vsphere-client if web-client
default_backend www.test.com #指定默认的后端服务器
4、backend
backend monitor.test.com #定义后端服务器群(web server/apache/nginx/iis..)
mode http
option forwardfor #后端服务器(apache/nginx/iis/*),从Http Header中获得客户端IP
balance leastconn #参见 balance 算法:
cookie SERVERID #插入serverid到cookie中,serverid后面可以定义
option httpchk HEAD /check.html #用来做健康检查html文档 基于七层的健康检查,默认为4层
#option httpchk HEAD /index.php HTTP/1.1\r\nHost:monitor.test.com #HTTP && Host
server server1 10.0.100.70:80 cookie server1 check inter 2000 rise 3 fall 3 weight 3
#服务器定义:
#cookie server1表示serverid为server1;
#check inter 2000 是检测心跳频率(check 默认 );
#rise 3 表示 3次正确认为服务器可用;
#fall 3 表示 3次失败认为服务器不可用;
#weight 表示权重。Name server 列表
5、listen
listen admin_stat #status
bind 0.0.0.0:8080 #监听端口
mode http #http的7层模式
stats refresh 30s #统计页面自动刷新时间
stats uri /haproxy_stats_url #统计页面URL
stats realm Haproxy\ Statistics #统计页面密码框上提示文本
stats auth admin:admin #统计页面用户名和密码设置
stats hide-version #隐藏统计页面上HAProxy的版本信息
stats admin if TRUE #手工启用/禁用,后端服务器
balance 算法:
1. roundrobin,表示简单的轮询
2. static-rr,表示根据权重,
3. leastconn,表示最少连接者先处理,
4. source,表示根据请求源IP,
5. uri,表示根据请求的URI;
6. url_param,表示根据请求的URl参数'balance url_param' requires an URL parameter name
7. hdr(name),表示根据HTTP请求头来锁定每一次HTTP请求;
8. rdp-cookie(name),表示根据据cookie(name)来锁定并哈希每一次TCP请求。
6、acl 规则
acl bbs hdr_reg(host) -i ^(bbs.test.com|forum.test.com) #使用正则匹配
acl bbs_path path_beg -i /bbs #url 目录
acl youxi path_beg -i /youxi
acl static path_end -i .html .css .js #url 结尾文件
acl php path_end -i .php
acl jsp path_end -i .jsp .do
use_backend bbs_pool if bbs or bbs_path #注意 "or"
use_backend youxi_pool if youxi
use_backend static_pool if static
use_backend php_pool if php
use_backend jsp_pool if jsp
default_backend www.test.com
举例:
openstack官方负载均衡样例
global
chroot /var/lib/haproxy
daemon
group haproxy
maxconn 4000
pidfile /var/run/haproxy.pid
user haproxy
defaults
log global
maxconn 4000
option redispatch
retries 3
timeout http-request 10s
timeout queue 1m
timeout connect 10s
timeout client 1m
timeout server 1m
timeout check 10s
listen dashboard_cluster
bind :443
balance source
option tcpka
option httpchk
option tcplog
server controller1 10.0.0.12:443 check inter 2000 rise 2 fall 5
server controller2 10.0.0.13:443 check inter 2000 rise 2 fall 5
server controller3 10.0.0.14:443 check inter 2000 rise 2 fall 5
listen galera_cluster
bind :3306
balance source
option mysql-check
server controller1 10.0.0.12:3306 check port 9200 inter 2000 rise 2 fall 5
server controller2 10.0.0.13:3306 backup check port 9200 inter 2000 rise 2 fall 5
server controller3 10.0.0.14:3306 backup check port 9200 inter 2000 rise 2 fall 5
listen glance_api_cluster
bind :9292
balance source
option tcpka
option httpchk
option tcplog
server controller1 10.0.0.12:9292 check inter 2000 rise 2 fall 5
server controller2 10.0.0.13:9292 check inter 2000 rise 2 fall 5
server controller3 10.0.0.14:9292 check inter 2000 rise 2 fall 5
listen glance_registry_cluster
bind :9191
balance source
option tcpka
option tcplog
server controller1 10.0.0.12:9191 check inter 2000 rise 2 fall 5
server controller2 10.0.0.13:9191 check inter 2000 rise 2 fall 5
server controller3 10.0.0.14:9191 check inter 2000 rise 2 fall 5
listen keystone_admin_cluster
bind :35357
balance source
option tcpka
option httpchk
option tcplog
server controller1 10.0.0.12:35357 check inter 2000 rise 2 fall 5
server controller2 10.0.0.13:35357 check inter 2000 rise 2 fall 5
server controller3 10.0.0.14:35357 check inter 2000 rise 2 fall 5
listen keystone_public_internal_cluster
bind :5000
balance source
option tcpka
option httpchk
option tcplog
server controller1 10.0.0.12:5000 check inter 2000 rise 2 fall 5
server controller2 10.0.0.13:5000 check inter 2000 rise 2 fall 5
server controller3 10.0.0.14:5000 check inter 2000 rise 2 fall 5
listen nova_ec2_api_cluster
bind :8773
balance source
option tcpka
option tcplog
server controller1 10.0.0.12:8773 check inter 2000 rise 2 fall 5
server controller2 10.0.0.13:8773 check inter 2000 rise 2 fall 5
server controller3 10.0.0.14:8773 check inter 2000 rise 2 fall 5
listen nova_compute_api_cluster
bind :8774
balance source
option tcpka
option httpchk
option tcplog
server controller1 10.0.0.12:8774 check inter 2000 rise 2 fall 5
server controller2 10.0.0.13:8774 check inter 2000 rise 2 fall 5
server controller3 10.0.0.14:8774 check inter 2000 rise 2 fall 5
listen nova_metadata_api_cluster
bind :8775
balance source
option tcpka
option tcplog
server controller1 10.0.0.12:8775 check inter 2000 rise 2 fall 5
server controller2 10.0.0.13:8775 check inter 2000 rise 2 fall 5
server controller3 10.0.0.14:8775 check inter 2000 rise 2 fall 5
listen cinder_api_cluster
bind :8776
balance source
option tcpka
option httpchk
option tcplog
server controller1 10.0.0.12:8776 check inter 2000 rise 2 fall 5
server controller2 10.0.0.13:8776 check inter 2000 rise 2 fall 5
server controller3 10.0.0.14:8776 check inter 2000 rise 2 fall 5
listen ceilometer_api_cluster
bind :8777
balance source
option tcpka
option tcplog
server controller1 10.0.0.12:8777 check inter 2000 rise 2 fall 5
server controller2 10.0.0.13:8777 check inter 2000 rise 2 fall 5
server controller3 10.0.0.14:8777 check inter 2000 rise 2 fall 5
listen nova_vncproxy_cluster
bind :6080
balance source
option tcpka
option tcplog
server controller1 10.0.0.12:6080 check inter 2000 rise 2 fall 5
server controller2 10.0.0.13:6080 check inter 2000 rise 2 fall 5
server controller3 10.0.0.14:6080 check inter 2000 rise 2 fall 5
listen neutron_api_cluster
bind :9696
balance source
option tcpka
option httpchk
option tcplog
server controller1 10.0.0.12:9696 check inter 2000 rise 2 fall 5
server controller2 10.0.0.13:9696 check inter 2000 rise 2 fall 5
server controller3 10.0.0.14:9696 check inter 2000 rise 2 fall 5
listen swift_proxy_cluster
bind :8080
balance source
option tcplog
option tcpka
server controller1 10.0.0.12:8080 check inter 2000 rise 2 fall 5
server controller2 10.0.0.13:8080 check inter 2000 rise 2 fall 5
server controller3 10.0.0.14:8080 check inter 2000 rise 2 fall 5
参考:
http://www.haproxy.com/