ewe22 发表于 2015-2-10 07:22:33

HAProxy安装使用

1.下载并安装软件包
yum -y install make gcc pcre*
mkdir -p /data/app_platform/haproxy/conf/
useradd -r haproxy -s /sbin/nologin
wget http://www.haproxy.org/download/1.4/src/haproxy-1.4.26.tar.gz
tar zxvf haproxy-1.4.26.tar.gz
cd haproxy-1.4.26
make TARGET=linux26 PREFIX=/data/app_platform/haproxy/
make install PREFIX=/data/app_platform/haproxy/

2.添加配置文件
/data/app_platform/haproxy/conf/haproxy.cfg

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
global
    chroot      /data/app_platform/haproxy/share/
    log         127.0.0.1 local3 info
    daemon      
    user      haproxy
    group       haproxy
    pidfile   /var/run/haproxy.pid
    nbproc      1                     
    stats socket/tmp/haproxy level admin
    stats maxconn 20                  
    node      master_loadbalance1
    description xxxx
    maxconn   65536
    nosplice                           
    spread-checks 3                     

defaults
    option httpchkGET /php-fpm_status
    log         global                  
    mode http
    option httpclose
    option abortonclose         
    no option accept-invalid-http-response
    option allbackups         
    #option http-server-close                  
    #option httplog                     
    #option log-health-checks            
    #option tcp-smart-connect            
    option tcpka                        
    option redispatch
    option forwardfor except 127.0.0.1
    retries 3   
    timeout check 60s                  
    timeout connect 600s               
    timeout http-keep-alive 600s         
    timeout http-request 600s            
    timeout queue 600s                  
    timeout server 600s                  
    timeout tarpit 60s
    timeout client 600s                        

frontendgame_pool 0.0.0.0:80
    mode http
    no option accept-invalid-http-request
    option http-server-close
    log         global
    option log-separate-errors
    #option forwardfor except 127.0.0.1 header X-Real-IP
    option forwardfor except 127.0.0.1
    #   acl path_web_inf   path_beg       -i    /WEB-INF
    # block if path_web_inf
   acl game_url      hdr_reg(host) -i   ^(s\d*.xxxx.com|s\d*.xxx.com|s\d*.xxx.com|s\d*.xxx.com)$

backend game_pool
    mode http
    balance   roundrobin
    cookieSERVERID insert indirect
#    http-check disable-on-404
#    http-check send-state
#    option httpchkGET /check.php HTTP/1.1\r\nHost:\ example.com
    default-server inter 2s fastinter 1s downinter 5s slowstart 60s rise 2 fall 5 weight 30

    serverxxx-taiwan-game1192.168.1.183:80 cookie xxx-taiwan-game1_80 check maxconn 2000
    serverxxx-taiwan-game2192.168.1.184:80 cookie xxx-taiwan-game2_80 check maxconn 2000



listen monitor1 0.0.0.0:10443
    mode http
    balance   roundrobin
    stats enable
    stats uri /nc-haproxy            # - default is haproxy?stats
    stats realm Haproxy\ statistics    # - Enable statistics and set authentication realm
    stats show-desc example gintama-taiwan-lb1 status page
    stats auth ncadmin:taiwan
    stats refresh 30s
    stats show-legends               # - Enable reporting additional informations on the statistics page
    timeout check 10s
    timeout client 45s
    timeout connect 45s
    timeout http-keep-alive 45s
    timeout http-request 45s
    timeout queue 45s
    timeout server 45s
    timeout tarpit 45s





3.添加启动脚本

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
#!/bin/sh
#
# haproxy
#
# chkconfig:   - 85 15
# description:HAProxy is a free, very fast and reliable solution \
#               offering high availability, load balancing, and \
#               proxying for TCP andHTTP-based applications
# processname: haproxy
# config:      /data/app_platform/haproxy/conf/haproxy.cfg
# pidfile:   /var/run/haproxy.pid

# Source function library.
. /etc/rc.d/init.d/functions

# Source networking configuration.
. /etc/sysconfig/network

