公司變更股權(quán)怎么辦理北京廈門網(wǎng)站優(yōu)化
目錄
Haproxy概述
haproxy算法:
Haproxy實(shí)現(xiàn)七層負(fù)載
①部署nginx-server測(cè)試頁(yè)面
②(主/備)部署負(fù)載均衡器
?③部署keepalived高可用
?④增加對(duì)haproxy健康檢查
⑤測(cè)試
Haproxy概述
haproxy---主要是做負(fù)載均衡的7層,也可以做4層負(fù)載均衡
apache也可以做7層負(fù)載均衡,但是很麻煩。實(shí)際工作中沒有人用。
負(fù)載均衡是通過OSI協(xié)議對(duì)應(yīng)的
7層負(fù)載均衡:用的7層http協(xié)議,
4層負(fù)載均衡:用的是tcp協(xié)議加端口號(hào)做的負(fù)載均衡haproxy算法:
1.roundrobin
基于權(quán)重進(jìn)行輪詢,在服務(wù)器的處理時(shí)間保持均勻分布時(shí),這是最平衡,最公平的算法.此算法是動(dòng)態(tài)的,這表示其權(quán)重可以在運(yùn)行時(shí)進(jìn)行調(diào)整.不過在設(shè)計(jì)上,每個(gè)后端服務(wù)器僅能最多接受4128個(gè)連接
2.static-rr
基于權(quán)重進(jìn)行輪詢,與roundrobin類似,但是為靜態(tài)方法,在運(yùn)行時(shí)調(diào)整其服務(wù)器權(quán)重不會(huì)生效.不過,其在后端服務(wù)器連接數(shù)上沒有限制
3.leastconn
新的連接請(qǐng)求被派發(fā)至具有最少連接數(shù)目的后端服務(wù)器.
Haproxy實(shí)現(xiàn)七層負(fù)載
keepalived+haproxy
192.168.134.165? master
192.168.134.166 slave
192.168.134.163? nginx-server
192.168.134.164 nginx-server
192.168.134.160? ?VIP(虛擬IP)
①部署nginx-server測(cè)試頁(yè)面
兩臺(tái)nginx都部署方便測(cè)試
[root@server03 ~]# yum -y install nginx
[root@server03 ~]# systemctl start nginx
[root@server03 ~]# echo "webserver01..." > /usr/share/nginx/html/index.html [root@server04 ~]# yum -y install nginx
[root@server04 ~]# systemctl start nginx
[root@server04 ~]# echo "webserver02..." > /usr/share/nginx/html/index.html
②(主/備)部署負(fù)載均衡器
[root@server01 ~]# yum -y install haproxy
[root@server01 ~]# vim /etc/haproxy/haproxy.cfg
globallog 127.0.0.1 local2 infopidfile /var/run/haproxy.pidmaxconn 4000user haproxygroup haproxydaemonnbproc 1
defaultsmode httplog globalretries 3option redispatchmaxconn 4000contimeout 5000clitimeout 50000srvtimeout 50000
listen statsbind *:81stats enablestats uri /haproxystats auth aren:123
frontend webmode httpbind *:80option httplogacl html url_reg -i \.html$use_backend httpservers if htmldefault_backend httpservers
backend httpserversbalance roundrobinserver http1 192.168.134.163:80 maxconn 2000 weight 1 check inter 1s rise 2 fall 2server http2 192.168.134.164:80 maxconn 2000 weight 1 check inter 1s rise 2 fall 2[root@server01 ~]# systemctl start haproxy
瀏覽器訪問haproxy監(jiān)控
master:
slave:
頁(yè)面主要參數(shù)解釋
Queue
Cur: current queued requests //當(dāng)前的隊(duì)列請(qǐng)求數(shù)量
Max:max queued requests ????//最大的隊(duì)列請(qǐng)求數(shù)量
Limit: ??????????//隊(duì)列限制數(shù)量Errors
Req:request errors ????????????//錯(cuò)誤請(qǐng)求
Conn:connection errors ?????????//錯(cuò)誤的連接Server列表:
Status:狀態(tài),包括up(后端機(jī)活動(dòng))和down(后端機(jī)掛掉)兩種狀態(tài)
LastChk: ???持續(xù)檢查后端服務(wù)器的時(shí)間
Wght: (weight) : 權(quán)重
?③部署keepalived高可用
注意:master和slave的優(yōu)先級(jí)不一樣,但虛擬路由id(virtual_router_id)保持一致;并且slave配置?nopreempt(不搶占資源)
master:
[root@server01 ~]# yum -y install keepalived
[root@server01 ~]# vim /etc/keepalived/keepalived.conf
! Configuration File for keepalivedglobal_defs {router_id director1
}
vrrp_instance VI_1 {state MASTERinterface ens33virtual_router_id 80priority 100advert_int 1authentication {auth_type PASSauth_pass 1111}virtual_ipaddress {192.168.134.160/24}
}[root@server01 ~]# systemctl start keepalived
slaver:
[root@localhost ~]# yum -y install keepalived
[root@localhost ~]# vim /etc/keepalived/keepalived.conf
! Configuration File for keepalivedglobal_defs {router_id directory2
}
vrrp_instance VI_1 {state BACKUPinterface ens33nopreemptvirtual_router_id 80priority 50advert_int 1authentication {auth_type PASSauth_pass 1111}virtual_ipaddress {192.168.134.160/24}
}
[root@localhost ~]# systemctl start keepalived
查看IP
?④增加對(duì)haproxy健康檢查
兩臺(tái)機(jī)器都做,讓Keepalived以一定時(shí)間間隔執(zhí)行一個(gè)外部腳本,腳本的功能是當(dāng)Haproxy失敗,則關(guān)閉本機(jī)的Keepalived。
[root@server01 ~]# vim /etc/keepalived/check.sh
#!/bin/bash /usr/bin/curl -I http://localhost &>/dev/null
if [ $? -ne 0 ];then
# /etc/init.d/keepalived stopsystemctl stop keepalived
fi
[root@server01 ~]# chmod a+x /etc/keepalived/check.sh
在keepalived增加健康檢查配置vrrp_script check_haproxy并且用?track_script調(diào)用。
! Configuration File for keepalivedglobal_defs {router_id director1
}
vrrp_script check_haproxy {script "/etc/keepalived/check.sh"interval 5
}
vrrp_instance VI_1 {state MASTERinterface ens33virtual_router_id 80priority 100advert_int 1authentication {auth_type PASSauth_pass 1111}virtual_ipaddress {192.168.134.160/24}track_script {check_haproxy}
}
重啟keepalived
[root@server01 ~]# systemctl restart keepalived
⑤測(cè)試
關(guān)閉master的haproxy服務(wù)可以發(fā)現(xiàn)master的keepalived服務(wù)也關(guān)閉,此時(shí)master上的VIP轉(zhuǎn)移到slave上
- 關(guān)閉master的服務(wù)并查看VIP
- 查看slave的IP可以發(fā)現(xiàn)VIP跳轉(zhuǎn)至此。
- 在web界面查看服務(wù)是否正常
第一次刷新
第二次刷新