日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

高可用集群下的负载均衡(6):haproxy实现访问不同资源的负载均衡(日志、监控、acl访问控制的配置)

發布時間:2023/12/15 编程问答 24 豆豆
生活随笔 收集整理的這篇文章主要介紹了 高可用集群下的负载均衡(6):haproxy实现访问不同资源的负载均衡(日志、监控、acl访问控制的配置) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

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的配置文件中添加回環

[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】日志配置成功
可查看到日志內容:

[root@server2 haproxy]# vim haproxy.cfg [root@server2 haproxy]# systemctl restart haproxy [root@server2 haproxy]# > /var/log/messages [root@server2 haproxy]# cat /var/log/messages [root@server2 haproxy]# systemctl restart haproxy.service [root@server2 haproxy]# cat /var/log/messages

二、haproxy實現不同類型不同調用?

【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】

[root@server2 haproxy]# vim haproxy.cfg 90 #balance roundrobin91 # balance source92 balance static-rr93 server app1 172.25.15.3:80 check94 server app2 172.25.15.4:80 check[root@server2 haproxy]# systemctl restart haproxy


3. 分別采用源路由算法和權重算法

[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

  • 源路由算法
[root@server2 ~]# vim /etc/haproxy/haproxy.cfgbackend app# balance roudrobinbalance source# balance static-rrserver app1 172.25.18.3:80 check server app2 172.25.18.4:80 check [root@server2 ~]# systemctl restart haproxy


  • 權重算法
[root@server2 ~]# vim /etc/haproxy/haproxy.cfgbackend appbalance static-rrserver app1 172.25.18.3:80 check weight 1server app2 172.25.18.4:80 check weight 2[root@server2 ~]# systemctl restart haproxy


用server1測試:

4.當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 later

5.訪問限制

【1】將172.25.18.250主機加入黑名單,不能訪問server3,server4,當其訪問時,會跳轉到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實現動靜分離

server3發布靜態頁面,server4發布php動態頁面

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



總結

以上是生活随笔為你收集整理的高可用集群下的负载均衡(6):haproxy实现访问不同资源的负载均衡(日志、监控、acl访问控制的配置)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。