# Check that networking is up.
[ "$NETWORKING" = "no" ] && exit 0

exec="/data/app_platform/haproxy/sbin/haproxy"
prog=$(basename $exec)
conf_file="/data/app_platform/haproxy/conf/haproxy.cfg"

[ -e /etc/sysconfig/$prog ] && . /etc/sysconfig/$prog

lockfile=/var/lock/subsys/haproxy

check() {
    $exec -c -V -f $conf_file
}

start() {
    $exec -c -q -f $conf_file
    if [ $? -ne 0 ]; then
      echo "Errors in configuration file, check with $prog check."
      return 1
    fi

    echo -n $"Starting $prog: "
    # start it up here, usually something like "daemon $exec"
    daemon $exec -D -f $conf_file -p /var/run/$prog.pid
    retval=$?
    echo
    [ $retval -eq 0 ] && touch $lockfile
    return $retval
}

stop() {
    echo -n $"Stopping $prog: "
    # stop it here, often "killproc $prog"
    killproc $prog
    retval=$?
    echo
    [ $retval -eq 0 ] && rm -f $lockfile
    return $retval
}

restart() {
    $exec -c -q -f $conf_file
    if [ $? -ne 0 ]; then
      echo "Errors in configuration file, check with $prog check."
      return 1
    fi
    stop
    start
}

reload() {
    $exec -c -q -f $conf_file
    if [ $? -ne 0 ]; then
      echo "Errors in configuration file, check with $prog check."
      return 1
    fi
    echo -n $"Reloading $prog: "
    $exec -D -f $conf_file -p /var/run/$prog.pid -sf $(cat /var/run/$prog.pid)
    retval=$?
    echo
    return $retval
}

force_reload() {
    restart
}

fdr_status() {
    status $prog
}

case "$1" in
    start|stop|restart|reload)
      $1
      ;;
    force-reload)
      force_reload
      ;;
    check)
      check
      ;;
    status)
      fdr_status
      ;;
    condrestart|try-restart)
      [ ! -f $lockfile ] || restart
    ;;
    *)
      echo $"Usage: $0 {start|stop|status|restart|try-restart|reload|force-reload}"
      exit 2
esac





4.添加日志输出
/etc/rsyslog.d/haproxy.conf

1
2
3
4
local3.* /var/log/haproxy.log
$ModLoad imudp
$UDPServerAddress 127.0.0.1
$UDPServerRun 514




service rsyslog restart

5.启动HAProxy
service haproxy start


1
2
3
4
5
6
7
8
9
10
11
tail -f /var/log/haproxy.log
Feb9 17:43:53 127.0.0.1 haproxy: backend game_pool has no server available!
Feb9 17:44:02 127.0.0.1 haproxy: Proxy game_pool started.
Feb9 17:44:02 127.0.0.1 haproxy: Proxy game_pool started.
Feb9 17:44:02 127.0.0.1 haproxy: Proxy monitor1 started.
Feb9 17:44:03 127.0.0.1 haproxy: Proxy game_pool started.
Feb9 17:44:03 127.0.0.1 haproxy: Proxy game_pool started.
Feb9 17:44:03 127.0.0.1 haproxy: Proxy monitor1 started.
Feb9 17:44:05 127.0.0.1 haproxy: Server game_pool/xxx-taiwan-game1 is DOWN, reason: Layer4 timeout, check duration: 2001ms. 1 active and 0 backup servers left. 0 sessions active, 0 requeued, 0 remaining in queue.
Feb9 17:44:05 127.0.0.1 haproxy: Server game_pool/xxx-taiwan-game2 is DOWN, reason: Layer4 timeout, check duration: 2001ms. 0 active and 0 backup servers left. 0 sessions active, 0 requeued, 0 remaining in queue.
Feb9 17:44:05 127.0.0.1 haproxy: backend game_pool has no server available!





6.设置HAProxy开机启动
chkconfig --level 35 haproxy on

页: [1]
查看完整版本: HAProxy安装使用