haproxy的原理就類似於在伺服器的前端架設一個proxy server,一般作用不但可以擋住對後端實體server的攻擊,
另一個也可以兼做負載平衡與多台機器管理之用,
另外還有額外的監控功能,詳細設定如下
首先用yum安裝haproxy
yum install haproxy.x86_64
接著他的設定檔放在/etc/haproxy/haproxy.cfg
一般設定
global
maxconn 32768
user haproxy
group haproxy
daemon
nbproc 8
defaults
log global
option tcplog
option dontlognull
retries 3
option redispatch
maxconn 32768
contimeout 5000
clitimeout 50000
srvtimeout 50000
假設後端有兩台一樣的機器要進行負載平衡
listen 服務名稱 #容易識別即可
bind 192.168.5.111:8888
mode tcp (HTTP)
balance roundrobin
server nic SERVER_A_IP:80 weight 3 check
server mcu SERVER_B_IP:80 weight 3 check
監控介面設定
listen 服務名稱 #容易識別即可
bind 0.0.0.0:9999
mode http
stats uri /stats #監控haproxy狀態
balance roundrobin #負載均衡算法
cookie SERVERID insert indirect
option httpclose
option forwardfor #apache日誌轉發功能
stats auth admin:admin #監控頁面的用戶和密碼admin,可以設置多個用戶名
stats hide-version #隱藏版本訊息
#option httpchk HEAD /index.html HTTP/1.0 #網頁的健康檢查,檢查是否可連線index.html
server name SERVER_A_IP:SERVER_A_PORT check inter 1500 rise 3 fall 3 weight 2
server name SERVER_B_IP:SERVER_B_PORT check inter 1500 rise 3 fall 3 weight 2
check inter 1500表示1.5秒檢測一次,rise 3 fall 3表示成功(失敗)三次代表(不)可用,weight代表權重
最後記得開啟防火牆,之後連線到http://192.168.5.111:8888就會自動轉連線到後端的SERVER_A與SERVER_B,
另一個也可以兼做負載平衡與多台機器管理之用,
另外還有額外的監控功能,詳細設定如下
首先用yum安裝haproxy
yum install haproxy.x86_64
接著他的設定檔放在/etc/haproxy/haproxy.cfg
一般設定
global
maxconn 32768
user haproxy
group haproxy
daemon
nbproc 8
defaults
log global
option tcplog
option dontlognull
retries 3
option redispatch
maxconn 32768
contimeout 5000
clitimeout 50000
srvtimeout 50000
假設後端有兩台一樣的機器要進行負載平衡
listen 服務名稱 #容易識別即可
bind 192.168.5.111:8888
mode tcp (HTTP)
balance roundrobin
server nic SERVER_A_IP:80 weight 3 check
server mcu SERVER_B_IP:80 weight 3 check
監控介面設定
listen 服務名稱 #容易識別即可
bind 0.0.0.0:9999
mode http
stats uri /stats #監控haproxy狀態
balance roundrobin #負載均衡算法
cookie SERVERID insert indirect
option httpclose
option forwardfor #apache日誌轉發功能
stats auth admin:admin #監控頁面的用戶和密碼admin,可以設置多個用戶名
stats hide-version #隱藏版本訊息
#option httpchk HEAD /index.html HTTP/1.0 #網頁的健康檢查,檢查是否可連線index.html
server name SERVER_A_IP:SERVER_A_PORT check inter 1500 rise 3 fall 3 weight 2
server name SERVER_B_IP:SERVER_B_PORT check inter 1500 rise 3 fall 3 weight 2
check inter 1500表示1.5秒檢測一次,rise 3 fall 3表示成功(失敗)三次代表(不)可用,weight代表權重
最後記得開啟防火牆,之後連線到http://192.168.5.111:8888就會自動轉連線到後端的SERVER_A與SERVER_B,