目前有兩台伺服器,上面運行了Apache,讓兩台伺服器依覆載選擇伺服器;前面就要加上一台Haproxy,分配給兩台伺服器
假設
- A架設Apache 192.168.1.1
- B架設Apache 192.168.1.2
- C架設Haproxy 192.168.1.3
輸入下方指令安裝Haproxy
sudo apt-get install -y haproxy
切換到haproxy修改配置檔
cd /etc/haproxy/ sudo vim haproxy.cfg
配置檔如下:
global
log /dev/log local0
log /dev/log local1 notice
chroot /var/lib/haproxy
user haproxy
group haproxy
daemon
defaults
log global
mode http
option httplog
option dontlognull
contimeout 5000
clitimeout 50000
srvtimeout 50000
errorfile 400 /etc/haproxy/errors/400.http
errorfile 403 /etc/haproxy/errors/403.http
errorfile 408 /etc/haproxy/errors/408.http
errorfile 500 /etc/haproxy/errors/500.http
errorfile 502 /etc/haproxy/errors/502.http
errorfile 503 /etc/haproxy/errors/503.http
errorfile 504 /etc/haproxy/errors/504.http
#監聽 別名 IP:Port
listen web 0.0.0.0:80
mode http
stats enable
stats uri /ha-status
stats hide-version
stats refresh 30s
balance roundrobin
option httpclose
option forwardfor
#Server 別名 IP:Port 權重 檢查項目
server web1 192.168.1.1:80 weight 1 check inter 3000 rise 2 fall 3
server web1 192.168.1.2:80 weight 1 check inter 3000 rise 2 fall 3
將Haproxy啟動,啟動時沒出現錯誤就是正常
sudo haproxy -f /etc/haproxy/haproxy.cfg
可以透過『192.168.1.3/ha-status』查看流量
瀏覽192.168.1.3/index.html,可以看得到是連接到A
在瀏覽一次,可以看得到切到B
如果要關閉Haproxy可以輸入
sudo killall haproxy or sudo kill pid
從該流量可得知Haproxy確實可以分配工作給底下的機器
謝謝Y.C.和球哥指教XD
參考資料:
伺服器負載平衡 ( Server Load Balancing )
http://haproxy.1wt.eu/
https://www.digitalocean.com/community/articles/how-to-use-haproxy-to-set-up-http-load-balancing-on-an-ubuntu-vps
http://heylinux.com/archives/1752.html
[FreeBSD & Linux]網站分流:簡易架設 HAProxy 伺服器
http://blog.longwin.com.tw/2009/03/haproxy-ha-load-balance-2009/
http://www.iyism.com/blog/post/216/
http://showerlee.blog.51cto.com/2047005/1201144
http://blog.wu-boy.com/2014/01/percona-xtradb-cluster-reference-architecture-with-haproxy/
http://blog.yam.com/keynes0918/article/63269343



