haproxy实现的负载均衡;动静分离;重定向;读写分离

基于haproxy实现的服务

安装环境部署:
server3 作为haproxy 实现负载均衡;动静分离;重定向;读写分离
server1—->web—>real server
server2—->web—>real server

yum install  gcc -y
yum install rpm-build -y
yum install pcre-devel 
rpmbuild -tb haproxy-1.6.11.tar.gz #解压并编译源码压缩包
cd rpmbuild/RPMS/
rpm -qpl haproxy-1.6.11-1.x86_64.rpm 
rpm -ivh haproxy-1.6.11-1.x86_64.rpm  #安装配置文件
tar zxf haproxy-1.6.11.tar.gz 
cd haproxy-1.6.11
find -name *.spec
cd examples/
ls
cp content-sw-sample.cfg /etc/haproxy/haproxy.cfg

建立haproxy用户

cd /etc/haproxy/
vim haproxy.cfg 
vim /etc/init.d/haproxy #指定haproxy配置文件为 /etc/haproxy/haproxy.cfg
grep 200 /etc/passwd
groupadd -g 200 haproxy #建立haproxy用户组
useradd -u 200 -g 200 -M haproxy #建立uid 200用户haproxy
id haproxy

修改操作系统参数

vim haproxy.cfg  #查看程序参数
vim /etc/security/limits.conf  #设置操作系统参数
haproxy          -       nofile          10000

1>负载均衡:roundrobin

vim haproxy.cfg 
global                     #全局变量
        maxconn         10000 
        stats socket    /var/run/haproxy.stat mode 600 level admin
        log             127.0.0.1 local0
        uid             200
        gid             200
        chroot          /var/empty
        daemon
Defaults                  #默认变量
        mode            http
        log             global
        option          httplog
        option          dontlognull
        monitor-uri     /monitoruri #监控uri端口
        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 clea
 #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 #默认后段域staic算法


#默认列表规则 访问静态算法为roundrobin

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请求
            *:80 name clear
        acl bl
8.balance rdp-cookie(name) # 根据据cookie(name)来锁定并哈希每一次TCP请求

负载均衡实现配置文件

# The static backend backend for 'Host: img', /img and /css. #静态访问一般为Host: img', /img and /css.格式

backend static
        mode            http
        balance         roundrobin

        server          statsrv1 172.25.30.2:80 check inter 1000
        server          statsrv2 172.25.30.1:80 check inter 1000

/etc/init.d/haproxy restart


默认访问static1就会一直访问static1中的规则

# The public 'www' address in the DMZ
frontend public
        bind            *:80 name clear
        default_backend static1 #默认访问static1域中的算法即会一直访问stactic1中指向的statsrv1 172.25.30.2:80
backend static1
        balance         roundrobin
        server          statsrv1 172.25.30.2:80 check inter 1000
backend static2
        balance         roundrobin
        server          statsrv2 172.25.30.1:80 check inter 1000

2>haproxy日至管理监控rsyslog

vim /etc/rsyslog.conf
# Provides UDP syslog reception
$ModLoad imudp #打开UDP传输端口
$UDPServerRun 514
*.info;mail.none;authpriv.none;cron.none;local0.none                /var/log/messages #让/var/log/messages不记录local0的日至
local7.*                                                /var/log/boot.log
local0.*                                                /var/log/haproxy.log
# local0.*的监控日志记录在/var/log/haproxy.log
 /etc/init.d/rsyslog restart #重新加载rsyslog服务
访问负载段就会生成数据记录在/var/log/haproxy.log


3>动静分离php
如果访问hap动态页面就会寻址到动态页面172.25.30.3/index.php
这里访问static2中的server2,我们用php来写动态页面

yum install php -y
vim /var/www/html/index.php
~
<?php
phpinfo()
?>
~   
/etc/init.d/httpd restart #需要重新加载因为apche访问默认发布.php文件
访问172.25.30.3/index.php----->static2---->server2---->(当访问apache时默认访问/var/www/html/*.php页面)index.php
 vim haproxy.cfg 
# The public 'www' address in the DMZ
frontend public
        bind            *:80 name clear
        use_backend      static2  if{ path_end -i .php }#动态访问用 static2访问server2的php页面
        default_backend static1
backend static1
        balance         roundrobin
        server          statsrv1 172.25.30.1:80 check inter 1000

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


动态访问:

4>访问控制:acl

errorloc
(定义列表)blacklist—>http-request deny(黑名单)
当定义的列表中指向黑名单用户那么src中就是黑名单用户访问时会得到403的拒绝
错误重定向errorloc 403 当黑名单用户访问时自动重定向到本机8080端口

frontend public
        bind            *:80 name clear
        acl blacklist src 172.25.30.250
        http-request deny if blacklist
        errorloc 403 http://172.25.30.3:8080
yum install httpd
vim /ect/httpd/conf/httpd.conf #编辑http配置文件修改端口为8080
/etc/init.d/httpd restart
vim /var/www/html/index.html #错误回访页面--->正在维护中



重定向redirect
关键字:redirect
访问haproxy重定向到http://www.baidu.com
也可以重定向到http://172.25.30.2/index.php
不能重定向到自己否则一直会在重定向的过程中
即redirect location http://127.0.0.1:8080
或者redirect location http://www.westos.org #在访问端设置域名解析

 acl blacklist src 172.25.30.250
 redirect  location http://www.baidu.com
 #redirect  location http://172.25.30.2/index.php
 default_backend static1


5>读写分离:

acl write method POST 
acl write method PUT
        use_backend      static2  if write
        default_backend static1 #默认访问用static1


写进static2—->server2—->/var/www/html/upload(777添加权限允许写入)—–>upload_file.php(编辑写入信息要求比如图片规则)
访问进入谁的页面就用谁写不过无论访问谁最终写入还是write—>static2—>server
要写入文件首先访问界面就要是能够写入文件页面
安装php用php语言写

yum install php server1,server2都要安装
覆盖/var/www/html原来界面下载upload--->/var/www/html/
编辑写入文件规则要求vim /var/www/html/upload_file.php
chmod 777 /var/www/html


编辑写入规则:vim upload_file.php

访问haproxy上传文件—->sbmmit—>在server/upload查看


猜你喜欢

转载自blog.csdn.net/dreamer_xixixi/article/details/81413781
今日推荐