歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux基礎 >> 關於Linux >> 負載均衡工具haproxy安裝,配置,使用基礎教程

負載均衡工具haproxy安裝,配置,使用基礎教程

日期:2017/3/2 9:57:32   编辑:關於Linux

一,什麼是haproxy
HAProxy提供高可用性、負載均衡以及基於TCP和HTTP應用的代 理,支持虛擬主機,它是免費、快速並且可靠的一種解決方案。HAProxy特別適用於那些負載特大的web站點,這些站點通常又需要會話保持或七層處理。HAProxy運行在當前的硬件上,完全可以支持數以萬計的並發連接。並且它的運行模式使得它可以很簡單安全的整合進您當前的架構中, 同時可以保護你的web服務器不被暴露到網絡上。

其支持從4層至7層的網絡交換,即覆蓋所有的TCP協議。就是說,Haproxy 甚至還支持 Mysql 的均衡負載。。
如果說在功能上,能以proxy反向代理方式實現 WEB均衡負載,這樣的產品有很多。包括 Nginx,ApacheProxy,lighttpd,Cheroke 等。
但要明確一點的,Haproxy 並不是 Http 服務器。以上提到所有帶反向代理均衡負載的產品,都清一色是 WEB 服務器。簡單說,就是他們能自個兒提供靜態(html,jpg,gif..)或動態(php,cgi..)文件的傳輸以及處理。而Haproxy 僅僅,而且專門是一款的用於均衡負載的應用代理。其自身並不能提供http服務。



二,安裝haproxy
下載列表地址http://haproxy.1wt.eu

#wget http://haproxy.1wt.eu/download/1.4/src/haproxy-1.4.8.tar.gz
#tar zxvf haproxy-1.4.8.tar.gz
#cd haproxy-1.4.8
#uname -a //查看linux內核版本
#make TARGET=linux26 PREFIX=/usr/local/haproxy
#make install PREFIX=/usr/local/haproxy

三,配置haproxy


#vi /usr/local/haproxy/haproxy.cfg

修改配置文件為


global
maxconn 5120
chroot /usr/local/haproxy
uid 99
gid 99
daemon
quiet
nbproc 2
pidfile /usr/local/haproxy/haproxy.pid
defaults
log global
mode http
option httplog
option dontlognull
log 127.0.0.1 local3
retries 3
option redispatch
maxconn 2000
contimeout 5000
clitimeout 50000
srvtimeout 50000

listen webinfo :1080
mode http
balance roundrobin
option httpclose
option forwardfor
server phpinfo1 192.168.18.2:10000 check weight 1 minconn 1 maxconn 3 check inter 40000
server phpinfo2 127.0.0.1:80 check weight 1 minconn 1 maxconn 3 check inter 40000

listen webmb :1081
mode http
balance roundrobin
option httpclose
option forwardfor
server webmb1 192.168.1.91:10000 weight 1 minconn 1 maxconn 3 check inter 40000
server webmb2 127.0.0.1:10000 weight 1 minconn 1 maxconn 3 check inter 40000

listen stats :8888
mode http
transparent
stats uri / haproxy-stats
stats realm Haproxy \ statistic
stats auth admin:admin

四,啟動haproxy
#啟動haproxy


#/usr/local/haproxy/sbin/haproxy -f /usr/local/haproxy/haproxy.cfg

查看是否啟動


[zhangy@BlackGhost haproxy]$ ps -e|grep haproxy
25053 ? 00:00:00 haproxy
25054 ? 00:00:00 haproxy

五,測試

http://localhost:1080/phpinfo.php

六,監控

http://localhost:8888/stats

用戶名admin密碼admin,就是之前你在配置文件中設置的!

Copyright © Linux教程網 All Rights Reserved