haproxy 负载均衡

haproxy 安装:

一.haproxy使用rpmbuild安装

rpmbuild 用来指示转换的源码编译成二进制文件的包,创建rpm包!

基本格式:rpmbuild  参数  spec文档   tarball包| 源码包

参数:-bb  ##至建立二进制包

         -ba    ##建立源码与二进制包

1.解压压缩包,进入解压目录,当前目录下/examples/haproxy.spec文件;(安装步骤详细阅读解压目录下的README文件)


2.yum安装rpm-build软件,rpmbuild  -bb  haproxy.spec ;


3.报错/root/rpmbuild/SOURCES目录没有haproxy的源码包,源码包移动该目录,再次执行该命令不进行报错;



4.在rpmbuild/RPMS/x86_64/目录下有haproxy.rpm包;


5.rpm 命令直接安装;


6.rpm  -qpl   安装包名 ,查看生成的文件;


7.在examples目录复制content-sw-sample.cfg配置文件到/etc/haptroxy/haproxy.cfg


8.创建haproxy用户uid=gid=200


二 .  源码编译安装:

1.解压压缩包,进入解压目录,Makefile文件是编译好的安装环境


2.make (make clean 清除make编译的环境)  &&  make install


    

三.haproxy配置文件;

1.全局配置<global>

daemon     ###默认以后台模式运行haproxy

nbproc 1    ###设置进程数量

maxconn   4096     ###最大连接数,需要考虑ulimit -n限制

pidfile   /var/run/haproxy.pid     ###haproxy进程pid文件

ulimit   -n   数字         ####ulimit的数量限制

chroot   /usr/share/haproxy       ###chroot运行路径

debug     ##haproxy调试级别,只在开启单进程的时候调试

log    127.0.0.1   local0       ###日志的级别

2,默认配置<default>(有些文件没有可以写在全局配置后面)

log global

mode http #默认的模式mode { tcp|http|health }tcp4层,http7层,health只会返回OK

option httplog #日志类别,采用httplog

option dontlognull #不记录健康检查日志信息

retries 2 #两次连接失败就认为是服务器不可用,也可以通过后面设置

option forwardfor #如果后端服务器需要获得客户端真实ip需要配置的参数,可以从Http Header中获得客户端ip

option httpclose #每次请求完毕后主动关闭http通道,haproxy不支持keep-alive,只能模拟这种模式的实现

option redispatch #serverId对应的服务器挂掉后,强制定向到其他健康的服务器,以后将不支持

option abortonclose #当服务器负载很高的时候,自动结束掉当前队列处理比较久的链接

maxconn 4096 #默认的最大连接数

timeout connect 5000ms #连接超时

timeout client 30000ms #客户端超时

timeout server 30000ms #服务器超时

timeout check 2000 #心跳检测超时

timeout http-keep-alive10s #默认持久连接超时时间

timeout http-request 10s #默认http请求超时时间

timeout queue 1m #默认队列超时时间

balance roundrobin #设置默认负载均衡方式,轮询方式

balance source #设置默认负载均衡方式,类似于nginxip_hash

balnace leastconn #设置默认负载均衡方式,最小连接数

3.frontend  public <haproxy 的前端设置>

bin     *:80    ####监听所有服务的80端口

mode   http     ####http模式

#####acl定义策略######

acl    规则名称   算法   -i <表示忽略大小写>   请求规则

acl   blacklist(黑名单)  src(源地址)  192.168.88.150  ###定义acl的列表blacklist,如果源地址是192.168.88.150,进行下面做法

#####acl策略对应做法#####

http-request   deny     if   blacklist     ###如果出现在blacklist列表里对于http的请求直接拒绝

errorloc  403  http://192.168.88.152:8080    #对于blacklist的ip的http请求转接到192.168.88.152的403报错自定义页面

redirect   location    http://192.168.88.152:8080   if  blacklist    

####直接将blacklist列表的ip请求转发到192.168.88.152:8080页面

default_backend dynamic      #不满足则响应backend的默认页面

4.backend  static   <haproxy后端配置>

mode  http   

balance      roundrobin       #负载均衡的算法模式

option httpchk /index.html HTTP/1.0 #健康检查, 检测文件,如果分发到后台index.html访问不到就不再分发给它

