高可用集群下的负载均衡(6):haproxy实现访问不同资源的负载均衡(日志、监控、acl访问控制的配置)
                                                            生活随笔
收集整理的這篇文章主要介紹了
                                高可用集群下的负载均衡(6):haproxy实现访问不同资源的负载均衡(日志、监控、acl访问控制的配置)
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.                        
                                8.7am 2
1.haproxy的日志管理
【1】修改日至配置文件
[root@server2 haproxy]# pwd /etc/haproxy [root@server2 haproxy]# vim /etc/rsyslog.conf14 #Provides UDP syslog reception15 $ModLoad imudp #接受haproxy日志16 $UDPServerRun 51472 # Save boot messages also to boot.log73 local7.* /var/log/boot.log74 75 local2.* /var/log/haproxy.log # 添加local2,日志文件位置 [root@server2 haproxy]# systemctl restart rsyslog # 重啟【2】在server2下haproxy的配置文件中添加回環(huán)
[root@server2 haproxy]# vim haproxy.cfgbackend appbalance roundrobinserver app1 172.25.18.3:80 checkserver app2 172.25.18.4:80 checkserver buckup 127.0.0.1:8080 backup[root@server2 haproxy]# systemctl restart haproxy
 【3】日志配置成功
 可查看到日志內(nèi)容:
二、haproxy實現(xiàn)不同類型不同調(diào)用?
【1】在server2中修改haproxy的配置文件
[root@server2 ~]# vi /etc/haproxy/haproxy.cfg frontend mainbind *:80acl url_static path_beg -i /static /images /javascript /stylesheets #修改此處acl url_static path_end -i .jpg .gif .png .css .jsuse_backend static if url_staticdefault_backend app #--------------------------------------------------------------------- # static backend for serving up images, stylesheets and such #--------------------------------------------------------------------- backend staticbalance roundrobinserver static 172.25.14.4:80 check ## server3 #--------------------------------------------------------------------- # round robin balancing between the various backends #--------------------------------------------------------------------- backend app#balance roundrobinbalance sourceserver app1 172.25.15.3:80 checkserver app2 172.25.15.4:80 check ------------------------------------------------------------------------ [root@server2 haproxy]# systemctl restart haproxy
 
 
 
 【2】
 
3. 分別采用源路由算法和權(quán)重算法
[root@server2 html]# vi /etc/haproxy/haproxy.cfgfrontend main *:80acl blacklist scr 172.25.15.1block if blacklist # acl url_static path_beg -i /static images /javascript /stylesheets # acl url_static path_end -i .jpg .gif .png .css .js# acl read_request method GEF# acl read_request method HEAD# acl write_request method PUT# acl write_request method POST# use_backend static if url_static # use_backend app if write_requestdefault_backend app[root@server2 haproxy]# systemctl reload haproxy- 源路由算法
 
 
- 權(quán)重算法
 
 用server1測試:
 
4.當(dāng)server3,server4停用,訪問backup備機
[root@server2 haproxy]# vim haproxy.cfg [root@server2 haproxy]# systemctl reload haproxy [root@server2 haproxy]# echo "sorry, please try again later" > /var/www/html/index[root@server3 ~]# systemctl stop httpd [root@server3 ~]# ssh server4 systemctl stop httpd測試: [root@server1 ~]# curl 172.25.18.2 sorry, please try again later [root@server1 ~]# curl 172.25.18.2 sorry, please try again later5.訪問限制
【1】將172.25.18.250主機加入黑名單,不能訪問server3,server4,當(dāng)其訪問時,會跳轉(zhuǎn)到172.0.0.1:8000端口。
[root@server2 haproxy]# vim haproxy.cfgfrontend main *:80acl blacklist src 172.25.18.250block if blacklisterrorloc 403 http://127.0.0.1:8000[root@server2 haproxy]# systemctl reload haproxy【2】限制用戶訪問指定的頁面test.html
frontend main *:80acl blacklist src 172.25.18.250# block if blacklist# errorloc 403 http://127.0.0.1:8000acl testfile path /test.htmlhttp-request deny if testfile blacklist# acl url_static path_beg -i /static /images /javascript /stylesheets# acl url_static path_end -i .jpg .gif .png .css .js# use_backend static if url_staticdefault_backend app測試: [root@server1 ~]# curl 172.25.18.2/test.html test [root@server1 ~]# curl 172.25.18.2/test.html test
 
【3】訪問限制,黑名單用戶訪問時,重定向到百度
[root@server2 haproxy]# vim haproxy.cfgrontend main *:80acl blacklist src 172.25.18.250# block if blacklist# errorloc 403 http://127.0.0.1:8000# acl testfile path /test.html# http-request deny if testfile blacklistredirect location http://www.baidu.com# acl url_static path_beg -i /static /images /javascript /stylesheets# acl url_static path_end -i .jpg .gif .png .css .js# use_backend static if url_staticdefault_backend app[root@server2 haproxy]# systemctl reload haproxy
 
 
6.用haproxy實現(xiàn)動靜分離
server3發(fā)布靜態(tài)頁面,server4發(fā)布php動態(tài)頁面
root@server3 ~]# mkdir /var/www/html/imgaes [root@server3 ~]# cd /var/www/html/images [root@server3 images]# ls redhat.jpg [root@server3 images]# systemctl start httpd [root@server3 images]# ssh server4 systemctl start httpd[root@server4 ~]# vim /var/www/html/index.php [root@server4 ~]# cat /var/www/html/index.php <?php phpinfo() ?>測試: 訪問172.25.13.3/images/redhat.jpg 172.25.13.4/index.php
 
 
總結(jié)
以上是生活随笔為你收集整理的高可用集群下的负载均衡(6):haproxy实现访问不同资源的负载均衡(日志、监控、acl访问控制的配置)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
                            
                        - 上一篇: sftp、nohup、lftp、scp命
 - 下一篇: 重力传感器方向