| 关键词: nbsp HAProxy local RETVAL check restart usr option http echo |
细数下来,算是东莞的项目的话,HAProxy+Keepalived我差不多也有三套在线上跑了,另外,这套Web方案也是我的一拍网的备份方案之一,目前也在测试,如果速度和稳定性够强劲的话,我也考虑将LVS+Keepalived换成HAProxy+Keepalived,关于HAProxy的语法和安装步骤请参考我的专题系列文章http://network.51cto.com/art/201101/241997.htm,另外,此篇文章跟刘天斯的不一样,我主要用其作为Web级别的负载均衡(七层应用)。一、线上跑的HAProxy配置文件,代码如下:global log 127.0.0.1 local0 maxconn 65535 chroot /usr/local/haproxy uid 99 gid 99 daemon nbproc 8 pidfile /usr/local/haproxy/haproxy.pid debug defaults log 127.0.0.1 local3 mode http option httplog option httpclose option dontlognull option forwardfor option redispatch retries 2 maxconn 2000 balance source stats uri /web-status contimeout 5000 clitimeout 50000 srvtimeout 50000 listen www.1paituan.com bind *:80 mode http option httplog log global option httpchk HEAD /index.jsp HTTP/1.0 server web1 203.93.236.147:80 weight 5 check inter 2000 rise 2 fall 3 server web2 203.93.236.146:80 weight 3 check inter 2000 rise 2 fall 3 二、HAProxy的启动、关闭和重启脚本,代码如下:#!/bin/sh # chkconfig 35 on # description: HAProxy is a TCP/HTTP reverse proxy which is particularly suited for high availability environments. # Source function library. if [ -f /etc/init.d/functions ]; then . /etc/init.d/functions elif [ -f /etc/rc.d/init.d/functions ] ; then . /etc/rc.d/init.d/functions else exit 0 fi # Source networking configuration. . /etc/sysconfig/network # Check that networking is up. [ ${NETWORKING} = "no" ] && exit 0 [ -f /usr/local/haproxy/conf/haproxy.cfg ] || exit 1 RETVAL=0 start() { /usr/local/haproxy/sbin/haproxy -c -q -f /usr/local/haproxy/conf/haproxy.cfg if [ $? -ne 0 ]; then echo "Errors found in configuration file." return 1 fi echo -n "Starting HAproxy: " daemon /usr/local/haproxy/sbin/haproxy -D -f /usr/local/haproxy/conf/haproxy.cfg -p /var/run/haproxy.pid RETVAL=$? echo [ $RETVAL -eq 0 ] && touch /var/lock/subsys/haproxy return $RETVAL } stop() { echo -n "Shutting down HAproxy: " killproc haproxy -USR1 RETVAL=$? echo [ $RETVAL -eq 0 ] && rm -f /var/lock/subsys/haproxy [ $RETVAL -eq 0 ] && rm -f /var/run/haproxy.pid return $RETVAL } restart() { /usr/local/haproxy/sbin/haproxy -c -q -f /usr/local/haproxy/conf/haproxy.cfg if [ $? -ne 0 ]; then echo "Errors found in configuration file, check it with 'haproxy check'." return 1 fi stop start } check() { /usr/local/haproxy/sbin/haproxy -c -q -V -f /usr/local/haproxy/conf/haproxy.cfg } rhstatus() { status haproxy } condrestart() { [ -e /var/lock/subsys/haproxy ] && restart || : } # See how we were called. case "$1" in start) start ;; stop) stop ;; restart) restart ;; reload) restart ;; condrestart) condrestart ;; status) rhstatus ;; check) check ;; *) echo $"Usage: haproxy {start|stop|restart|reload|condrestart|status|check}" RETVAL=1 esac exit $RETVAL 三、HAProxy的监控脚本我没有做,这个实施起来也简单,我们可以用curl -s --head http://www.1paituan.com/index.jsp | awk '/HTTP/ {print $2}'的方法,判断是否返回了正常的200代码。 四、加上日志支持,代码如下:#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 五、大家需要注意的几个地方是:1)HAProyx采用的是balance source机制,它跟Nginx的ip_hash机制原理类似,是让客户机访问时始终访问后端的某一台真实的web服务器,这样让session就固定下来了;2)option httpchk HEAD /index.jsp HTTP/1.0 是网页监控,如果HAProxy检测不到Web的根目录下没有index.jsp,就会产生503报错。3)有网友说HAProxy必须采用listen 203.93.236.141:80这样类似的格式,这样其实不好,做集群时会导致从机启动不了,我们可以用bind *:80的方式代替。4)HAProxy的并发监控和日志收集分析是下一步考虑的事情。 本文出自 “抚琴煮酒” 博客,请务必保留此出处http://andrewyu.blog.51cto.com/1604432/669491 |
|
声明:文章版权归原作者所有 部分文章转自互联网 如有侵权请联系
[邮箱地址] 删除
|