server  westos1   192.168.88.152:80  check inter 2000 

server  westos2   192.168.88.153:80  check inter 2000 ###定义后端服务器(check  inter 2000 检测心跳频率)

5.haproxy的调度算法8种:

    roundrobin:  简单的轮询,每台服务器轮询的次数一样;算法是动态,对于启动较慢服务器会调整权重

    static-rr :  根据权重轮流使用

   leastconn : 最少连接着最先处理,适用于长会话服务

    source :  根据请求的源ip进行hash,用可用服务器的权重总数除以hash值,根据结果分配;

    uri : 根据请求的uri左端进行hash,用可用服务器的权重总数除以hash值,根据结果分配。一般用于代理缓存和反病毒代理,以最大限度的提高缓存命中率,只能用于http后端;

     uri_param:根据的请求uri参数进行锁定,一般用于将同一个用户信息发送到同一个后端服务器。

     hdr(name)  : 根据http请求报文的头部信息进行锁定后端服务器,如果没有头部信息或者头部信息没有价值进行roundrobin算法。

    rdp-cookie(name):  根据cookie来hash每次http请求;使用退化的持久模式,同一个用户或者同一ip发送同一个服务器,当然如果浏览器清楚cookie缓存则再次请求时会从新hash请求服务器。 

四.haproxy示例

1.haproxy 负载均衡器

实验环境:redhat6.5  防火墙关闭

后端服务器: server3 172.25.36.3   server5 172.25.36.5    实验要求开启httpd服务,编辑默认发布页面


负载均衡器  server4 : 172.25.36.4  实验要求安装haproxy服务

server4的haproxy配置文件/etc/haproxy/haproxy.cfg如下:

global
        maxconn         10000
        log             127.0.0.1 local0
        uid             200
        gid             200
        chroot          /var/empty
        daemon

# The public 'www' address in the DMZ
frontend public

        bind            *:80
        mode            http
        log             global
        option          httplog
        option          dontlognull
        monitor-uri     /monitoruri
        maxconn         8000
        timeout client  30s
#        use_backend     static if { hdr_beg(host) -i img }
 #       use_backend  dynamic if write
        default_backend static
# The static backend backend for 'Host: img', /img and /css.

# the application servers go here
backend dynamic
        mode            http
        balance         roundrobin
        retries         2
        option redispatch
        timeout connect 5s
        timeout server  30s
        timeout queue   30s
        server           westos1 172.25.36.3:80  check inter 1000
        server      westos2    172.25.36.5:80   check inter  1000

客户端浏览器测试: 输入负载均衡器的ip  (需要进行本地解析 /etc/hosts)


2. 设置预备后端服务器:haproxy配置文件如下:

global
        maxconn         10000
        log             127.0.0.1 local0
        uid             200
        gid             200
        chroot          /var/empty
        daemon

# The public 'www' address in the DMZ
frontend public

        bind            *:80
        mode            http
        log             global
        option          httplog
        option          dontlognull
        monitor-uri     /monitoruri
        maxconn         8000
        timeout client  30s
#        use_backend     static if { hdr_beg(host) -i img }
 #       use_backend  dynamic if write
        default_backend static
# The static backend backend for 'Host: img', /img and /css.

# the application servers go here
backend dynamic
        mode            http
        balance         roundrobin
        retries         2
        option redispatch
        timeout connect 5s
        timeout server  30s
        timeout queue   30s
        server           westos1 172.25.36.3:80  check inter 1000

        server      westos2    172.25.36.5:80   check inter  1000

        server           westos3 172.25.36.2:80  check inter 1000

   客户端浏览器测试: 

   当后端server3服务器出现故障,页面不在轮询


当后端两台服务器出现故障,热备服务器接替工作(只要后端服务器还有一台在运行,热备服务器处于备用状态)


3. 配置文件默认haproxy的监控页面admin/stats,测试浏览器输入ip/admin/stats


4.基于用户访问监控页面:

配置文件添加参数  stats  auth   admin:westos   (设置访问监控页面的用户为admin和密码为westos),重新加载服务

global
        maxconn         10000
        log             127.0.0.1 local0
        uid             200
        gid             200
        chroot          /var/empty
        daemon

