日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 >

项目实战4—HAProxy实现高级负载均衡实战和ACL控制

發(fā)布時(shí)間:2024/9/21 42 豆豆
生活随笔 收集整理的這篇文章主要介紹了 项目实战4—HAProxy实现高级负载均衡实战和ACL控制 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
分類:?Linux架構(gòu)篇

??轉(zhuǎn)自https://www.cnblogs.com/along21/

?haproxy實(shí)現(xiàn)高級(jí)負(fù)載均衡實(shí)戰(zhàn)

  環(huán)境:隨著公司業(yè)務(wù)的發(fā)展,公司負(fù)載均衡服務(wù)已經(jīng)實(shí)現(xiàn)四層負(fù)載均衡,但業(yè)務(wù)的復(fù)雜程度提升,公司要求把mobile手機(jī)站點(diǎn)作為單獨(dú)的服務(wù)提供,不在和pc站點(diǎn)一起提供服務(wù),此時(shí)需要做7層規(guī)則負(fù)載均衡,運(yùn)維總監(jiān)要求,能否用一種服務(wù)同既能實(shí)現(xiàn)七層負(fù)載均衡,又能實(shí)現(xiàn)四層負(fù)載均衡,并且性能高效,配置管理容易,而且還是開源。

總項(xiàng)目流程圖,詳見?http://www.cnblogs.com/along21/p/8000812.html

Haproxy詳解和相關(guān)代碼段含義詳見,詳見?http://www.cnblogs.com/along21/p/7899771.html

實(shí)驗(yàn)前準(zhǔn)備

① 兩臺(tái)服務(wù)器都使用yum 方式安裝haproxy 和 keepalived 服務(wù)

yum -y install haproxy

yum -y install keepalived

② iptables -F && setenforing 清空防火墻策略,關(guān)閉selinux

??

實(shí)戰(zhàn)一:實(shí)現(xiàn)基于Haproxy+Keepalived負(fù)載均衡高可用架構(gòu)

1、環(huán)境準(zhǔn)備:

機(jī)器名稱

IP配置

服務(wù)角色

備注

haproxy-server-master

VIP:172.17.100.100

DIP:172.17.1.6

負(fù)載均衡器

主服務(wù)器

配置keepalived

haproxy-server-backup

VIP:172.17.100.100

DIP:172.17.11.11

負(fù)載服務(wù)器

從服務(wù)器

配置keepalived

rs01

RIP:172.17.1.7

后端服務(wù)器

?

rs02

RIP:172.17.22.22

后端服務(wù)器

?

?

2、先配置好keepalived的主從

(1)在haproxy-server-master 上

vim /etc/keepalived/keepalived.conf

