歡迎您光臨本站 註冊首頁

Haproxy安裝及配置

←手機掃碼閱讀     火星人 @ 2014-03-04 , reply:0

1.安裝

# wget http://haproxy.1wt.eu/download/1.3/src/haproxy-1.3.20.tar.gz

# tar zcvf haproxy-1.3.20.tar.gz

# cd haproxy-1.3.20

# make TARGET=linux26 PREFIX=/usr/local/haproxy                                #將haproxy安裝到/usr/local/haproxy

# make install PREFIX=/usr/local/haproxy

2.配置

安裝完畢后,進入安裝目錄配置文件,默認情況下目錄里是沒有.cfg配置文件的,可以回到安裝文件目錄下將examples下的haproxy.cfg拷貝到usr/local/haproxy下。

# cd /usr/local/haproxy

# vi haproxy.cfg

 默認文件內容如下:
 # this config needs haproxy-1.1.28 or haproxy-1.2.1
 global
log 127.0.0.1 local0
log 127.0.0.1 local1 notice
#log loghost local0 info
maxconn 4096
chroot /usr/share/haproxy
uid 99
gid 99
daemon
#debug
#quiet
defaults
log global
mode http
option httplog
option dontlognull
retries 3
redispatch
maxconn 2000
contimeout 5000
clitimeout 50000
srvtimeout 50000
listen appli1-rewrite 0.0.0.0:10001
cookie SERVERID rewrite
balance roundrobin
server app1_1 192.168.34.23:8080 cookie app1inst1 check inter 2000 rise 2  fall 5
server app1_2 192.168.34.32:8080 cookie app1inst2 check inter 2000 rise 2  fall 5
server app1_3 192.168.34.27:8080 cookie app1inst3 check inter 2000 rise 2  fall 5
server app1_4 192.168.34.42:8080 cookie app1inst4 check inter 2000 rise 2  fall 5
listen appli2-insert 0.0.0.0:10002
option httpchk
balance roundrobin
cookie SERVERID insert indirect nocache
server inst1 192.168.114.56:80 cookie server01 check inter 2000 fall 3
server inst2 192.168.114.56:81 cookie server02 check inter 2000 fall 3
capture cookie vgnvisitor= len 32
option httpclose # disable keep-alive
rspidel ^Set-cookie:\ IP= # do not let this cookie tell our internal IP address
listen appli3-relais 0.0.0.0:10003
dispatch 192.168.135.17:80
listen appli4-backup 0.0.0.0:10004
option httpchk /index.html
option persist
balance roundrobin
server inst1 192.168.114.56:80 check inter 2000 fall 3
server inst2 192.168.114.56:81 check inter 2000 fall 3 backup
listen ssl-relay 0.0.0.0:8443
option ssl-hello-chk
balance source
server inst1 192.168.110.56:443 check inter 2000 fall 3
server inst2 192.168.110.57:443 check inter 2000 fall 3
server back1 192.168.120.58:443 backup
listen appli5-backup 0.0.0.0:10005
option httpchk *
balance roundrobin
cookie SERVERID insert indirect nocache
server inst1 192.168.114.56:80 cookie server01 check inter 2000 fall 3
server inst2 192.168.114.56:81 cookie server02 check inter 2000 fall 3
server inst3 192.168.114.57:80 backup check inter 2000 fall 3
capture cookie ASPSESSION len 32
srvtimeout 20000
option httpclose # disable keep-alive
option checkcache # block response if set-cookie & cacheable
rspidel ^Set-cookie:\ IP= # do not let this cookie tell our internal IP address
errorloc 502 http://192.168.114.58/error502.html
errorfile 503 /etc/haproxy/errors/503.http

根據實際需求,更改配置文件,我的配置如下

global           #全局設置

       log 127.0.0.1   local0      #日誌輸出配置,所有日誌都記錄在本機,通過local0輸出

       #log loghost    local0 info

       maxconn 4096             #最大連接數

       chroot /usr/local/haproxy

       uid 99                   #所屬運行的用戶uid

       gid 99                   #所屬運行的用戶組

       daemon                   #以後台形式運行haproxy

       nbproc 2                 #啟動2個haproxy實例

       pidfile /usr/local/haproxy/haproxy.pid  #將所有進程寫入pid文件

       #debug

       #quiet

defaults             #默認設置

       #log    global

       log     127.0.0.1       local3         #日誌文件的輸出定向

       mode    http         #所處理的類別,默認採用http模式,可配置成tcp作4層消息轉發

       option  httplog       #日誌類別,採用httplog

       option  dontlognull  

       option  forwardfor   #如果後端伺服器需要獲得客戶端真實ip需要配置的參數,可以從Http Header中獲得客戶端ip

       option  httpclose    #每次請求完畢後主動關閉http通道,haproxy不支持keep-alive,只能模擬這種模式的實現

       retries 3           #3次連接失敗就認為伺服器不可用,主要通過後面的check檢查

       option  redispatch   #當serverid對應的伺服器掛掉后,強制定向到其他健康伺服器

       maxconn 2000                     #最大連接數

stats   uri     /haproxy-admin  #haproxy 監控頁面的訪問地址

       contimeout      5000            #連接超時時間

       clitimeout      50000           #客戶端連接超時時間

       srvtimeout      50000           #伺服器端連接超時時間

stats auth  Frank:Frank   #設置監控頁面的用戶和密碼:Frank


stats hide-version         #隱藏統計頁面的HAproxy版本信息



frontend http-in                        #前台

       bind *:80

       mode    http

       option  httplog

       log     global

       default_backend htmpool       #靜態伺服器池

backend htmpool                    #後台

       balance leastconn#負載均衡演算法

       option  httpchk HEAD /index.htm HTTP/1.0       #健康檢查

       server  web1 10.16.0.9:8085 cookie 1 weight 5 check inter 2000 rise 2 fall 3

       server  web2 10.16.0.10:8085 cookie 2 weight 3 check inter 2000 rise 2 fall 3

#cookie 1表示serverid為1,check inter 1500 是檢測心跳頻率

 #rise 2是2次正確認為伺服器可用,fall 3是3次失敗認為伺服器不可用,weight代表權重

3.加上日誌支持

# vim /etc/syslog.conf

在最下邊增加
local3.*         /var/log/haproxy.log
local0.*         /var/log/haproxy.log

#vim /etc/sysconfig/syslog

修改: SYSLOGD_OPTIONS="-r -m 0"

重啟日誌服務service syslog restart

4.設置開機啟動

為了方便系統在開機時載入,還可以創建啟動腳本:
# 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.conf
PIDFILE=$PROGDIR/$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"
       haproxy_pid=cat $PIDFILE
       kill $haproxy_pid
       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

5.啟動服務

啟動服務:
# /usr/local/haproxy/sbin/haproxy -f /usr/local/haproxy/haproxy.cfg

重啟服務:
# /usr/local/haproxy/sbin/haproxy -f /usr/local/haproxy/haproxy.cfg -st `cat /usr/local/haproxy/logs/haproxy.pid`  (沒有換行)

停止服務:
# killall haproxy


[火星人 ] Haproxy安裝及配置已經有515次圍觀

http://coctec.com/docs/service/show-post-4307.html