web1以及web2輸入以下指令安裝Apache
yum install -y httpd
LB的Server則輸入以下指令安裝HaProxy
yum install -y haproxy
web1以及web2安裝好時輸入以下指令並讓Apache服務再開機時自動執行
hostname > /var/www/html/index.html chkconfig httpd on service httpd start
LB5這邊輸入以下指令編輯配置檔,配置檔如下
vi /etc/haproxy/haproxy.cfg
global
log 127.0.0.1 local2
chroot /var/lib/haproxy
pidfile /var/run/haproxy.pid
maxconn 4000
user haproxy
group haproxy
daemon
defaults
mode http
log global
option httplog
timeout http-request 10s
timeout queue 1m
timeout connect 10s
timeout client 1m
timeout server 1m
timeout http-keep-alive 10s
timeout check 10s
maxconn 3000
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 web1 WEB1 IP:80 weight 1 check
server web2 WEB2 IP:80 weight 1 check
目前透過瀏覽器可以看到Web1和Web2的畫面切換
也可以透過LB IP/ha-status看到目前狀態
當web1關機時,web1狀態會改變為黃色
當web1已經關機,web1狀態會改變為紅色
目前只會看到web2的網頁
再將web1重啟時,web1狀態會改變為黃色
正常運作時,web1狀態會恢復到綠色
再切換時就可以看到web1的畫面了
參考資料:
http://cyfangnotepad.blogspot.tw/2014/03/ubuntu-1204-lts-haproxyload-balancing.html


