haproxy负载均衡

安装haproxy
下载 haproxy-1.6.11.tar.gz 压缩包,制作rpm包

yum install rpm-build   
rpmbuild -tb haproxy-1.6.11.tar.gz 

查看这个rpm包都会生成什么文件

rpm -qpl haproxy-1.6.11-1.x86_64.rpm

安装rpm包

cd rpmbuild/RPMS/x86_64/
rpm -ivh haproxy-1.6.11-1.x86_64.rpm 

解压压缩包,因为安装的rpm包中不会生成配置文件,所以将里面的配置文件模版复制到/etc/haproxy/haproxy.cfg,haproxy的脚本文件会去/etc/haproxy/haproxy.cfg这个位置读它的配置文件

tar zxf haproxy-1.6.11.tar.gz 
cd haproxy-1.6.11/examples/
cp content-sw-sample.cfg /etc/haproxy/haproxy.cfg

用haproxy实现均衡负载
实验环境:

调度器:server6 安装haproxy 
realserver: 
server2:安装阿帕奇(httpd) 在 /var/www/html/index.html 文件中写入server2 
server3:安装阿帕奇(httpd) 在 /var/www/html/index.html 文件中写入server3

编辑配置文件

vim /etc/haproxy/haproxy.cfg
global
        maxconn         10000
        stats socket    /var/run/haproxy.stat mode 600 level admin
        log             127.0.0.1 local0    #日志输出配置,所有日志都记录在本机,通过local0输出
        uid             200
        gid             200
        chroot          /var/empty
        daemon      #以后台形式运行haproxy
defaults
        mode            http            #默认模式  tcp是4层 http是7层
        log             global
        option          httplog         #日志级别
        option          dontlognull     #不记录健康检查日志信息
        monitor-uri     /monitoruri
        maxconn         8000
        timeout client  30s

        stats uri       /admin/stats
        option prefer-last-server
        retries         2
        option redispatch
        timeout connect 5s
        timeout server  5s

# The public 'www' address in the DMZ
frontend public
        bind            *:80 name clear
        #bind            192.168.1.10:443 ssl crt /etc/haproxy/haproxy.pem
        use_backend     static if { hdr_beg(host) -i img }
        use_backend     static if { path_beg /img /css   }
        default_backend static  #默认访问后端为 static

# The static backend backend for 'Host: img', /img and /css.
backend static
        mode            http
        balance         roundrobin  #轮询算法
        server          statsrv1 172.25.31.2:80 check inter 1000
        server          statsrv2 172.25.31.3:80 check inter 1000

创建haproxy用户

groupadd -g 200 haproxy
useradd -u 200 -g 200 -M haproxy
id haproxy  #查看用户

对用户做访问限制

vim /etc/security/limits.conf
haproxy -       nofile  10000




/etc/init.d/haproxy start#打开服务

测试负载均衡是否可用
在浏览器中访问

172.25.31.6/monitoruri #网站健康检测URL,用来检测HAProxy管理的网站是否可以用,正常返回200 
 
172.25.31.6/admin/stats #对后端服务器的管理和页面监控数据监控 

检测负载均衡功能是否正常

Haproxy中的算法

名称	工作方式
1.balance roundrobin	轮询,软负载均衡基本都具备这种算法
2.balance static-rr	根据权重
3.balance leastconn	最少连接者先处理
4.balance source	根据请求源IP
5.balance uri	根据请求的URI
6.balance url_param,	根据请求的URl参数’balance url_param’ requires an URL parameter name
7.balance hdr(name)	根据HTTP请求头来锁定每一次HTTP请求
8.balance rdp-cookie(name)	根据据cookie(name)来锁定并哈希每一次TCP请求

开启日志功能

   vim /etc/rsyslog.conf

# Provides UDP syslog reception
$ModLoad imudp
$UDPServerRun 514


# Don't log private authentication messages!
*.info;mail.none;authpriv.none;cron.none;local0.none          /var/log/messages

# Save boot messages also to boot.log
local7.*                                                /var/log/boot.log
local0.*                                                /var/log/haproxy.log
 



/etc/init.d/rsyslog restart #重启日志服务
cat /var/log/haproxy.log    #查看日志

动静分离
在server3安装php ,编辑 /var/www/html/index.php

vim /var/www/html/index.php
<?php
phpinfo()
?>

在server6中编辑haproxy的配置文件

vim /etc/haproxy/haproxy.cfg

# The public 'www' address in the DMZ
frontend public
        bind            *:80 name clear
        #bind            192.168.1.10:443 ssl crt /etc/haproxy/haproxy.pem
        #use_backend     static if { hdr_beg(host) -i img }
        #use_backend     static if { path_beg /img /css   }
        use_backend     static2 if { path_end -i .php   }
        default_backend static1

# The static backend backend for 'Host: img', /img and /css.
backend static1
        balance         roundrobin
        server          statsrv1 172.25.31.2:80 check inter 1000

backend static2
        balance         roundrobin
        server          statsrv2 172.25.31.3:80 check inter 1000

在浏览器中访问172.25.31.6时访问到的是server2的页面

当访问172.25.31.6/index.php 时,访问的是server3 中的页面

黑名单

vim /etc/haproxy/haproxy.cfg
acl blacklist src 172.25.31.250
http-request deny if blacklist

不允许ip为172.25.31.250 的用户访问 (也可以写网段)

重定向
在server6中安装httpd
将端口改为8080

编辑 /var/www/html/index.html 文件 写入

网站正在维护中…

编辑haproxy 配置文件

vim /etc/haproxy/haproxy.cfg

acl blacklist src 172.25.31.250
errorloc 403 http://172.25.31.6:8080

当得到403 的错误时,重定向到172.25.31.6:8080 页面

直接重定向

vim /etc/haproxy/haproxy.cfg
acl blacklist src 172.25.31.250

redirect location http://172.25.31.6:8080

读写分离
编辑haproxy配置文件

vim /etc/haproxy/haproxy.cfg

frontend public
        bind            *:80 name clear

        acl write method POST
        acl write method PUT

    #use_backend     static2 if { path_end -i .php   }
    use_backend     static2 if write

/etc/init.d/haproxy reload #重新加载配置文件

将上传脚本放在server2、3的httpd默认发布目录/var/www/html/下(server2、3都要安装php)
给upload目录一个777权限

访问172.25.31.6 上传图片

图片将上传到server3上

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值