! Configuration File for keepalived global_defs {notification_email {root@localhost}notification_email_from root@along.comsmtp_server 127.0.0.1smtp_connect_timeout 30router_id keepalived_haproxy }vrrp_script chk_haproxy { #定義一個(gè)腳本,發(fā)現(xiàn)haproxy服務(wù)關(guān)閉就降優(yōu)先級(jí)script "killall -0 haproxy"interval 2fall 2rise 2weight -4 }vrrp_instance VI_1 {state MASTERinterface eth0virtual_router_id 191priority 100advert_int 1authentication {auth_type PASSauth_pass along}virtual_ipaddress {172.17.100.100} track_script { #執(zhí)行腳本 chk_haproxy } }

service keepalived start 開啟keepalived服務(wù)

開啟服務(wù)后可以查看,VIP已經(jīng)生成

?

(2)在haproxy-server-backup 從上:只需把主換成從,優(yōu)先級(jí)降低就好

vim /etc/keepalived/keepalived.conf

! Configuration File for keepalived global_defs {notification_email {root@localhost}notification_email_from root@along.comsmtp_server 127.0.0.1smtp_connect_timeout 30router_id keepalived_haproxy }vrrp_instance VI_1 {state BACKUPinterface eth0virtual_router_id 191priority 98advert_int 1authentication {auth_type PASSauth_pass along}virtual_ipaddress {172.17.100.100} }

?

service keepalived start 開啟keepalived服務(wù)

?

3、配置haproxy ,總共有兩大段,和第二大段的4小段,兩個(gè)haproxy可以配置的一樣

(1)第一大段:global 全局段

globallog 127.0.0.1 local2chroot /var/lib/haproxypidfile /var/run/haproxy.pidmaxconn 40000user haproxygroup haproxydaemonstats socket /var/lib/haproxy/stats

(2)第二大段:proxies 對(duì)代理的設(shè)定

① defaults 默認(rèn)參數(shù)設(shè)置段 defaultsmode httplog globaloption httplogoption dontlognulloption http-server-closeoption forwardfor except 127.0.0.0/8option redispatchretries 3timeout http-request 10stimeout queue 1mtimeout connect 10stimeout client 1mtimeout server 1mtimeout http-keep-alive 10stimeout check 10smaxconn 3000② listen 段 listen stats bind 0.0.0.0:1080 stats enable stats hide-version stats uri /haproxyadmin stats auth along:along stats admin if TRUE③ frontend 與客戶端建立連接,打開服務(wù)監(jiān)聽端口段 frontend web bind :80 default_backend lnmp-server④ backend 與后端服務(wù)器聯(lián)系段 backend lnmp-serverbalance roundrobinoption httpchk GET /index.htmlserver lnmpserver1 172.17.1.7:80 check inter 3000 rise 3 fall 5server lnmpserver2 172.17.22.22:80 check inter 3000 rise 3 fall 5

?

開啟服務(wù) service haproxy start

?

4、在后端server·打開事先準(zhǔn)備好的web server

systemctl start nginx

systemctl start php-fpm

systemctl start mariadb

?

5、測試

(1)網(wǎng)頁訪問?http://172.17.100.100:1080/haproxyadmin 進(jìn)入狀態(tài)監(jiān)控頁面,可以控制自己的后端服務(wù)

?

(2)可以壞2臺(tái)不是一組的機(jī)器

一臺(tái)后端server宕機(jī),haproxy會(huì)調(diào)度到另一個(gè)server,繼續(xù)提供服務(wù)

一個(gè)主的haproxy宕機(jī),keepalived會(huì)把VIP漂移到從上,繼續(xù)提供服務(wù)

?

實(shí)戰(zhàn)二:基于ACL控制實(shí)現(xiàn)動(dòng)靜分離

原理:acl:訪問控制列表,用于實(shí)現(xiàn)基于請求報(bào)文的首部響應(yīng)報(bào)文的內(nèi)容其它的環(huán)境狀態(tài)信息來做出轉(zhuǎn)發(fā)決策,這大大增強(qiáng)了其配置彈性。其配置法則通常分為兩步,首先去定義ACL?,即定義一個(gè)測試條件,而后在條件得到滿足時(shí)執(zhí)行某特定的動(dòng)作,如阻止請求或轉(zhuǎn)發(fā)至某特定的后端。

1、環(huán)境準(zhǔn)備:

?

機(jī)器名稱

IP配置

服務(wù)角色

備注

haproxy-server

172.17.2.7

負(fù)載均衡器

配置keepalived

ACL控制

rs01

RIP:192.168.30.107

靜態(tài)服務(wù)器

?小米網(wǎng)頁

rs02

RIP:192.168.30.7

動(dòng)態(tài)服務(wù)器

?小米網(wǎng)頁

2、在haproxy 上定義ACL和后端服務(wù)器

vim /etc/haproxy/haproxy.cfg? 前面global 全局段和default 段不用修改

① 定義web 監(jiān)控頁面 listen stats bind 0.0.0.0:1080 stats enable stats hide-version stats uri /haproxyadmin stats auth along:along stats admin if TRUE② 在frontend 段定義ACL frontend webbind :80acl staticfile path_end .jpg .png .bmp .htm .html .css .jsacl appfile path_end .phpuse_backend staticsrvs if staticfiledefault_backend appsrvs③ 設(shè)置backend 后端集群組 backend staticsrvsbalance roundrobinserver staticweb 192.168.30.107:80 check inter 3000 rise 3 fall 3backend appsrvsbalance roundrobinserver appweb 192.168.30.7:80 check inter 3000 rise 3 fall 3

?

3、開啟后端web服務(wù)

systemctl start nginx

systemctl start php-fpm

systemctl start mariadb

?

4、測試結(jié)果

(1)后端服務(wù)器正常時(shí)

web 檢測頁面,一切正常

?

(2)當(dāng)后端靜態(tài)頁面服務(wù)集群宕機(jī),顯示不出靜態(tài)頁面,說明動(dòng)靜分離成功

?

?

實(shí)戰(zhàn)三:基于ACL實(shí)現(xiàn)權(quán)限控制及會(huì)話保持

1、環(huán)境準(zhǔn)備:

?

機(jī)器名稱

IP配置

服務(wù)角色

備注

haproxy-server

172.17.2.7

負(fù)載均衡器

配置keepalived

ACL控制

rs01

RIP:192.168.30.107

后端服務(wù)器

?小米網(wǎng)頁

rs02

RIP:192.168.30.7

后端服務(wù)器

?小米網(wǎng)頁

2、這haproxy 上定義ACL和后端服務(wù)器

vim /etc/haproxy/haproxy.cfg? 前面global 全局段和default 段不用修改

① 定義web 監(jiān)控頁面 listen stats bind 0.0.0.0:1080 stats enable stats hide-version stats uri /haproxyadmin stats auth along:along stats admin if TRUE② 在frontend 段定義ACL,用戶權(quán)限控制 frontend webbind :80acl allow_src src 172.17.0.0/16block unless allow_srcdefault_backend appsrvs③ 設(shè)置backend 后端集群組,設(shè)置cookie,會(huì)話保持 backend staticsrvsbalance roundrobincookie SRV insert nocacheserver appweb1 192.168.30.107:80 check inter 3000 rise 3 fall 3 cookie srv1server appweb2 192.168.30.7:80 check inter 3000 rise 3 fall 3 cookie srv2

?

3、開啟后端web服務(wù)

systemctl start nginx

systemctl start php-fpm

systemctl start mariadb

?

4、檢測結(jié)果

(1)檢測權(quán)限控制

① 在172.17.0.0 段的機(jī)器訪問,正常

?

?② 在這個(gè)網(wǎng)段外的機(jī)器訪問,拒絕

?

?

(2)檢測會(huì)話保持

① 分別在兩個(gè)后端創(chuàng)建兩個(gè)測試頁面

vim ../test.html

server 1/2

② 測試

curl 測試需加-b SRV= 指定的對(duì)應(yīng)cookie訪問

curl -b SRV=srv1 172.17.2.7/test.html

curl -b SRV=srv2 172.17.2.7/test.html

?

?

實(shí)戰(zhàn)四:實(shí)現(xiàn)haproxy的ssl加密

1、自簽生成證書

cd /etc/pki/tls/certs

make /etc/haproxy/haproxy.pem

ls /etc/haproxy/haproxy.pem 確實(shí)生成了證書和秘鑰的文件

?

2、在haproxy 中設(shè)置

frontend webbind :80bind :443 ssl crt /etc/haproxy/haproxy.pem 監(jiān)聽443端口,且是ssl加密redirect scheme https if !{ ssl_fc } 實(shí)現(xiàn)302重定向,將80跳轉(zhuǎn)到443端口

3、網(wǎng)頁訪問?https://172.17.11.11/

?

轉(zhuǎn)載于:https://www.cnblogs.com/skyhu365/p/10636145.html

與50位技術(shù)專家面對(duì)面20年技術(shù)見證,附贈(zèng)技術(shù)全景圖

總結(jié)

以上是生活随笔為你收集整理的项目实战4—HAProxy实现高级负载均衡实战和ACL控制的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。