HAProxy

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.orgphp

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; donehtml

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的一致性hashnode

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 checknginx

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

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后端

相关文章
相关标签/搜索