# The public 'www' address in the DMZ
frontend public

        bind            *:80
        mode            http
        log             global
        option          httplog
        option          dontlognull
        monitor-uri     /monitoruri
        maxconn         8000

        timeout client  30s

        stats   auth  admin:westos

       stats   uri  /admin/stats

#        use_backend     static if { hdr_beg(host) -i img }
 #       use_backend  dynamic if write
        default_backend static
# The static backend backend for 'Host: img', /img and /css.

# the application servers go here
backend dynamic
        mode            http
        balance         roundrobin
        retries         2
        option redispatch
        timeout connect 5s
        timeout server  30s
        timeout queue   30s
        server           westos1 172.25.36.3:80  check inter 1000

        server      westos2    172.25.36.5:80   check inter  1000

        server           westos3 172.25.36.2:80  check inter 1000

测试:


5.定义acl列表

global
        maxconn         10000
        log             127.0.0.1 local0
        uid             200
        gid             200
        chroot          /var/empty
        daemon

# The public 'www' address in the DMZ
frontend public
        acl blacklist src 172.25.36.2        ###定义黑名单,源地址为172.25.36.2
        http-request  deny  if blacklist   ###黑名单中源地址http请求拒绝
        bind            *:80
        mode            http
        log             global
        option          httplog
        option          dontlognull
        monitor-uri     /monitoruri
        maxconn         8000
        timeout client  30s
#        use_backend     static if { hdr_beg(host) -i img }
#        use_backend  dynamic if write
        default_backend dynamic


# The static backend backend for 'Host: img', /img and /css.

# the application servers go here
backend dynamic
        mode            http
        balance         roundrobin
        retries         2
        option redispatch
        timeout connect 5s
        timeout server  30s
        timeout queue   30s
        server           westos1 172.25.36.3:80  check inter 1000

        server      westos2    172.25.36.5:80   check inter  1000

测试:


6.haproxy实现动静分离

配置文件如下:(后端设置 : backend static  和 backend  dunamic 指定相应服务器)

global
        maxconn         10000
        log             127.0.0.1 local0
        uid             200
        gid             200
        chroot          /var/empty
        daemon

# The public 'www' address in the DMZ
frontend public
        acl blacklist src 172.25.36.2
        http-request  deny  if blacklist
        bind            *:80
        mode            http
        log             global
        option          httplog
        option          dontlognull
        monitor-uri     /monitoruri
        maxconn         8000
        timeout client  30s
#        use_backend     static if { hdr_beg(host) -i img }
        use_backend  dynamic if write
        default_backend static
# The static backend backend for 'Host: img', /img and /css.

# the application servers go here
backend dynamic
        mode            http
        balance         roundrobin
        retries         2
        option redispatch
        timeout connect 5s
        timeout server  30s
        timeout queue   30s
        server           westos1 172.25.36.3:80  check inter 1000
backend  static
       balance     roundrobin
        server      westos2    172.25.36.5:80   check inter  1000

测试:

后端服务器编写php文件,如果浏览器加载不出来页面,重新加载http服务


7.haproxy服务器实现读写分离(读取文件和上传文件在指定后端服务器运行)

global
        maxconn         10000
        log             127.0.0.1 local0
        uid             200
        gid             200
        chroot          /var/empty
        daemon

# The public 'www' address in the DMZ
frontend public    
        bind            *:80
        acl write method  POST
        acl write  method PUT

        mode            http
        log             global
        option          httplog
        option          dontlognull
        monitor-uri     /monitoruri
        maxconn         8000
        timeout client  30s
#        use_backend     static if { hdr_beg(host) -i img }
        use_backend     dynamic  if write
        default_backend static
        

# The static backend backend for 'Host: img', /img and /css.

# the application servers go here
backend dynamic
        mode            http
        balance         roundrobin
        retries         2
        option redispatch
        timeout connect 5s
        timeout server  30s
        timeout queue   30s
        server           westos1 172.25.36.3:80  check inter 1000
backend  static

        mode       http

         retries         2
        option redispatch
        timeout connect 5s
        timeout server  30s
        timeout queue   30s

        balance     roundrobin
        server      westos2    172.25.36.5:80   check inter  1000

需要自己编写php上脚本,实验时老师已经完成,我们用于测试:



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值