企业级 ##Haproxy服务动静分离与读写分离相关配置相关配置##

Haproxy服务的概念:

HAProxy 是一款提供高可用性、负载均衡以及基于TCP(第四层)和HTTP(第七层)应用的代理软件。也是是一种高效、可靠、免费的高可用及负载均衡解决方案,非常适合于高负载站点的七层数据请求。客户端通过HAProxy代理服务器获得站点页面,而代理服务器收到客户请求后根据负载均衡的规则将请求数据转发给后端真实服务器。
同一客户端访问服务器,HAProxy保持回话的三种方案:
1 HAProxy将客户端ip进行Hash计算并保存,由此确保相同IP访问时被转发到同一真实服务器上。
2 HAProxy依靠真实服务器发送给客户端的cookie信息进行回话保持。
3 HAProxy保存真实服务器的session及服务器标识,实现会话保持功能。


安装haproxy服务

[root@server1 ~]# yum install gcc         ##下载编译gcc服务
[root@server1 ~]# yum install -y rpm-build        ##下载安装命令
[root@server1 ~]# yum install -y pcre-devel        
[root@server1 ~]# rpmbuild -tb haproxy-1.6.11.tar.gz      ##解压并编译
[root@server1 ~]# cd rpmbuild/RPMS/x86_64/
[root@server1 x86_64]# ls
haproxy-1.6.11-1.x86_64.rpm
[root@server1 x86_64]# rpm -qpl haproxy-1.6.11-1.x86_64.rpm         ##查看依赖性
[root@server1 x86_64]# rpm -ivh haproxy-1.6.11-1.x86_64.rpm    ##忽略依赖性安装
Preparing...                ########################################### [100%]
   1:haproxy                ########################################### [100%]
[root@server1 ~]# tar zxf haproxy-1.6.11.tar.gz     ##进行解压
[root@server1 ~]# ls
anaconda-ks.cfg  haproxy-1.6.11.tar.gz  install.log.syslog
haproxy-1.6.11   install.log            rpmbuild
[root@server1 ~]# cd haproxy-1.6.11
[root@server1 haproxy-1.6.11]# ls
CHANGELOG     doc       include      Makefile  src      VERDATE
contrib       ebtree    LICENSE      README    SUBVERS  VERSION
CONTRIBUTING  examples  MAINTAINERS  ROADMAP   tests
[root@server1 haproxy-1.6.11]# cd examples/
[root@server1 examples]# ls
acl-content-sw.cfg     debug2html    init.haproxy
auth.cfg               debugfind     option-http_proxy.cfg
check                  errorfiles    seamless_reload.txt
check.conf             haproxy.init  ssl.cfg
content-sw-sample.cfg  haproxy.spec  stats_haproxy.sh
debug2ansi             haproxy.vim   transparent_proxy.cfg
[root@server1 examples]# cp content-sw-sample.cfg  /etc/haproxy/haproxy.cfg                 ##复制配置文件
[root@server1 examples]# cd /etc/haproxy/
[root@server1 haproxy]# ls
haproxy.cfg

配置haproxy服务

1.建立haproxy登陆用户:

[root@server1 haproxy]# groupadd -g 200 haproxy
[root@server1 haproxy]# useradd -u 200 -g 200 -M haproxy
[root@server1 haproxy]# id haproxy           ##查看信息
uid=200(haproxy) gid=200(haproxy) groups=200(haproxy)

2.添加系统对haproxy服务进程文件的配置:

[root@server1 haproxy]# vim /etc/security/limits.conf
haproxy      -      nofile    10000

3.编辑配置文件:
[root@server1 haproxy]# vim haproxy.cfg

defaults
        mode            http
        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


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

# The static backend backend for 'Host: img', /img and /css.
backend static
        mode            http
        balance         roundrobin
        server          statsrv1 172.25.39.2:80 check inter 1000
        server          statsrv2 172.25.39.3:80 check inter 1000
[server]后面的全部删除

4.打开服务:
【server1】

[root@server1 haproxy]# /etc/init.d/haproxy  start
Starting haproxy:                                          [  OK  ]

