【轉】Haproxy安裝及配置

1.安裝php

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

# tar zcvf haproxy-1.3.20.tar.gzpython

# cd haproxy-1.3.20linux

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

# make install PREFIX=/usr/local/haproxyredis

2.配置算法

安裝完畢後,進入安裝目錄配置文件,默認狀況下目錄裏是沒有.cfg配置文件的,能夠回到安裝文件目錄下將examples下的haproxy.cfg拷貝到usr/local/haproxy下。vim

# 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

 

6.舉例

在瀏覽器中輸入haproxy監控地址:http://10.16.0.246:1080/haproxy-admin,如圖所示

 

在瀏覽器輸入haproxy設置的對外訪問地址:http://10.16.0.244/,如圖所示

 

 

 

參考文獻:

http://network.51cto.com/art/201110/295955_1.htm

http://blog.csdn.net/maijian/article/details/6082283

http://blog.sina.com.cn/s/blog_51d3553f0100u1bb.html

http://andrewyu.blog.51cto.com/1604432/647907

http://blog.chinaunix.net/space.php?uid=22600159&do=blog&id=2124188

http://sookk8.blog.51cto.com/455855/579719

http://freehat.blog.51cto.com/1239536/1347882

http://www.cnblogs.com/dkblog/archive/2012/03/13/2393321.html

http://jlsfwq.blog.51cto.com/818700/165617

 

做者URL: http://johnsz.blog.51cto.com/525379/715922/

相關文章
相關標籤/搜索