負(fù)載均衡:Load Balance,簡(jiǎn)稱LB,是一種服務(wù)或基于硬件設(shè)備等實(shí)現(xiàn)的高可用反向代理技術(shù),負(fù)載均衡將特定的業(yè)務(wù)(web服務(wù)、網(wǎng)絡(luò)流量等)分擔(dān)給指定的一個(gè)或多個(gè)后端特定的服務(wù)器或設(shè)備,從而提高了公司業(yè)務(wù)的并發(fā)處理能力、保證了業(yè)務(wù)的高可用性、方便了業(yè)務(wù)后期的水平動(dòng)態(tài)擴(kuò)展。
為什么使用負(fù)載均衡
Web服務(wù)器的動(dòng)態(tài)水平擴(kuò)展-->對(duì)用戶無(wú)感知 增加業(yè)務(wù)并發(fā)訪問(wèn)及處理能力-->解決單服務(wù)器瓶頸問(wèn)題 節(jié)約公網(wǎng)IP地址-->降低IT支出成本 隱藏內(nèi)部服務(wù)器IP-->提高內(nèi)部服務(wù)器安全性 配置簡(jiǎn)單-->固定格式的配置文件 功能豐富-->支持四層和七層,支持動(dòng)態(tài)下線主機(jī) 性能較強(qiáng)-->并發(fā)數(shù)萬(wàn)甚至數(shù)十萬(wàn)
負(fù)載均衡類型
四層: LVS:Linux Virtual Server Nginx:1.9版之后 HAProxy:High Availability Proxy 七層: HAProxy Nginx
硬件:
F5 https://f5.com/zh Netscaler https://www.citrix.com.cn/products/citrix-adc/ Array https://www.arraynetworks.com.cn/ 深信服 http://www.sangfor.com.cn/ 北京靈州 http://www.lingzhou.com.cn/cpzx/llfzjh/
應(yīng)用場(chǎng)景
四層:Redis、Mysql、RabbitMQ、Memcache等 七層:Nginx、Tomcat、Apache、PHP、圖片、動(dòng)靜分離、API等
HAProxy是法國(guó)開(kāi)發(fā)者威利塔羅(Willy Tarreau) 在2000年使用C語(yǔ)言開(kāi)發(fā)的一個(gè)開(kāi)源軟件,是一款具
備高并發(fā)(一萬(wàn)以上)、高性能的TCP和HTTP負(fù)載均衡器,支持基于cookie的持久性,自動(dòng)故障切換,支
持正則表達(dá)式及web狀態(tài)統(tǒng)計(jì),目前最新TLS版本為2.4
企業(yè)版網(wǎng)站:https://www.haproxy.com/ 社區(qū)版網(wǎng)站:http://www.haproxy.org/ github:https://github.com/haproxy
HAproxy支持功能:
TCP 和 HTTP反向代理 SSL/TSL服務(wù)器 可以針對(duì)HTTP請(qǐng)求添加cookie,進(jìn)行路由后端服務(wù)器 可平衡負(fù)載至后端服務(wù)器,并支持持久連接 支持所有主服務(wù)器故障切換至備用服務(wù)器 支持專用端口實(shí)現(xiàn)監(jiān)控服務(wù) 支持停止接受新連接請(qǐng)求,而不影響現(xiàn)有連接 可以在雙向添加,修改或刪除HTTP報(bào)文首部 響應(yīng)報(bào)文壓縮 支持基于pattern實(shí)現(xiàn)連接請(qǐng)求的訪問(wèn)控制 通過(guò)特定的URI為授權(quán)用戶提供詳細(xì)的狀態(tài)信息 支持http反向代理 支持動(dòng)態(tài)程序的反向代理 支持基于數(shù)據(jù)庫(kù)的反向代理
不具備的功能:
正向代理--squid,nginx 緩存代理--varnish web服務(wù)--nginx、tengine、apache、php、tomcat UDP--目前不支持UDP協(xié)議 單機(jī)性能--相比LVS性能較差
HAProxy 支持基于lua實(shí)現(xiàn)功能擴(kuò)展,lua是一種小巧的腳本語(yǔ)言,于1993年由巴西里約熱內(nèi)盧天主教大學(xué)(Pontifical Catholic University of Rio de Janeiro)里的一個(gè)研究小組開(kāi)發(fā),其設(shè)計(jì)目的是為了嵌入
應(yīng)用程序中,從而為應(yīng)用程序提供靈活的擴(kuò)展和定制功能。
范例:CentOS 7 安裝haproxy
[root@centos7 ~]# yum install haproxy -y
范例:CentOS 8 安裝haproxy
[root@centos8 ~]# dnf -y install haproxy
第三方安裝包
官方?jīng)]有提供rpm相關(guān)的包,可以通過(guò)第三方倉(cāng)庫(kù)的rpm包
從第三方網(wǎng)站下載rpm包:https://pkgs.org/download/haproxy
范例:
[root@centos8 ~]# wget http://www.nosuchhost.net/~cheese/fedora/packages/epel-7/x86_64/cheeserelease-7-1.noarch.rpm [root@centos8 ~]# rpm -ivh cheese-release-7-1.noarch.rpm [root@centos8 ~]# yum install haproxy
范例:利用第三方 yum 倉(cāng)庫(kù)安裝
[root@centos7 ~]# wget https://centos7.iuscommunity.org/ius-release.rpm [root@centos7 ~]# rpm -Uvh ius-release*rpm [root@centos7 ~]# yum -y install epel-release [root@centos7 ~]# rpm -Uvh ius-release*rpm [root@centos7 ~]# yum install haproxy
范例:下載rpm包離線安裝
#下載安裝lua庫(kù)對(duì)應(yīng)的版本 [root@centos7 ~]# wget https://dl.iuscommunity.org/pub/ius/stable/CentOS/7/x86_64/lua53u-libs-5.3.4-1.ius.centos7.x86_64.rpm #安裝lua庫(kù) [root@centos7 ~]# yum -y install lua53u-libs-5.3.4-1.ius.centos7.x86_64.rpm #下載haproxy [root@centos7 ~]# wget https://dl.iuscommunity.org/pub/ius/stable/CentOS/7/x86_64/haproxy18u-1.8.20- 1.el7.ius.x86_64.rpm #安裝haproxy [root@centos7 ~]# yum -y install haproxy18u-1.8.20-1.el7.ius.x86_64.rpm
編譯安裝HAproxy
# 依賴包 [root@centos8 ~]# yum install gcc readline-devel 下載lua包 [root@centos8 ~]# wget http://www.lua.org/ftp/lua-5.3.5.tar.gz [root@centos8 ~]# tar xf lua-5.3.5.tar.gz -C /usr/local/src/ [root@centos8 ~]# cd /usr/local/src/lua-5.3.5/ [root@centos8 ~]# make linux test # 查看lua安裝的版本 [root@centos8 ~]# /usr/local/src/lua-5.3.5/src/lua -v # 依賴包 [root@centos8 ~]# yum -y install gcc openssl-devel pcre-devel systemd-devel readline-devel [root@centos8 ~]# tar xf haproxy-2.1.3.tar.gz -C /usr/local/src/ [root@centos8 ~]# cd /usr/local/src/haproxy-2.1.3/ [root@centos8 ~]# make ARCH=x86_64 TARGET=linux-glibc USE_PCRE=1 USE_OPENSSL=1 USE_ZLIB=1 USE_SYSTEMD=1 USE_LUA=1 LUA_INC=/usr/local/src/lua-5.3.5/src/ LUA_LIB=/usr/local/src/lua-5.3.5/src/ [root@centos8 ~]# make install PREFIX=/apps/haproxy [root@centos8 ~]# ln -s /apps/haproxy/sbin/haproxy /usr/sbin/ # 查看haproxy安裝的版本 [root@centos8 ~]# haproxy -v
啟動(dòng)文件
[root@centos8 ~]# vim /usr/lib/systemd/system/haproxy.service [Unit] Description=HAProxy Load Balancer After=syslog.target network.target [Service] ExecStartPre=/usr/sbin/haproxy -f /etc/haproxy/haproxy.cfg -c -q ExecStart=/usr/sbin/haproxy -Ws -f /etc/haproxy/haproxy.cfg -p /var/lib/haproxy/haproxy.pid ExecReload=/bin/kill -USR2 $MAINPID LimitNOFILE=100000 [Install] WantedBy=multi-user.target
重新加載
[root@centos8 ~]# systemctl daemon-reload [root@centos8 ~]# systemctl start haproxy # 創(chuàng)建兩個(gè)文件夾 [root@centos8 ~]# mkdir /etc/haproxy [root@centos8 ~]# mkdir /var/lib/haproxy
配置文件
[root@centos8 ~]# vim /etc/haproxy/haproxy.cfg
global
maxconn 100000
chroot /apps/haproxy
stats socket /var/lib/haproxy/haproxy.sock mode 600 level admin
uid 99
gid 99
user haproxy
group haproxy
daemon
#nbproc 4
#cpu-map 1 0
#cpu-map 2 1
#cpu-map 3 2
#cpu-map 4 3
pidfile /var/lib/haproxy/haproxy.pid
log 127.0.0.1 local2 info
defaults
option http-keep-alive
option forwardfor
maxconn 100000
mode http
timeout connect 300000ms
timeout client 300000ms
timeout server 300000ms
listen stats
mode http
bind 0.0.0.0:9999
stats enable
log global
stats uri /status
stats auth haadmin:123456
listen web_port
bind 172.31.0.28:80
mode http
log global
#server web1 127.0.0.1:8080 check inter 3000 fall 2 rise 5
server 172.31.0.17 172.31.0.17:80 check inter 3000 fall 2 rise 5 # 這是后端的web服務(wù)器,必須要有
server 172.31.0.27 172.31.0.27:80 check inter 3000 fall 2 rise 5 # 這是后端的web服務(wù)器,必須要有
haproxy.cfg文件中定義了chroot、pidfile、user、group等參數(shù),如果系統(tǒng)沒(méi)有相應(yīng)的資源會(huì)導(dǎo)致
haproxy無(wú)法啟動(dòng),具體參考日志文件 /var/log/messages
[root@centos8 ~]# journalctl -xe Jun 22 05:08:24 centos8.longxuan.vip haproxy[10959]: [ALERT] 172/050824 (10959) : Cannot open configuration file/directory /etc/haproxy/haproxy.cfg : No such file or direct Jun 22 05:08:24 centos8.longxuan.vip systemd[1]: haproxy.service: Control process exited, code=exited status=1 Jun 22 05:08:24 centos8.longxuan.vip systemd[1]: haproxy.service: Failed with result 'exit-code'. Jun 22 05:12:13 centos8.longxuan.vip systemd[1]: Starting HAProxy Load Balancer... Jun 22 05:12:13 centos8.longxuan.vip haproxy[10974]: [ALERT] 172/051213 (10974) : parsing [/etc/haproxy/haproxy.cfg:7] : cannot find user id for 'haproxy' (0:Success) Jun 22 05:12:13 centos8.longxuan.vip haproxy[10974]: [ALERT] 172/051213 (10974) : parsing [/etc/haproxy/haproxy.cfg:8] : cannot find group id for 'haproxy' (0:Success) Jun 22 05:12:13 centos8.longxuan.vip haproxy[10974]: [ALERT] 172/051213 (10974) : Error(s) found in configuration file : /etc/haproxy/haproxy.cfg Jun 22 05:12:13 centos8.longxuan.vip haproxy[10974]: [ALERT] 172/051213 (10974) : Fatal errors found in configuration.
查看haproxy的狀態(tài)頁(yè)面
瀏覽器訪問(wèn): http://haproxy-server:9999/status
基礎(chǔ)配置詳解
官方文檔:
http://cbonte.github.io/haproxy-dconv/ # 帶版本的(2.1版本) http://cbonte.github.io/haproxy-dconv/2.1/configuration.html
HAProxy 的配置文件haproxy.cfg由兩大部分組成,分別是global和proxies部分
global:全局配置段
進(jìn)程及安全配置相關(guān)的參數(shù) 性能調(diào)整相關(guān)參數(shù) Debug參數(shù)
proxies:代理配置段
defaults:為frontend, backend, listen提供默認(rèn)配置 frontend:前端,相當(dāng)于nginx中的server {} backend:后端,相當(dāng)于nginx中的upstream {} listen:同時(shí)擁有前端和后端配置,配置簡(jiǎn)單,生產(chǎn)推薦使用
global 配置參數(shù)說(shuō)明
官方文檔:http://cbonte.github.io/haproxy-dconv/2.4/configuration.html#3 chroot #鎖定運(yùn)行目錄 deamon #以守護(hù)進(jìn)程運(yùn)行 stats socket /var/lib/haproxy/haproxy.sock mode 600 level admin process 1 #socket文件 user, group, uid, gid #運(yùn)行haproxy的用戶身份 nbproc n #開(kāi)啟的haproxy worker 進(jìn)程數(shù),默認(rèn)進(jìn)程數(shù)是一個(gè) #nbthread 1 #和多進(jìn)程 nbproc配置互斥(版本有關(guān),CentOS8的haproxy1.8無(wú)此問(wèn)題),指定每 個(gè)haproxy進(jìn)程開(kāi)啟的線程數(shù),默認(rèn)為每個(gè)進(jìn)程一個(gè)線程 #如果同時(shí)啟用nbproc和nbthread 會(huì)出現(xiàn)以下日志的錯(cuò)誤,無(wú)法啟動(dòng)服務(wù) Apr 7 14:46:23 haproxy haproxy: [ALERT] 097/144623 (1454) : config : cannot enable multiple processes if multiple threads are configured. Please use either nbproc or nbthread but not both. cpu-map 1 0 #綁定haproxy worker 進(jìn)程至指定CPU,將第1個(gè)work進(jìn)程綁定至0號(hào)CPU cpu-map 2 1 #綁定haproxy worker 進(jìn)程至指定CPU,將第2個(gè)work進(jìn)程綁定至1號(hào)CPU maxconn n #每個(gè)haproxy進(jìn)程的最大并發(fā)連接數(shù) maxsslconn n #每個(gè)haproxy進(jìn)程ssl最大連接數(shù),用于haproxy配置了證書(shū)的場(chǎng)景下 maxconnrate n #每個(gè)進(jìn)程每秒創(chuàng)建的最大連接數(shù)量 spread-checks n #后端server狀態(tài)check隨機(jī)提前或延遲百分比時(shí)間,建議2-5(20%-50%)之間,默認(rèn) 值0 pidfile #指定pid文件路徑 log 127.0.0.1 local2 info #定義全局的syslog服務(wù)器;日志服務(wù)器需要開(kāi)啟UDP協(xié)議,最多可以定 義兩個(gè)
多進(jìn)程和線程
范例:多進(jìn)程和socket文件
[root@centos7 ~]# vim /etc/haproxy/haproxy.cfg
global
maxconn 100000
chroot /apps/haproxy
stats socket /var/lib/haproxy/haproxy.sock1 mode 600 level admin process 1
stats socket /var/lib/haproxy/haproxy.sock2 mode 600 level admin process 2
uid 99
gid 99
daemon
nbproc 2
[root@centos7 ~]# systemctl restart haproxy
[root@centos7 ~]# pstree -p |grep haproxy
|-haproxy(2688)-+-haproxy(2690)
| `-haproxy(2691)
[root@centos7 ~]# ll /var/lib/haproxy/
total 4
-rw-r--r-- 1 root root 5 Mar 31 10:49 haproxy.pid
srw------- 1 root root 0 Mar 31 10:49 haproxy.sock1
srw------- 1 root root 0 Mar 31 10:49 haproxy.sock2
HAProxy日志配置
HAproxy本身不記錄客戶端的訪問(wèn)日志.此外為減少服務(wù)器負(fù)載,一般生產(chǎn)中HAProxy不記錄日志.
也可以配置HAProxy利用rsyslog服務(wù)記錄日志到指定日志文件中
HAProxy配置
[root@centos7 ~]# vim /etc/haproxy/haproxy.cfg
#在global配置項(xiàng)定義:
log 127.0.0.1 local{1-7} info #基于syslog記錄日志到指定設(shè)備,級(jí)別有(err、warning、
info、debug)
listen web_port
bind 127.0.0.1:80
mode http
log global #開(kāi)啟當(dāng)前web_port的日志功能,默認(rèn)不記錄日
志
server web1 127.0.0.1:8080 check inter 3000 fall 2 rise 5
[root@centos7 ~]# systemctl restart haproxy
Rsyslog配置(CentOS7版本的寫(xiě)法)生產(chǎn)不推薦haproxy記錄日志,這樣會(huì)加大haproxy負(fù)載
[root@centos7 ~]# vim /etc/rsyslog.conf $ModLoad imudp $UDPServerRun 514 ...... local3.* /var/log/haproxy.log ...... [root@centos7 ~]# systemctl restart rsyslog
重啟syslog服務(wù)并訪問(wèn)app頁(yè)面,然后驗(yàn)證是否生成日志
[root@centos8 ~]# tail -f /var/log/haproxy.log Jun 22 19:01:09 localhost haproxy[11406]: Connect from 172.31.0.29:50626 to 172.31.0.28:80 (web_port/HTTP)
Proxies配置
官方文檔:http://cbonte.github.io/haproxy-dconv/2.1/configuration.html#4 defaults [] #默認(rèn)配置項(xiàng),針對(duì)以下的frontend、backend和listen生效,可以多個(gè)name也可 以沒(méi)有name frontend #前端servername,類似于Nginx的一個(gè)虛擬主機(jī) server和LVS服務(wù)集群。 backend #后端服務(wù)器組,等于nginx的upstream和LVS中的RS服務(wù)器 listen #將frontend和backend合并在一起配置,相對(duì)于frontend和backend配置更簡(jiǎn) 潔,生產(chǎn)常用
注意:name字段只能使用大小寫(xiě)字母,數(shù)字,‘-’(dash),'_‘(underscore),'.' (dot)和 ':'(colon),并且嚴(yán)格區(qū)分大小寫(xiě)
Proxies配置-defaults
option redispatch #當(dāng)server Id對(duì)應(yīng)的服務(wù)器掛掉后,強(qiáng)制定向到其他健康的服務(wù)器,重新派 發(fā) option abortonclose #當(dāng)服務(wù)器負(fù)載很高時(shí),自動(dòng)結(jié)束掉當(dāng)前隊(duì)列處理比較久的連接,針對(duì)業(yè)務(wù)情 況選擇開(kāi)啟 option http-keep-alive #開(kāi)啟與客戶端的會(huì)話保持 option forwardfor #透?jìng)骺蛻舳苏鎸?shí)IP至后端web服務(wù)器 mode http|tcp #設(shè)置默認(rèn)工作類型,使用TCP服務(wù)器性能更好,減少壓力 timeout http-keep-alive 120s #session 會(huì)話保持超時(shí)時(shí)間,此時(shí)間段內(nèi)會(huì)轉(zhuǎn)發(fā)到相同的后端服務(wù) 器 timeout connect 120s #客戶端請(qǐng)求從haproxy到后端server最長(zhǎng)連接等待時(shí)間(TCP連接之前), 默認(rèn)單位ms timeout server 600s #客戶端請(qǐng)求從haproxy到后端服務(wù)端的請(qǐng)求處理超時(shí)時(shí)長(zhǎng)(TCP連接之后), 默認(rèn)單位ms,如果超時(shí),會(huì)出現(xiàn)502錯(cuò)誤,此值建議設(shè)置較大些,訪止502錯(cuò)誤 timeout client 600s #設(shè)置haproxy與客戶端的最長(zhǎng)非活動(dòng)時(shí)間,默認(rèn)單位ms,建議和timeout server相同 timeout check 5s #對(duì)后端服務(wù)器的默認(rèn)檢測(cè)超時(shí)時(shí)間 default-server inter 1000 weight 3 #指定后端服務(wù)器的默認(rèn)設(shè)置
Proxies配置-listen 簡(jiǎn)化配置
使用listen替換 frontend和backend的配置方式,可以簡(jiǎn)化設(shè)置,通常只用于TCP協(xié)議的應(yīng)用
#官網(wǎng)業(yè)務(wù)訪問(wèn)入口
listen WEB_PORT_80
bind 10.0.0.7:80
mode http
option forwardfor
server web1 172.31.0.17:8080 check inter 3000 fall 3 rise 5
server web2 172.31.0.27:8080 check inter 3000 fall 3 rise 5
Proxies配置-frontend
bind: #指定HAProxy的監(jiān)聽(tīng)地址,可以是IPV4或IPV6,可以同時(shí)監(jiān)聽(tīng)多個(gè)IP或端口,可同時(shí)用于 listen字段中 #格式: bind []:[, ...] [param*] #注意:如果需要綁定在非本機(jī)的IP,需要開(kāi)啟內(nèi)核參數(shù):net.ipv4.ip_nonlocal_bind=1 backlog #針對(duì)所有server配置,當(dāng)前端服務(wù)器的連接數(shù)達(dá)到上限后的后援隊(duì)列長(zhǎng)度,注意:不支持backend
范例:
listen http_proxy #監(jiān)聽(tīng)http的多個(gè)IP的多個(gè)端口和sock文件
bind :80,:443,:8801-8810
bind 171.31.0.1:10080,172.31.0.1:10443
bind /var/run/ssl-frontend.sock user root mode 600 accept-proxy
listen http_https_proxy #https監(jiān)聽(tīng)
bind :80
bind :443 ssl crt /etc/haproxy/site.pem #公鑰和私鑰公共文件
listen http_https_proxy_explicit #監(jiān)聽(tīng)ipv6、ipv4和unix sock文件
bind ipv6@:80
bind ipv4@public_ssl:443 ssl crt /etc/haproxy/site.pem
bind unix@ssl-frontend.sock user root mode 600 accept-proxy
listen external_bind_app1 #監(jiān)聽(tīng)file descriptor
bind "fd@${FD_APP1}"
生產(chǎn)示例:
frontend magedu_web_port #可以采用后面形式命名:業(yè)務(wù)-服務(wù)-端口號(hào)
bind :80,:8080
bind 172.31.0.7:10080,:8801-8810,172.31.0.17:9001-9010
mode http|tcp #指定負(fù)載協(xié)議類型
use_backend #調(diào)用的后端服務(wù)器組名稱
Proxies配置-backend
定義一組后端服務(wù)器,backend服務(wù)器將被frontend進(jìn)行調(diào)用。
注意: backend 的名稱必須唯一,并且必須在listen或frontend中事先定義才可以使用,否則服務(wù)無(wú)法啟動(dòng)
mode http|tcp #指定負(fù)載協(xié)議類型,和對(duì)應(yīng)的frontend必須一致 option #配置選項(xiàng) server #定義后端real server,必須指定IP和端口
注意:option后面加 httpchk,smtpchk,mysql-check,pgsql-check,ssl-hello-chk方法,可用于實(shí)現(xiàn)更多應(yīng)用層檢測(cè)功能。
server 配置
#針對(duì)一個(gè)server配置 check #對(duì)指定real進(jìn)行健康狀態(tài)檢查,如果不加此設(shè)置,默認(rèn)不開(kāi)啟檢查,只有check后面沒(méi)有其它配置也可以啟用檢查功能 #默認(rèn)對(duì)相應(yīng)的后端服務(wù)器IP和端口,利用TCP連接進(jìn)行周期性健康性檢查,注意必須指定端口才能實(shí)現(xiàn)健康性檢查 addr#可指定的健康狀態(tài)監(jiān)測(cè)IP,可以是專門(mén)的數(shù)據(jù)網(wǎng)段,減少業(yè)務(wù)網(wǎng)絡(luò)的流量 port #指定的健康狀態(tài)監(jiān)測(cè)端口 inter #健康狀態(tài)檢查間隔時(shí)間,默認(rèn)2000 ms fall #后端服務(wù)器從線上轉(zhuǎn)為線下的檢查的連續(xù)失效次數(shù),默認(rèn)為3 rise #后端服務(wù)器從下線恢復(fù)上線的檢查的連續(xù)有效次數(shù),默認(rèn)為2 weight #默認(rèn)為1,最大值為256,0(狀態(tài)為藍(lán)色)表示不參與負(fù)載均衡,但仍接受持久連 接 backup #將后端服務(wù)器標(biāo)記為備份狀態(tài),只在所有非備份主機(jī)down機(jī)時(shí)提供服務(wù),類似 Sorry Server disabled #將后端服務(wù)器標(biāo)記為不可用狀態(tài),即維護(hù)狀態(tài),除了持久模式,將不再接受連接,狀態(tài)為深黃色,優(yōu)雅下線,不再接受新用戶的請(qǐng)求 redirect prefix http://www.baidu.com/ #將請(qǐng)求臨時(shí)(302)重定向至其它URL,只適用于http模式 redir http://www.baidu.com #將請(qǐng)求臨時(shí)(302)重定向至其它URL,只適用于http模式 maxconn #當(dāng)前后端server的最大并發(fā)連接數(shù)
frontend+backend 配置實(shí)例
frontend longxuan-test-http
bind :80,:8080
mode tcp
use_backend longxuan-test-http-nodes
backend longxuan-test-http-nodes
mode tcp
default-server inter 1000 weight 6
server web1 172.31.0.17:80 weight 2 check addr 172.31.0.117 port 8080
server web1 172.31.0.27:80 check
范例2:
#官網(wǎng)業(yè)務(wù)訪問(wèn)入口
frontend WEB_PORT_80
bind 10.0.0.7:80
mode http
use_backend web_prot_http_nodes
backend web_prot_http_nodes
mode http
option forwardfor
server 10.0.0.17 10.0.0.17:8080 check inter 3000 fall 3 rise 5
server 10.0.0.27 10.0.0.27:8080 check inter 3000 fall 3 rise 5
使用子配置文件保存配置
當(dāng)業(yè)務(wù)眾多時(shí),將所有配置都放在一個(gè)配置文件中,會(huì)造成維護(hù)困難??梢钥紤]按業(yè)務(wù)分類,將配置信
息拆分,放在不同的子配置文件中,從而達(dá)到方便維護(hù)的目的。
注意: 子配置文件的文件后綴必須為.cfg
#創(chuàng)建子配置目錄
[root@centos7 ~]# mkdir /etc/haproxy/conf.d/
#創(chuàng)建子配置文件,注意:必須為cfg后綴非.開(kāi)頭的配置文件
[root@centos7 ~]# vim /etc/haproxy/conf.d/test.cfg
listen WEB_PORT_80
bind 172.31.0.7:80
mode http
balance roundrobin
server web1 172.31.0.17:80 check inter 3000 fall 2 rise 5
server web2 172.31.0.27:80 check inter 3000 fall 2 rise 5
#添加子配置目錄到unit文件中
[root@centos7 ~]# vim /lib/systemd/system/haproxy.service
[Unit]
Description=HAProxy Load Balancer
After=syslog.target network.target
[Service]
#修改下面兩行
ExecStartPre=/usr/sbin/haproxy -f /etc/haproxy/haproxy.cfg -f /etc/haproxy/conf.d/ -c -q
ExecStart=/usr/sbin/haproxy -Ws -f /etc/haproxy/haproxy.cfg -f /etc/haproxy/conf.d/ -p /var/lib/haproxy/haproxy.pid
ExecReload=/bin/kill -USR2 $MAINPID
[Install]
WantedBy=multi-user.target
#重新加載并啟動(dòng)
[root@centos7 ~]# systemctl daemon-reload
[root@centos7 ~]# systemctl restart haproxy
鏈接:https://www.cnblogs.com/xuanlv-0413/p/15037087.html
-
Web
+關(guān)注
關(guān)注
2文章
1302瀏覽量
73755 -
服務(wù)器
+關(guān)注
關(guān)注
13文章
10117瀏覽量
91037 -
負(fù)載均衡
+關(guān)注
關(guān)注
0文章
128瀏覽量
12822
原文標(biāo)題:HAProxy深度解析:打造高效反向代理和負(fù)載均衡的利器
文章出處:【微信號(hào):magedu-Linux,微信公眾號(hào):馬哥Linux運(yùn)維】歡迎添加關(guān)注!文章轉(zhuǎn)載請(qǐng)注明出處。
發(fā)布評(píng)論請(qǐng)先 登錄
負(fù)載均衡器的誕生和工作機(jī)制
基于labview的音樂(lè)均衡器的設(shè)計(jì)
如何提高自適應(yīng)均衡器的性能?
頻率均衡器的調(diào)控技巧
均衡器的使用誤區(qū)
什么是均衡器
均衡器的工作原理及設(shè)計(jì)分析
電腦均衡器軟件哪個(gè)好_電腦均衡器怎么設(shè)置才好聽(tīng)
全面剖析HAProxy 負(fù)載均衡器
了解圖形均衡器與參數(shù)均衡器的區(qū)別
FilterBank均衡器插件介紹

Haproxy負(fù)載均衡器深度解析
評(píng)論