haproxy部署笔记

jerry linux 2015年11月23日 收藏

1、HAProxy安装及日志配置

  1. # vim install-haproxy.sh
  2. #! /bin/bash
  3.  
  4. dir=/usr/local
  5. ha_dir=${dir}/haproxy
  6.  
  7. #install
  8. if [ ! -e "$ha_dir" ]; then
  9. #下载源码到/tmp/haproxy-1.4.22.tar.gz
  10. wget -nc -P /tmp http://haproxy.1wt.eu/download/1.4/src/haproxy-1.4.22.tar.gz
  11. tar -zxvf /tmp/haproxy-1.4.22.tar.gz -C /tmp
  12. cd /tmp/haproxy-1.4.22
  13.  
  14. #编译、安装
  15. make TARGET=linux26 PREFIX=/usr/local/haproxy
  16. make install PREFIX=/usr/local/haproxy
  17. cd /usr/local/haproxy
  18. #
  19. if [ ! -e "$ha_dir" ]; then
  20. echo "error! can't install haproxy please check! Will now out of the scrip !"
  21. exit 1
  22. else
  23. #添加启动用户组
  24. mkdir -p /var/haproxy
  25. /usr/sbin/groupaddhaproxy
  26. /usr/sbin/useradd -g haproxy -d /var/haproxy -s /sbin/nologinhaproxy
  27. #创建日志目录
  28. mkdir -p /data/haproxy/logs
  29. chown -R haproxy:haproxy /data/haproxy/
  30. chmod 775 -R /data/haproxy
  31. ! grep 'haproxy' /etc/syslog.conf&& echo ' local0.* /data/haproxy/logs/haproxy.log'>> /etc/syslog.conf
  32. sed -ir 's/SYSLOGD_OPTIONS="-m 0"/SYSLOGD_OPTIONS="-r -m 0"/g' /etc/sysconfig/syslog && /etc/init.d/syslog restart
  33. fi
  34. else
  35. echo "haproxy is already exists!"
  36. fi

2、添加/usr/local/haproxy/conf/haproxy.cfg配置文件

  1. #HAProxy配置文件vim /usr/local/haproxy/conf/haproxy.cfg
  2.  
  3. global
  4. log 127.0.0.1 local0 info #[err warning info debug]
  5. maxconn 65535
  6. chroot /usr/local/haproxy
  7. user haproxy
  8. group haproxy
  9. daemon
  10. nbproc 1
  11. pidfile /usr/local/haproxy/haproxy.pid
  12. defaults
  13. maxconn 65535
  14. timeout connect 5s #连接超时
  15. timeout client 30s #客户端超时
  16. timeout server 30s #服务器超时
  17. listen stats
  18. bind 0.0.0.0:1080
  19. mode http
  20. log 127.0.0.1 local0 info
  21. option httplog
  22. option httpclose
  23. option dontlognull
  24. retries 3
  25. stats uri /admin?stats
  26. frontend varnish_front
  27. bind :80
  28. mode http
  29. log global
  30. option httplog
  31. option httpclose
  32. option forwardfor
  33. maxconn 65535
  34. capture request header Host len 40
  35. capture request header Content-Length len 10
  36. capture request header Refererlen 200
  37. capture response header Server len 40
  38. capture response header Content-lenthlen 10
  39. capture response header Cache-Control len 10
  40. default_backendvarnish_backend
  41. backend varnish_backend
  42. mode http
  43. balance uri
  44. hash-type consistent
  45.  
  46. server varnish0 192.168.1.20:8089 cookie varnish0 weight 1 check inter 40000 maxconn 65535 rise 3 fall 3
  47. server varnish1 192.168.1.21:8089 cookie varnish1 weight 1 check inter 40000 maxconn 65535 rise 3 fall 3
  48. server varnish2 192.168.1.22:8089 cookie varnish2 weight 1 check inter 40000 maxconn 65535 rise 3 fall 3

3、工作配置 3.1、HAProxy开机启动脚本 # vim /etc/rc.d/init.d/haproxy内容如下:

  1. #! /bin/sh
  2. # chkconfig: - 90 10 #chkconfig:行和 description:行一定要写,否则执行service –add时会出现:haproxy服务不支持chkconfig
  3. # description: HAProxy is a TCP/HTTP reverse proxy which is particularly suited for high availability environments.
  4.  
  5. . /etc/init.d/functions
  6. # Source networking configuration.
  7. . /etc/sysconfig/network
  8. # Check that networking is up.
  9.  
  10. [ ${NETWORKING} = "no" ] && exit 0
  11. config="/usr/local/haproxy/conf/haproxy.cfg"
  12. exec="/usr/local/haproxy/sbin/haproxy"
  13. PID=/usr/local/haproxy/haproxy.pid
  14. prog="haproxy"
  15. [ -f $config ] || exit 1
  16. RETVAL=0
  17. start() {
  18. daemon $exec -c -f $config
  19. if [ $? -ne 0 ]; then
  20. echo "Errors found in configuration file."
  21. return 1
  22. fi
  23. echo -n "Starting HAproxy: "
  24. $exec -D -f $config
  25. RETVAL=$?
  26. echo
  27. [ $RETVAL -eq 0 ] && touch /var/lock/subsys/haproxy
  28. return $RETVAL
  29. }
  30. stop() {
  31. echo -n "Shutting down HAproxy: "
  32. killproc -p $PID $prog
  33. RETVAL=$?
  34. echo
  35. [ $RETVAL -eq 0 ] &&rm -f /var/lock/subsys/haproxy
  36. return $RETVAL
  37. }
  38. restart() {
  39. $exec -c -q -f $config
  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. }
  47. rhstatus() {
  48. status -p $PID $prog
  49. }
  50. # See how we were called.
  51. case "$1" in
  52. start)
  53. start
  54. ;;
  55. stop)
  56. stop
  57. ;;
  58. restart)
  59. restart
  60. ;;
  61. status)
  62. rhstatus
  63. ;;
  64. *)
  65. echo $"Usage: haproxy {start|stop|restart|status}"
  66. RETVAL=1
  67. esac
  68. exit $RETVAL

#保存后给予可执行权限 chmod +x /etc/rc.d/init.d/haproxy #就可以使用/sbin/service haproxystart|stop|restart|status来控制服务的启动、关闭和重启、查询状态 #并可以通过以下命令加载到开机服务列表 /sbin/chkconfig --add haproxy /sbin/chkconfig --level 2345haproxy on 3.2、HAProxy日志切分脚本 a)、添加/usr/local/haproxy/bin/cut_haproxy_log.sh脚本

  1. #! /bin/sh
  2.  
  3. HOME_PATH=/data/haproxy
  4. LOG_FILE=${HOME_PATH}/logs/haproxy.log
  5. date=$(date -d "yesterday" +"%Y-%m-%d")
  6.  
  7. cp ${LOG_FILE} ${HOME_PATH}/logs/haproxy-${date}.log
  8. echo > ${LOG_FILE}

b)、定时运行切分日志脚本

  1. echo "00 0 * * * /usr/local/haproxy/bin/cut_haproxy_log.sh" >> /etc/crontab