设为首页 收藏本站
查看: 1471|回复: 0

[经验分享] HAProxy安装使用

[复制链接]
累计签到:1 天
连续签到:1 天
发表于 2015-2-10 07:22:33 | 显示全部楼层 |阅读模式
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 httpchk  GET /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                          

frontend  game_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
    cookie  SERVERID insert indirect
#    http-check disable-on-404
#    http-check send-state
#    option httpchk  GET /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

    server  xxx-taiwan-game1  192.168.1.183:80 cookie xxx-taiwan-game1_80 check maxconn 2000
    server  xxx-taiwan-game2  192.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 and  HTTP-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
Feb  9 17:43:53 127.0.0.1 haproxy[25770]: backend game_pool has no server available!
Feb  9 17:44:02 127.0.0.1 haproxy[25810]: Proxy game_pool started.
Feb  9 17:44:02 127.0.0.1 haproxy[25810]: Proxy game_pool started.
Feb  9 17:44:02 127.0.0.1 haproxy[25810]: Proxy monitor1 started.
Feb  9 17:44:03 127.0.0.1 haproxy[25828]: Proxy game_pool started.
Feb  9 17:44:03 127.0.0.1 haproxy[25828]: Proxy game_pool started.
Feb  9 17:44:03 127.0.0.1 haproxy[25828]: Proxy monitor1 started.
Feb  9 17:44:05 127.0.0.1 haproxy[25829]: 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.
Feb  9 17:44:05 127.0.0.1 haproxy[25829]: 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.
Feb  9 17:44:05 127.0.0.1 haproxy[25829]: backend game_pool has no server available!




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


运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-42292-1-1.html 上篇帖子: Hapoxy+keepalived实现双主高可用负载均衡 下篇帖子: HAProxy匹配泛解析域名排错经历
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表