【server2】【server3】打开httpd服务
5.进行测试:
(1)真机上进行网页测试haproxy负载均衡
这里写图片描述
这里写图片描述
(2)真机上查看健康检查是否正常
这里写图片描述
(3)真机上查看监控是否正常
这里写图片描述


Haproxy算法:

1.roundrobin : 基于权重轮循。
2.static-rr : 基于权重轮循。静态算法,运行时改变无法生效
3.source : 基于请求源IP的算法。对请求的源IP进行hash运算,然后将结果与后端服务器的权重总数想除后转发至某台匹配服务器。使同一IP客户端请求始终被转发到某特定的后端服务器。
4.leastconn : 最小连接。(适合数据库负载均衡,不适合会话短的环境)
5.uri : 对部分或整体URI进行hash运算,再与服务器的总权重想除,最后转发到匹配后端。
6.uri_param : 根据URL路径中参数进行转发,保证在后端服务器数量不变的情况下,同一用户请求分发到同一机器。
7.hdr() : 根据http头转发,如果不存在http头。则使用简单轮循。

1.配置环境:

[root@server1 haproxy]# vim /etc/rsyslog.conf 
42行:
*.info;mail.none;authpriv.none;cron.none;local0.none                /var/log/messages
62行:
local0.*                                                /var/log/haproxy.log
[root@server1 haproxy]# /etc/init.d/rsyslog restart
Shutting down system logger:                               [  OK  ]
Starting system logger:                                    [  OK  ]
[root@server1 haproxy]# cd /var/log
[root@server1 log]# ls
anaconda.ifcfg.log    btmp           lastlog            spooler
anaconda.log          btmp-20180804  maillog            spooler-20180804
anaconda.program.log  cron           maillog-20180804   tallylog
anaconda.storage.log  cron-20180804  messages           wtmp
anaconda.syslog       dmesg          messages-20180804  yum.log
anaconda.yum.log      dmesg.old      rhsm
audit                 dracut.log     secure
boot.log              haproxy.log    secure-20180804
[root@server1 log]# ll haproxy.log 
-rw------- 1 root root 0 Aug  4 11:27 haproxy.log

2.添加source算法:

[root@server1 haproxy]# vim haproxy.cfg 
backend static
        mode            http
       # balance         roundrobin
        balance          source
        server          statsrv1 172.25.39.2:80 check inter 1000
        server          statsrv2 172.25.39.3:80 check inter 1000

3.进行算法测试:
(1)在【server1】中重启haproxy服务

[root@server1 haproxy]# /etc/init.d/haproxy restart
Shutting down haproxy:                                     [  OK  ]
Starting haproxy:                                          [  OK  ]

(2)在真机网站上访问测试
这里写图片描述
不进行轮询,只能访问到server2,这是算法的作用
(3)在【server2】中关闭httpd服务再进行测试

[root@server2 ~]# /etc/init.d/httpd stop
Stopping httpd:                                            [  OK  ]

这里写图片描述
又重新自动直接转换到【server3】
(4)重新在【server1】中打开httpd服务,因为这个服务默认server2为主机,当服务重启时,自动变为server2,不进行轮询


动静分离

这里写图片描述
1.实验配置:
【server1】

[root@server1 haproxy]# vim haproxy.cfg
43行:
        default_backend static1

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


backend static2
        balance         roundrobin
        server          statsrv2 172.25.39.3:80 check inter 1000
[root@server1 haproxy]# vim haproxy.cfg
43行
use_backend     static2 if { path_end -i  .php   }

【server3】

[root@server3 ~]# yum install -y php
[root@server3 ~]# cd /var/www/html/
[root@server3 html]# vim index.php
编辑内容:
<?php
phpinfo()
?>

在【server3】上重启httpd服务,在【server1】上重启haproxy服务

2.进行在真机上的访问测试:
这里写图片描述
访问的server1可以看到server3中的数据,html是静态,php是动态


重定向:

拒绝客户端主机访问网页
一.普通重定向:
1.修改配置文件,增加黑名单:

[root@server1 haproxy]# vim haproxy.cfg 
 #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   }


        acl blacklist  src 172.25.39.250            ##增加主机黑名单
        http-request  deny if blacklist


        use_backend     static2 if { path_end -i  .php   }
        default_backend static1
