一、安裝:
1、從官網 http://haproxy.1wt.eu/#down 下載下傳最新版本,如 haproxy-1.4.16.tar.gz
2、解壓縮, # tar zcvf haproxy-1.4.16.tar.gz 建議移動到工作目錄 /usr/local/haproxy/ 下,否則後續安裝時還要用PREFIX=/usr/local/haprpxy指定安裝路徑
3、運作 make install 即完成安裝。
程式為 /usr/local/haproxy 或 /usr/local/sbin/haproxy, 運作程式 haproxy,顯示版本資訊即說明安裝成功
文檔在/usr/local/doc/haproxy下,Man:/usr/local/share/man/man1
二、配置
# 建立haproxy.cfg,并加入如下内容:
- global
- log 127.0.0.1 local0
- #log 127.0.0.1 local1 notice
- #log loghost local0 info
- maxconn 4096
- chroot /usr/local/haproxy
- uid 99
- gid 99
- daemon
- nbproc 1
- pidfile / usr/local/haproxy/logs/haproxy.pid
- #debug
- #quiet
- defaults
- log 127.0.0.1 local3
- mode http
- option httplog
- option httpclose
- option dontlognull
- option forwardfor
- option redispatch
- retries 2
- maxconn 2000
- balance roundrobin
- stats uri /haproxy-stats
- contimeout 5000
- clitimeout 50000
- srvtimeout 50000
- listen web_proxy 0.0.0.0:80
- option httpchk GET /ping.php
- server s1 192.168.0.101:80 weight 2 check
- server s2 192.168.0.102:80 weight 2 check
三、配置syslog,輸出haproxy日志
1、vi /etc/sysconfig/syslog
把SYSLOGD_OPTIONS="-m 0" 改成 SYSLOGD_OPTIONS="-r -m 0"
讓syslogd接受遠端的日志輸出
2、在/etc/ syslog.conf裡加一行
local3.* /var/log/haproxy.log
設定其權限
#touch /var/log/haproxy.log
# chmod 777 /var/log/haproxy.log
3、重新開機syslog
# killall -HUP syslogd 重新開機 syslogd
或者
#/etc/init.d/syslog restart
四、啟動HAProxy
/usr/local/sbin/haproxy –f /usr/local/haproxy/haproxy.cfg
檢視日志:tail –f /var/log/harpoxy.log
五、使用腳本來控制Haproxy 啟動 重新開機動 停止等操作。
- vim /etc/rc.d/init.d/haproxy
- #! /bin/sh
- set -e
- PATH=/sbin:/bin:/usr/sbin:/usr/bin:/usr/local/haproxy/sbin
- PROGDIR=/usr/local/haproxy
- PROGNAME=haproxy
- DAEMON=$PROGDIR/sbin/$PROGNAME
- CONFIG=$PROGDIR/$PROGNAME.cfg
- PIDFILE=$PROGDIR/logs/$PROGNAME.pid
- DESC="HAProxy daemon"
- SCRIPTNAME=/etc/init.d/$PROGNAME
- # Gracefully exit if the package has been removed.
- test -x $DAEMON || exit 0
- start()
- {
- echo -n "Starting $DESC: $PROGNAME"
- $DAEMON -f $CONFIG
- echo "."
- }
- stop()
- {
- echo -n "Stopping $DESC: $PROGNAME"
- killall haproxy
- echo "."
- }
- restart()
- {
- echo -n "Restarting $DESC: $PROGNAME"
- $DAEMON -f $CONFIG -p $PIDFILE -sf $(cat $PIDFILE)
- echo "."
- }
- case "$1" in
- start)
- start
- ;;
- stop)
- stop
- ;;
- restart)
- restart
- ;;
- *)
- echo "Usage: $SCRIPTNAME {start|stop|restart}" >&2
- exit 1
- ;;
- esac
- exit 0
儲存後賜予可執行權限
# chmod +x /etc/rc.d/init.d/haproxy
就可以使用 service haproxy start|stop|restart 來控制服務的啟動停止跟重新開機。
并通過以下指令加載到開機服務啟動清單
# chkconfig --add haproxy
echo “/usr/local/haproxy/sbin/haproxy -f /usr/local/haproxy/haproxy.cfg” >> /etc/rc.local
六、成功部署後,可以打開 stats 檢視HAProxy具體狀态:
http://192.168.0.100:1080/haproxy-stats,如:
至此,部署完畢。