1. 安裝 haproxy
# cd /usr/ports/net/haproxy
# make install clean
2. 安裝完後不會自動產生預設的設定檔,所以要自己建立
# vi /usr/local/etc/haproxy.conf
global
daemon
maxconn 4096
defaults
log global
mode http
timeout connect 5s
timeout client 10s
timeout server 10s
#以mysql 為例
listen mydb
bind 0.0.0.0:3306
mode tcp
balance roundrobin
server db11 192.168.2.11:3306 check
server db11 192.168.2.12:3306 check
# web 管理介面,可以透過這個網址檢視backend 狀態
listen webinterface
bind 0.0.0.0:8080
mode http
stats enable
stats uri /
stats realm Strictly\ Private
stats auth admin:12345 #這邊分別是登入的帳號:密碼
3. 設定重開機時,自動啟用 haproxy
# /usr/local/etc/rc.d/haproxy enable
4. 第一次手動啟動 haproxy
# /usr/local/etc/rc.d/haproxy onestart