企业级高可用Web架构之HAProxy+Keepalived

细数下来,算是东莞的项目的话,HAProxy+Keepalived我差不多也有三套在线上跑了,另外,这套Web方案也是我的一拍网的备份方案之一,目前也在测试,如果速度和稳定性够强劲的话,我也考虑将LVS+Keepalived换成HAProxy+Keepalived,关于HAProxy的语法和安装步骤请参考我的专题系列文章http://network.51cto.com/art/201101/241997.htm,另外,此篇文章跟刘天斯的不一样,我主要用其作为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.  
  12. defaults  
  13.          log     127.0.0.1       local3  
  14.          mode   http  
  15.          option httplog  
  16.          option httpclose  
  17.          option dontlognull  
  18.          option forwardfor  
  19.          option redispatch  
  20.          retries 2  
  21.          maxconn 2000  
  22.          balance source  
  23.          stats   uri     /web-status  
  24.          contimeout      5000  
  25.          clitimeout      50000  
  26.          srvtimeout      50000  
  27.  
  28. listen  www.1paituan.com 
  29.         bind *:80 
  30.         mode http 
  31.         option httplog 
  32.         log global 
  33.         option httpchk HEAD /index.jsp HTTP/1.0 
  34.         server web1  203.93.236.147:80 weight 5  check inter 2000 rise 2 fall 3 
  35.         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.  
  5. Source function library. 
  6. if [ -f /etc/init.d/functions ]; then 
  7.   . /etc/init.d/functions 
  8. elif [ -f /etc/rc.d/init.d/functions ] ; then 
  9.   . /etc/rc.d/init.d/functions 
  10. else 
  11.   exit 0 
  12. fi 
  13.  
  14. Source networking configuration. 
  15. . /etc/sysconfig/network 
  16.  
  17. # Check that networking is up. 
  18. [ ${NETWORKING} = "no" ] && exit 0 
  19.  
  20. [ -f /usr/local/haproxy/conf/haproxy.cfg ] || exit 1 
  21.  
  22. RETVAL=0 
  23.  
  24. start() { 
  25.   /usr/local/haproxy/sbin/haproxy -c -q -f /usr/local/haproxy/conf/haproxy.cfg 
  26.   if [ $? -ne 0 ]; then 
  27.     echo "Errors found in configuration file." 
  28.     return 1 
  29.   fi 
  30.  
  31.   echo -n "Starting HAproxy: " 
  32.   daemon /usr/local/haproxy/sbin/haproxy -D -f /usr/local/haproxy/conf/haproxy.cfg -p /var/run/haproxy.pid 
  33.   RETVAL=$? 
  34.   echo 
  35.   [ $RETVAL -eq 0 ] && touch /var/lock/subsys/haproxy 
  36.   return $RETVAL 
  37.  
  38. stop() { 
  39.   echo -n "Shutting down HAproxy: " 
  40.   killproc haproxy -USR1 
  41.   RETVAL=$? 
  42.   echo 
  43.   [ $RETVAL -eq 0 ] && rm -f /var/lock/subsys/haproxy 
  44.   [ $RETVAL -eq 0 ] && rm -f /var/run/haproxy.pid 
  45.   return $RETVAL 
  46.  
  47. restart() { 
  48.   /usr/local/haproxy/sbin/haproxy -c -q -f /usr/local/haproxy/conf/haproxy.cfg 
  49.   if [ $? -ne 0 ]; then 
  50.     echo "Errors found in configuration file, check it with 'haproxy check'." 
  51.     return 1 
  52.   fi 
  53.   stop 
  54.   start 
  55.  
  56. check() { 
  57.   /usr/local/haproxy/sbin/haproxy -c -q -V -f /usr/local/haproxy/conf/haproxy.cfg 
  58.  
  59. rhstatus() { 
  60.   status haproxy 
  61.  
  62. condrestart() { 
  63.   [ -e /var/lock/subsys/haproxy ] && restart || : 
  64.  
  65. # See how we were called. 
  66. case "$1" in 
  67.   start) 
  68.     start 
  69.     ;; 
  70.   stop) 
  71.     stop 
  72.     ;; 
  73.   restart) 
  74.     restart 
  75.     ;; 
  76.   reload) 
  77.     restart 
  78.     ;; 
  79.   condrestart) 
  80.     condrestart 
  81.     ;; 
  82.   status) 
  83.     rhstatus 
  84.     ;; 
  85.   check) 
  86.     check 
  87.     ;; 
  88.   *) 
  89.     echo $"Usage: haproxy {start|stop|restart|reload|condrestart|status|check}" 
  90.     RETVAL=1 
  91. esac 
  92.  
  93. exit $RETVAL 

三、HAProxy的监控脚本我没有做,这个实施起来也简单,我们可以用curl -s --head http://www.1paituan.com/index.jsp | 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的并发监控和日志收集分析是下一步考虑的事情。

你可能感兴趣的:(职场,休闲)