負載均衡:Load Balance,簡稱LB,是一種服務或基于硬件設備等實現的高可用反向代理技術,負載均衡將特定的業務(web服務、網絡流量等)分擔給指定的一個或多個后端特定的服務器或設備,從而提高了公司業務的并發處理能力、保證了業務的高可用性、方便了業務后期的水平動態擴展。
為什么使用負載均衡
Web服務器的動態水平擴展-->對用戶無感知 增加業務并發訪問及處理能力-->解決單服務器瓶頸問題 節約公網IP地址-->降低IT支出成本 隱藏內部服務器IP-->提高內部服務器安全性 配置簡單-->固定格式的配置文件 功能豐富-->支持四層和七層,支持動態下線主機 性能較強-->并發數萬甚至數十萬
負載均衡類型
四層: 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/
應用場景
四層:Redis、Mysql、RabbitMQ、Memcache等 七層:Nginx、Tomcat、Apache、PHP、圖片、動靜分離、API等
HAProxy是法國開發者威利塔羅(Willy Tarreau) 在2000年使用C語言開發的一個開源軟件,是一款具
備高并發(一萬以上)、高性能的TCP和HTTP負載均衡器,支持基于cookie的持久性,自動故障切換,支
持正則表達式及web狀態統計,目前最新TLS版本為2.4
企業版網站:https://www.haproxy.com/ 社區版網站:http://www.haproxy.org/ github:https://github.com/haproxy
HAproxy支持功能:
TCP 和 HTTP反向代理 SSL/TSL服務器 可以針對HTTP請求添加cookie,進行路由后端服務器 可平衡負載至后端服務器,并支持持久連接 支持所有主服務器故障切換至備用服務器 支持專用端口實現監控服務 支持停止接受新連接請求,而不影響現有連接 可以在雙向添加,修改或刪除HTTP報文首部 響應報文壓縮 支持基于pattern實現連接請求的訪問控制 通過特定的URI為授權用戶提供詳細的狀態信息 支持http反向代理 支持動態程序的反向代理 支持基于數據庫的反向代理
不具備的功能:
正向代理--squid,nginx 緩存代理--varnish web服務--nginx、tengine、apache、php、tomcat UDP--目前不支持UDP協議 單機性能--相比LVS性能較差
HAProxy 支持基于lua實現功能擴展,lua是一種小巧的腳本語言,于1993年由巴西里約熱內盧天主教大學(Pontifical Catholic University of Rio de Janeiro)里的一個研究小組開發,其設計目的是為了嵌入
應用程序中,從而為應用程序提供靈活的擴展和定制功能。
范例:CentOS 7 安裝haproxy
[root@centos7 ~]# yum install haproxy -y
范例:CentOS 8 安裝haproxy
[root@centos8 ~]# dnf -y install haproxy
第三方安裝包
官方沒有提供rpm相關的包,可以通過第三方倉庫的rpm包
從第三方網站下載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 倉庫安裝
[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庫對應的版本 [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庫 [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
啟動文件
[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 # 創建兩個文件夾 [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服務器,必須要有 server 172.31.0.27 172.31.0.27:80 check inter 3000 fall 2 rise 5 # 這是后端的web服務器,必須要有
haproxy.cfg文件中定義了chroot、pidfile、user、group等參數,如果系統沒有相應的資源會導致
haproxy無法啟動,具體參考日志文件 /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的狀態頁面
瀏覽器訪問: http://haproxy-server:9999/status
基礎配置詳解
官方文檔:
http://cbonte.github.io/haproxy-dconv/ # 帶版本的(2.1版本) http://cbonte.github.io/haproxy-dconv/2.1/configuration.html
HAProxy 的配置文件haproxy.cfg由兩大部分組成,分別是global和proxies部分
global:全局配置段
進程及安全配置相關的參數 性能調整相關參數 Debug參數
proxies:代理配置段
defaults:為frontend, backend, listen提供默認配置 frontend:前端,相當于nginx中的server {} backend:后端,相當于nginx中的upstream {} listen:同時擁有前端和后端配置,配置簡單,生產推薦使用
global 配置參數說明
官方文檔:http://cbonte.github.io/haproxy-dconv/2.4/configuration.html#3 chroot #鎖定運行目錄 deamon #以守護進程運行 stats socket /var/lib/haproxy/haproxy.sock mode 600 level admin process 1 #socket文件 user, group, uid, gid #運行haproxy的用戶身份 nbproc n #開啟的haproxy worker 進程數,默認進程數是一個 #nbthread 1 #和多進程 nbproc配置互斥(版本有關,CentOS8的haproxy1.8無此問題),指定每 個haproxy進程開啟的線程數,默認為每個進程一個線程 #如果同時啟用nbproc和nbthread 會出現以下日志的錯誤,無法啟動服務 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 進程至指定CPU,將第1個work進程綁定至0號CPU cpu-map 2 1 #綁定haproxy worker 進程至指定CPU,將第2個work進程綁定至1號CPU maxconn n #每個haproxy進程的最大并發連接數 maxsslconn n #每個haproxy進程ssl最大連接數,用于haproxy配置了證書的場景下 maxconnrate n #每個進程每秒創建的最大連接數量 spread-checks n #后端server狀態check隨機提前或延遲百分比時間,建議2-5(20%-50%)之間,默認 值0 pidfile #指定pid文件路徑 log 127.0.0.1 local2 info #定義全局的syslog服務器;日志服務器需要開啟UDP協議,最多可以定 義兩個
多進程和線程
范例:多進程和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本身不記錄客戶端的訪問日志.此外為減少服務器負載,一般生產中HAProxy不記錄日志.
也可以配置HAProxy利用rsyslog服務記錄日志到指定日志文件中
HAProxy配置
[root@centos7 ~]# vim /etc/haproxy/haproxy.cfg #在global配置項定義: log 127.0.0.1 local{1-7} info #基于syslog記錄日志到指定設備,級別有(err、warning、 info、debug) listen web_port bind 127.0.0.1:80 mode http log global #開啟當前web_port的日志功能,默認不記錄日 志 server web1 127.0.0.1:8080 check inter 3000 fall 2 rise 5 [root@centos7 ~]# systemctl restart haproxy
Rsyslog配置(CentOS7版本的寫法)生產不推薦haproxy記錄日志,這樣會加大haproxy負載
[root@centos7 ~]# vim /etc/rsyslog.conf $ModLoad imudp $UDPServerRun 514 ...... local3.* /var/log/haproxy.log ...... [root@centos7 ~]# systemctl restart rsyslog
重啟syslog服務并訪問app頁面,然后驗證是否生成日志
[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 [] #默認配置項,針對以下的frontend、backend和listen生效,可以多個name也可 以沒有name frontend #前端servername,類似于Nginx的一個虛擬主機 server和LVS服務集群。 backend #后端服務器組,等于nginx的upstream和LVS中的RS服務器 listen #將frontend和backend合并在一起配置,相對于frontend和backend配置更簡 潔,生產常用
注意:name字段只能使用大小寫字母,數字,‘-’(dash),'_‘(underscore),'.' (dot)和 ':'(colon),并且嚴格區分大小寫
Proxies配置-defaults
option redispatch #當server Id對應的服務器掛掉后,強制定向到其他健康的服務器,重新派 發 option abortonclose #當服務器負載很高時,自動結束掉當前隊列處理比較久的連接,針對業務情 況選擇開啟 option http-keep-alive #開啟與客戶端的會話保持 option forwardfor #透傳客戶端真實IP至后端web服務器 mode http|tcp #設置默認工作類型,使用TCP服務器性能更好,減少壓力 timeout http-keep-alive 120s #session 會話保持超時時間,此時間段內會轉發到相同的后端服務 器 timeout connect 120s #客戶端請求從haproxy到后端server最長連接等待時間(TCP連接之前), 默認單位ms timeout server 600s #客戶端請求從haproxy到后端服務端的請求處理超時時長(TCP連接之后), 默認單位ms,如果超時,會出現502錯誤,此值建議設置較大些,訪止502錯誤 timeout client 600s #設置haproxy與客戶端的最長非活動時間,默認單位ms,建議和timeout server相同 timeout check 5s #對后端服務器的默認檢測超時時間 default-server inter 1000 weight 3 #指定后端服務器的默認設置
Proxies配置-listen 簡化配置
使用listen替換 frontend和backend的配置方式,可以簡化設置,通常只用于TCP協議的應用
#官網業務訪問入口 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的監聽地址,可以是IPV4或IPV6,可以同時監聽多個IP或端口,可同時用于 listen字段中 #格式: bind []:[, ...] [param*] #注意:如果需要綁定在非本機的IP,需要開啟內核參數:net.ipv4.ip_nonlocal_bind=1 backlog #針對所有server配置,當前端服務器的連接數達到上限后的后援隊列長度,注意:不支持backend
范例:
listen http_proxy #監聽http的多個IP的多個端口和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監聽 bind :80 bind :443 ssl crt /etc/haproxy/site.pem #公鑰和私鑰公共文件 listen http_https_proxy_explicit #監聽ipv6、ipv4和unix sock文件 bind ipv6@:80 bind ipv4@public_ssl:443 ssl crt /etc/haproxy/site.pem bind [email protected] user root mode 600 accept-proxy listen external_bind_app1 #監聽file descriptor bind "fd@${FD_APP1}"
生產示例:
frontend magedu_web_port #可以采用后面形式命名:業務-服務-端口號 bind :80,:8080 bind 172.31.0.7:10080,:8801-8810,172.31.0.17:9001-9010 mode http|tcp #指定負載協議類型 use_backend#調用的后端服務器組名稱
Proxies配置-backend
定義一組后端服務器,backend服務器將被frontend進行調用。
注意: backend 的名稱必須唯一,并且必須在listen或frontend中事先定義才可以使用,否則服務無法啟動
mode http|tcp #指定負載協議類型,和對應的frontend必須一致 option #配置選項 server #定義后端real server,必須指定IP和端口
注意:option后面加 httpchk,smtpchk,mysql-check,pgsql-check,ssl-hello-chk方法,可用于實現更多應用層檢測功能。
server 配置
#針對一個server配置 check #對指定real進行健康狀態檢查,如果不加此設置,默認不開啟檢查,只有check后面沒有其它配置也可以啟用檢查功能 #默認對相應的后端服務器IP和端口,利用TCP連接進行周期性健康性檢查,注意必須指定端口才能實現健康性檢查 addr#可指定的健康狀態監測IP,可以是專門的數據網段,減少業務網絡的流量 port #指定的健康狀態監測端口 inter #健康狀態檢查間隔時間,默認2000 ms fall #后端服務器從線上轉為線下的檢查的連續失效次數,默認為3 rise #后端服務器從下線恢復上線的檢查的連續有效次數,默認為2 weight #默認為1,最大值為256,0(狀態為藍色)表示不參與負載均衡,但仍接受持久連 接 backup #將后端服務器標記為備份狀態,只在所有非備份主機down機時提供服務,類似 Sorry Server disabled #將后端服務器標記為不可用狀態,即維護狀態,除了持久模式,將不再接受連接,狀態為深黃色,優雅下線,不再接受新用戶的請求 redirect prefix http://www.baidu.com/ #將請求臨時(302)重定向至其它URL,只適用于http模式 redir http://www.baidu.com #將請求臨時(302)重定向至其它URL,只適用于http模式 maxconn #當前后端server的最大并發連接數
frontend+backend 配置實例
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:
#官網業務訪問入口 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
使用子配置文件保存配置
當業務眾多時,將所有配置都放在一個配置文件中,會造成維護困難。可以考慮按業務分類,將配置信
息拆分,放在不同的子配置文件中,從而達到方便維護的目的。
注意: 子配置文件的文件后綴必須為.cfg
#創建子配置目錄 [root@centos7 ~]# mkdir /etc/haproxy/conf.d/ #創建子配置文件,注意:必須為cfg后綴非.開頭的配置文件 [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 #重新加載并啟動 [root@centos7 ~]# systemctl daemon-reload [root@centos7 ~]# systemctl restart haproxy
鏈接:https://www.cnblogs.com/xuanlv-0413/p/15037087.html
-
Web
+關注
關注
2文章
1281瀏覽量
70789 -
服務器
+關注
關注
12文章
9681瀏覽量
87260 -
負載均衡
+關注
關注
0文章
118瀏覽量
12530
原文標題:HAProxy深度解析:打造高效反向代理和負載均衡的利器
文章出處:【微信號:magedu-Linux,微信公眾號:馬哥Linux運維】歡迎添加關注!文章轉載請注明出處。
發布評論請先 登錄
頻率均衡器的調控技巧
功率均衡器,功率均衡器原理是什么?
均衡器的工作原理及設計分析

全面剖析HAProxy 負載均衡器

評論