HAProxy负载均衡原理及企业级实例部署haproxy集群
HAProxy是一種高效、可靠、免費(fèi)的高可用及負(fù)載均衡解決方案,非常適合于高負(fù)載站點(diǎn)的七層數(shù)據(jù)請求。客戶端通過HAProxy代理服務(wù)器獲得站點(diǎn)頁面,而代理服務(wù)器收到客戶請求后根據(jù)負(fù)載均衡的規(guī)則將請求數(shù)據(jù)轉(zhuǎn)發(fā)給后端真實(shí)服務(wù)器。
同一客戶端訪問服務(wù)器,HAProxy保持回話的三種方案:
HAProxy將客戶端ip進(jìn)行Hash計(jì)算并保存,由此確保相同IP訪問時(shí)被轉(zhuǎn)發(fā)到同一真實(shí)服務(wù)器上。
HAProxy依靠真實(shí)服務(wù)器發(fā)送給客戶端的cookie信息進(jìn)行回話保持。
HAProxy保存真實(shí)服務(wù)器的session及服務(wù)器標(biāo)識,實(shí)現(xiàn)會(huì)話保持功能。
?
HAProxy拓?fù)浣Y(jié)構(gòu)圖
?
二 ?配置文件解析
?
HAProxy安裝后默認(rèn)沒有配置文件,需要手動(dòng)創(chuàng)建/etc/haproxy.cfg。啟動(dòng)HAProxy時(shí)用-f指定配置文件路徑。haproxy的配置文件包含全局設(shè)置段與代理段,global是全局段,defaults、listen、frontend、backend為代理段。frontend用來匹配客戶端請求的域名或者URL;backend定義后端服務(wù)器集群。
HAProxy配置文件參數(shù)詳細(xì)解析:
三 ?Haproxy實(shí)例部署
?
本例使用listen定義一個(gè)監(jiān)控端口;
使用frontend定義一個(gè)前端80端口;
通過backend定義名為inside_servers 和 external_servers的服務(wù)器組;
使用default_backend定義默認(rèn)服務(wù)器組external_servers;
external_servers包括web1.test.com和web2.test.com 兩臺服務(wù)器
inside_servers包含web3.test.com 一臺服務(wù)器
1首先配置web服務(wù)器
在web1、web2、web3上安裝httpd并配置網(wǎng)卡
vim /etc/sysconfig/network-scripts/ifcfg-eth0
DEVICE=eth0
BOOTPROTO=static
IPADDR=192.168.1.3
NETMASK=255.255.255.0
GATEWAY=192.168.1.2
ONBOOT=yes
TYPE=Ethernet
service network restart
yum install -y httpd
iptables -F
iptables -X
service iptables save
setenforce 0
sed -i s/enforcing/disabled/g /etc/sysconfig/selinux
echo "web1 ?192.168.1.3" > /var/www/html/index.html
service httpd restart
chkconfig httpd on
?
web2、web3機(jī)器上執(zhí)行與web1相同步驟,注意修改部分參數(shù)
2接著haproxy服務(wù)器配置
設(shè)置兩塊網(wǎng)卡
vim /etc/sysconfig/network-scripts/ifcfg-eth0
DEVICE=eth0
BOOTPROTO=static
IPADDR=10.10.10.10
NETMASK=255.0.0.0
ONBOOT=yes
TYPE=Ethernet
vim /etc/sysconfig/network-scripts/ifcfg-eth1
DEVICE=eth1
BOOTPROTO=static
IPADDR=192.168.1.2
NETMASK=255.255.255.0
GATEWAY=192.168.1.1
ONBOOT=yes
TYPE=Ethernet
service network restart
service iptables stop
?
內(nèi)核調(diào)優(yōu),修改系統(tǒng)文件
vim /etc/security/limits.conf
*????soft????nofile????????65535?
*????hard????nofile????????65535
配置日志文件,添加三行
vim /etc/rsyslog.conf
$ModLoad????imudp
$UDPServerRun????514
local3.* ? /var/log/haproxy.log
yum -y install gcc
wget?http://www.haproxy.org/download/1.6/src/haproxy-1.6.11.tar.gz
tar zxf?haproxy-1.6.11.tar.gz -C /usr/src/
cd /usr/src/haproxy-1.6.11/
make TARGET=linux2628
make install
mkdir /var/haproxy
3創(chuàng)建配置文件
vim /etc/haproxy.cfg
global
maxconn????4096
log????127.0.0.1????local3????info
chroot????/var/haproxy
uid????99
gid????99
daemon
nbproc????1
pidfile????/var/run/haproxy.pid
ulimit-n????65535
stats????socket????/var/tmp/stats
defaults
log????global
mode????http
maxconn????20480
option????httplog
option????httpclose
option????dontlognull
option????forwardfor
option????redispatch
option????abortonclose
stats????refresh????30
retries????3
balance????roundrobin
cookie????SRV
timeout????check????2000ms
timeout????connect????5000ms
timeout????server????50000ms
timeout????client????50000ms
listen????admin_status????????????????????????????????????????#定義haproxy的監(jiān)控界面
bind????0.0.0.0:6553
mode????http
log????127.0.0.1????local3 info
stats????enable
stats????refresh????5s????????????????????????????????????????????#監(jiān)控頁面自動(dòng)刷新時(shí)間5s
stats????realm????Haproxy\????Statistics????????????????#登錄監(jiān)控頁面提示符
stats????uri????/admin?stats????????????????????????????????? #監(jiān)控頁面URL路徑
stats????auth????admin:123456?????????????????????????????#監(jiān)控頁面的賬戶密碼
stats????hide-version????????????????????????????????????????? ?#隱藏haproxy版本
frontend????web_service?????????????????????????????????????#定義前端服務(wù)器
bind????0.0.0.0:80
mode????http
log????global
option????httplog
option????httpclose
option????forwardfor
#acl????inside_src src 192.168.1.0/24????????????????????#定義acl
#use_backend????inside_servers if inside_src????????#判斷acl的源地址,把請求轉(zhuǎn)發(fā)到inside_servers組
default_backend????external_servers?????????????????????#默認(rèn)服務(wù)器組
backend????external_servers
mode????http
balance????roundrobin????????????????????????????????????????????#輪詢真實(shí)服務(wù)器
option????httpchk????GET????/index.html????????????????#檢查index文件,判斷服務(wù)器是否健康
##定義后端真實(shí)服務(wù)器,向cookie中插入web1信息,check進(jìn)行健康檢查,檢查時(shí)間間隔為2000ms,##連續(xù)兩次健康則認(rèn)為是正常開啟的,連續(xù)三次檢查失敗則認(rèn)為宕機(jī),服務(wù)器權(quán)重1
server web1 192.168.1.3:80 cookie web1 check inter 2000 rise 2 fall 3 weight 1
server web2 192.168.1.4:80 cookie web2 check inter 2000 rise 2 fall 3 weight 1
#backend ? ?inside_servers
#mode????http
#balance????roundrobin?????????????????????????????????????????#輪詢真實(shí)服務(wù)器
#option????httpchk????GET????/index.html????????????????#檢查index文件,判斷服務(wù)器是否健康
#server web3 192.168.1.5:80 cookie web3 check inter 2000 rise 2 fall 3 weight 1
?
4啟動(dòng)haproxy服務(wù)
service rsyslog restart????????????????#重啟系統(tǒng)日志服務(wù)
haproxy -f /etc/haproxy.cfg????????#啟動(dòng)haproxy服務(wù)
echo "/usr/local/sbin/haproxy -f /etc/haproxy.cfg" >> /etc/rc.local
5測試驗(yàn)證
瀏覽器訪問監(jiān)控頁面
在一個(gè)ip為10.10.10.100的機(jī)器上訪問http://10.10.10.10:6553/admin?stats
多次刷新訪問將得到web1和web2 不同頁面信息
如果配置文件中開啟使用inside_servers,則在192.168.1.0/24網(wǎng)段機(jī)器上訪問http://192.168.1.2,服務(wù)器返回的會(huì)一直是web3的頁面信息。
轉(zhuǎn)載于:https://www.cnblogs.com/nongchaoer/p/6410944.html
總結(jié)
以上是生活随笔為你收集整理的HAProxy负载均衡原理及企业级实例部署haproxy集群的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 因一纸设计稿,我把竞品APP扒得裤衩不剩
- 下一篇: MATLAB 拟合曲线