Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1658711
  • 博文数量: 782
  • 博客积分: 2455
  • 博客等级: 大尉
  • 技术积分: 4140
  • 用 户 组: 普通用户
  • 注册时间: 2011-04-06 21:37
个人简介

Linux ,c/c++, web,前端,php,js

文章分类

全部博文(782)

文章存档

2015年(8)

2014年(28)

2013年(110)

2012年(307)

2011年(329)

分类:

2012-02-29 10:02:00

原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 、作者信息和本声明。否则将追究法律责任。

细数下来,算是东莞的项目的话,HAProxy+Keepalived我差不多也有三套在线上跑了,另外,这套Web方案也是我的一拍网的备份方案之一,目前也在测试,如果速度和稳定性够强劲的话,我也考虑将LVS+Keepalived换成HAProxy+Keepalived,关于HAProxy的语法和安装步骤请参考我的专题系列文章,另外,此篇文章跟刘天斯的不一样,我主要用其作为Web级别的负载均衡(七层应用)。
一、线上跑的HAProxy配置文件,代码如下:

  1. global 
  2.         log 127.0.0.1   local0 
  3.         maxconn 65535 
  4.         chroot /usr/local/haproxy 
  5.         uid 99    
  6.         gid 99 
  7.         daemon 
  8.         nbproc 8 
  9.         pidfile /usr/local/haproxy/haproxy.pid 
  10.         debug 
  11. defaults  
  12.          log     127.0.0.1       local3  
  13.          mode   http  
  14.          option httplog  
  15.          option httpclose  
  16.          option dontlognull  
  17.          option forwardfor  
  18.          option redispatch  
  19.          retries 2  
  20.          maxconn 2000  
  21.          balance source  
  22.          stats   uri     /web-status  
  23.          contimeout      5000  
  24.          clitimeout      50000  
  25.          srvtimeout      50000  
  26. listen  
  27.         bind *:80 
  28.         mode http 
  29.         option httplog 
  30.         log global 
  31.         option httpchk HEAD /index.jsp HTTP/1.0 
  32.         server web1  203.93.236.147:80 weight 5  check inter 2000 rise 2 fall 3 
  33.         server web2  203.93.236.146:80 weight 3  check inter 2000 rise 2 fall 3 

二、HAProxy的启动、关闭和重启脚本,代码如下:

  1. #!/bin/sh 
  2. # chkconfig 35 on 
  3. # description: HAProxy is a TCP/HTTP reverse proxy which is particularly suited for high availability environments. 
  4. # Source function library. 
  5. if [ -f /etc/init.d/functions ]; then 
  6.   . /etc/init.d/functions 
  7. elif [ -f /etc/rc.d/init.d/functions ] ; then 
  8.   . /etc/rc.d/init.d/functions 
  9. else 
  10.   exit 0 
  11. fi 
  12. # Source networking configuration. 
  13. . /etc/sysconfig/network 
  14. # Check that networking is up. 
  15. [ ${NETWORKING} = "no" ] && exit 0 
  16. [ -f /usr/local/haproxy/conf/haproxy.cfg ] || exit 1 
  17. RETVAL=0 
  18. start() { 
  19.   /usr/local/haproxy/sbin/haproxy -c -q -f /usr/local/haproxy/conf/haproxy.cfg 
  20.   if [ $? -ne 0 ]; then 
  21.     echo "Errors found in configuration file." 
  22.     return 1 
  23.   fi 
  24.   echo -n "Starting HAproxy: " 
  25.   daemon /usr/local/haproxy/sbin/haproxy -D -f /usr/local/haproxy/conf/haproxy.cfg -p /var/run/haproxy.pid 
  26.   RETVAL=$? 
  27.   echo 
  28.   [ $RETVAL -eq 0 ] && touch /var/lock/subsys/haproxy 
  29.   return $RETVAL 
  30. stop() { 
  31.   echo -n "Shutting down HAproxy: " 
  32.   killproc haproxy -USR1 
  33.   RETVAL=$? 
  34.   echo 
  35.   [ $RETVAL -eq 0 ] && rm -f /var/lock/subsys/haproxy 
  36.   [ $RETVAL -eq 0 ] && rm -f /var/run/haproxy.pid 
  37.   return $RETVAL 
  38. restart() { 
  39.   /usr/local/haproxy/sbin/haproxy -c -q -f /usr/local/haproxy/conf/haproxy.cfg 
  40.   if [ $? -ne 0 ]; then 
  41.     echo "Errors found in configuration file, check it with 'haproxy check'." 
  42.     return 1 
  43.   fi 
  44.   stop 
  45.   start 
  46. check() { 
  47.   /usr/local/haproxy/sbin/haproxy -c -q -V -f /usr/local/haproxy/conf/haproxy.cfg 
  48. rhstatus() { 
  49.   status haproxy 
  50. condrestart() { 
  51.   [ -e /var/lock/subsys/haproxy ] && restart || : 
  52. # See how we were called. 
  53. case "$1" in 
  54.   start) 
  55.     start 
  56.     ;; 
  57.   stop) 
  58.     stop 
  59.     ;; 
  60.   restart) 
  61.     restart 
  62.     ;; 
  63.   reload) 
  64.     restart 
  65.     ;; 
  66.   condrestart) 
  67.     condrestart 
  68.     ;; 
  69.   status) 
  70.     rhstatus 
  71.     ;; 
  72.   check) 
  73.     check 
  74.     ;; 
  75.   *) 
  76.     echo $"Usage: haproxy {start|stop|restart|reload|condrestart|status|check}" 
  77.     RETVAL=1 
  78. esac 
  79. exit $RETVAL 

三、HAProxy的监控脚本我没有做,这个实施起来也简单,我们可以用curl -s --head | awk '/HTTP/ {print $2}'的方法,判断是否返回了正常的200代码。 

四、加上日志支持,代码如下:

  1. #vim /etc/syslog.conf 
  2. 添加: 
  3. local3.*        /var/log/haproxy.log 
  4. local0.*        /var/log/haproxy.log 
  5. #vim /etc/sysconfig/syslog 
  6. 修改: 
  7. SYSLOGD_OPTIONS="-r -m 0" 
  8. 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的并发监控和日志收集分析是下一步考虑的事情。

阅读(723) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~