[root@server1 haproxy]# /etc/init.d/haproxy reload

2.在客户端进行访问测试:
这里写图片描述
测试结果正确,客户端访问已经被拒绝,但是给客户这样显示不好

二.进行重定向优化:
1.添加优化内容,修改端口:

[root@server1 haproxy]# yum install -y httpd    ##下载httpd服务
[root@server1 haproxy]# cd /var/www/html/
[root@server1 html]# ls
[root@server1 html]# vim index.html           ##添加网站显示文本
网站正在维护.....

[root@server1 html]# vim /etc/httpd/conf/httpd.conf           ##修改端口
Listen 8080
[root@server1 html]# /etc/init.d/httpd start
Starting httpd: httpd: Could not reliably determine the server's fully qualified domain name, using 172.25.39.1 for ServerName
                                                           [  OK  ]
[root@server1 html]# /etc/init.d/haproxy reload

[root@server1 html]# cd /etc/haproxy/
[root@server1 haproxy]# ls
haproxy.cfg
[root@server1 haproxy]# vim haproxy.cfg
    http-request  deny if blacklist
    ##网页访问172.25.39.1时,直接跳转到172.25.39.1:8080
        errorloc 403 http://172.25.39.1:8080   
        use_backend     static2 if { path_end -i  .php   }
        default_backend static1

2.真机上测试:
这里写图片描述


读写分离:

1.【server2】上下载图片:

[root@server2 ~]# cd /var/www/html/
[root@server2 html]# ls
images  index.html
[root@server2 html]# cd images/
[root@server2 images]# mv /root/redhat.jpg  .
[root@server2 images]# ls
redhat.jpg

这里写图片描述
2.真机上进行网页测试:
这里写图片描述
当访问server1时,可以看到server2主机的images下的图片
3.下载服务进行配置:
【真机】

[root@foundation39 Desktop]# lftp 172.25.254.250
lftp 172.25.254.250:~> cd /pub/docs/haproxy/
cd ok, cwd=/pub/docs/haproxy
lftp 172.25.254.250:/pub/docs/haproxy> mirror upload/
Total: 1 directory, 2 files, 0 symlinks 
New: 2 files, 0 symlinks
1184 bytes transferred
lftp 172.25.254.250:/pub/docs/haproxy> quit
[root@foundation39 Desktop]# scp -r  upload [email protected]:/root
root@172.25.39.3's password: 
index.php                                    100%  257     0.3KB/s   00:00    
upload_file.php                              100%  927     0.9KB/s   00:00  

【server3】【server2】

[root@server3 html]# mv /root/upload/  .
[root@server3 html]# ls
index.html  index.php  upload  www1
[root@server3 html]# cd upload/
[root@server3 upload]# ls
index.php  upload_file.php
[root@server3 upload]# mv * ..
mv: overwrite `../index.php'? y
[root@server3 upload]# ls
[root@server3 upload]# cd ..
[root@server3 html]# ls
index.html  index.php  upload  upload_file.php  www1
[root@server3 html]# ll
total 20
-rw-r--r-- 1 root root   30 Aug  4 12:45 index.html
-rw-r--r-- 1 root root  257 Aug  4 14:07 index.php
drwxr-xr-x 2 root root 4096 Aug  4 14:09 upload
-rw-r--r-- 1 root root  927 Aug  4 14:07 upload_file.php
drwxr-xr-x 2 root root 4096 Jul 27 17:06 www1
[root@server3 html]# chmod 777 upload
[root@server3 html]# vim  upload_file.php
<?php
if ((($_FILES["file"]["type"] == "image/gif")
|| ($_FILES["file"]["type"] == "image/jpeg")
|| ($_FILES["file"]["type"] == "image/pjpeg"))
&& ($_FILES["file"]["size"] < 2000000))
  {
  if ($_FILES["file"]["error"] > 0)
    {

【server1】
注释php行
重启服务
4.进行测试:
访问http://172.25.39.1/
这里写图片描述
这里写图片描述

在【server3】中可以看到

[root@server3 upload]# ls
redhat.gif  upload_file.php

连通成功

猜你喜欢

转载自blog.csdn.net/China_zgd/article/details/81408607