您的位置:首页 > 其它

HAProxy

2017-12-13 19:30 113 查看
haproxy是一款专业的7层的反向代理负载均衡器,可以实现4层和7层的负载均衡,四层:lvs(DR),nginx(stream),haproxy(mode tcp)
七层:nginx(http-upstream),httpd,haproxy(mode http),Pound,...

HAProxy 官方网站:
www.haproxy.com/www.haproxy.org

HAProxy 与nginx作为反代服务器的区别:同样工作在用户空间,nginx是一款轻量级,能实现缓存、webserver、邮件、负载均衡等功能,但nginx的许多功能都需要第三方的模块,而haproxy的转发能力比nginx有更强更灵活的定制性,可以运用splice实现0复制的转发,并且有更直观的图形化管理界面,不过通用性不如nginx,并无缓存功能。

安装和配置:
.可直接通过base  yum源安装,也可编译安装。
yum -y install haproxy

程序环境:
主程序:/usr/sbin/haproxy
配置文件:/etc/haproxy/haproxy.cfg
Unit File:/usr/lib/systemd/system/haproxy.service

主配置文件的结构:
两个配置段:
global:全局配置段;
进程及安全配置相关的参数;
性能调整相关的参数;
Debug参数;

注意:全局配置段中,除了maxconn之外,其他的配置一般无需修改;

proxies:代理配置段;
defaults:为下面的frontend、backend、listen配置段提供默认配置;
frontend:前端,面向客户端提供请求信息的接收和处理;相当于nginx的server{};
backend:后端,面向后端服务器发送请求和接收响应结果;相当于nginx的upstream{};
listen:用于四层反代和LB,相当于nginx的stream{};

调度算法
roundrobin  动态,加权轮询,所谓动态就是可以实时生效,不用重启服务,但是连接数受限,最多支持4128
static-rr   静态轮询,需重启服务
leastconn   动态,根据后端主机的负载数量进行调度
source   类似源地址hash,可以指定hash-type ,有map-based(取膜法,静态),    consistent(一致性哈希,动态)
uri  类似于DH算法,目标地址哈希,可以指定hash-type ,有map-based(取膜法,静态), consistent(一致性哈希,动态)
hdr(  ):根据请求报文中指定的header(User-agent,referer,hostname,cookie)进行调度,把指定的header的值做hash计算;可根据header首部来进行调度,非常强大,比如根据User-Agent浏览器类型来进行调度,可以指定hash-type ,有map-based(取膜法,静态), consistent(一致性哈希,动态)

基于会话绑定的cookie
cookie <name> [ rewrite | insert | prefix ] [ indirect ] [ nocache ] [ postonly ] [ preserve ] [ httponly ] [ secure ] [ domain <domain> ]* [ maxidle <idle> ] [ maxlife <life> ]

应用配置段:
defaults, listen, backend

rewrite:将指定名称的cookie的值重写,即修改cookie值;
insert:在原cookie值后面插入新的内容;
prefix:在原cookie值前面添加新的内容;
indirect:间接插入,只在insert模式中使用;
nocache:不做缓存,只在insert模式中使用;

注意:cookie参数的作用为:基于cookie的session sticky的实现方案;
小实验
准备三台host:

host1:172.16.66.71
host2: 172.16.66.72
host3:172.16.66.73
host1: yum install haproxy ,已经被收录进centos的base源了
分别现在host2和host3上面准备两台httpd服务,并存放数个html页面
yum install -y httpd mariadb-server php
host2: for i in {1..10};do echo "<h1>Page $i on node3</h1>" > /var/www/html/test$i.html; done
host3: for i in {1..10};do echo "<h1>Page $i on node4</h1>" > /var/www/html/test$i.html; done

eg1:利roundrobin实现最简单的调度,host1,可以同时绑定多个端口
frontend webs
bind :80,:8080
default_backend webserver
backend webserver
balance roundrobin
server web1 172.16.66.72:80 check
server web2 172.16.66.73:80 check

eg2:基于uri的一致性hash

frontend webs
bind :80
default_backend webserver
backend webserver
balance uri
hash-type consistent
server web1 172.16.66.72:80 check
server web2 172.16.66.73:80 check

测试:http://172.16.66.71/test1.html
测试:http://172.16.66.71/test2.html

eg3:基于hdr进行一致性hash调度,User-Agent
frontend webs
bind :80
default_backend webserver
backend webserver
balance hdr(User-Agent)
hash-type consistent
server web1 172.16.66.72:80 check
server web2 172.16.66.73:80 check

eg4: 设置cookie,基于会话绑定
frontend webs
bind :80
default_backend webserver
backend webserver
balance roundrobin
server web1 172.16.66.72:80 check weight 1 cookie web1
server web2 172.16.66.73:80 check weight 3 cookie web2
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  haproxy