haproxy部署笔记

1、HAProxy安装及日志配置

#  vim install-haproxy.sh 
#! /bin/bash 

dir=/usr/local 
ha_dir=${dir}/haproxy

#install 
if [ ! -e "$ha_dir" ]; then 
#下载源码到/tmp/haproxy-1.4.22.tar.gz 
wget -nc -P /tmp http://haproxy.1wt.eu/download/1.4/src/haproxy-1.4.22.tar.gz 
  tar -zxvf /tmp/haproxy-1.4.22.tar.gz -C /tmp 
  cd /tmp/haproxy-1.4.22

#编译、安装 
  make TARGET=linux26 PREFIX=/usr/local/haproxy 
  make install PREFIX=/usr/local/haproxy 
  cd /usr/local/haproxy 
# 
if [ ! -e "$ha_dir" ]; then 
echo "error! can't install haproxy please check! Will now out of the scrip !" 
        exit 1 
  else 
     #添加启动用户组 
     mkdir -p /var/haproxy 
     /usr/sbin/groupaddhaproxy 
     /usr/sbin/useradd -g haproxy -d /var/haproxy -s /sbin/nologinhaproxy 
     #创建日志目录 
     mkdir -p /data/haproxy/logs 
     chown -R haproxy:haproxy /data/haproxy/ 
     chmod 775 -R /data/haproxy 
        ! grep 'haproxy' /etc/syslog.conf&& echo ' local0.*      /data/haproxy/logs/haproxy.log'>> /etc/syslog.conf 
     sed -ir 's/SYSLOGD_OPTIONS="-m 0"/SYSLOGD_OPTIONS="-r -m 0"/g'   /etc/sysconfig/syslog && /etc/init.d/syslog restart 
fi 
else 
echo "haproxy is already exists!" 
fi

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

#HAProxy配置文件vim /usr/local/haproxy/conf/haproxy.cfg

global 
  log 127.0.0.1 local0 info #[err warning info debug] 
maxconn 65535 
chroot /usr/local/haproxy 
  user haproxy 
  group haproxy 
  daemon 
nbproc 1 
pidfile /usr/local/haproxy/haproxy.pid 
defaults 
maxconn 65535 
  timeout connect 5s  #连接超时 
  timeout client 30s  #客户端超时 
  timeout server 30s  #服务器超时 
listen stats 
  bind 0.0.0.0:1080 
  mode http 
  log 127.0.0.1 local0 info 
  option httplog 
  option httpclose 
  option dontlognull 
  retries 3 
  stats uri /admin?stats 
frontend varnish_front 
  bind :80 
mode http 
log global 
  option httplog 
  option httpclose 
  option forwardfor 
maxconn 65535  
  capture request header Host len 40 
  capture request header Content-Length len 10 
  capture request header Refererlen 200 
  capture response header Server len 40 
  capture response header Content-lenthlen 10 
  capture response header Cache-Control len 10  
default_backendvarnish_backend 
backend varnish_backend 
  mode http 
  balance uri 
  hash-type consistent

server varnish0 192.168.1.20:8089 cookie varnish0 weight 1 check inter 40000 maxconn 65535 rise 3 fall 3 
server varnish1 192.168.1.21:8089 cookie varnish1 weight 1 check inter 40000 maxconn 65535 rise 3 fall 3 
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内容如下:

#! /bin/sh 
# chkconfig: - 90 10  #chkconfig:行和 description:行一定要写,否则执行service –add时会出现:haproxy服务不支持chkconfig 
# description: HAProxy is a TCP/HTTP reverse proxy which is particularly suited for high availability environments.

. /etc/init.d/functions 
# Source networking configuration. 
. /etc/sysconfig/network 
# Check that networking is up.

[ ${NETWORKING} = "no" ] && exit 0 
config="/usr/local/haproxy/conf/haproxy.cfg" 
exec="/usr/local/haproxy/sbin/haproxy" 
PID=/usr/local/haproxy/haproxy.pid 
prog="haproxy" 
[ -f $config ] || exit 1 
RETVAL=0 
start() { 
  daemon $exec -c -f $config 
if [ $? -ne 0 ]; then 
echo "Errors found in configuration file." 
        return 1 
    fi 
  echo -n "Starting HAproxy: " 
  $exec -D -f $config 
  RETVAL=$? 
  echo 
  [ $RETVAL -eq 0 ] && touch /var/lock/subsys/haproxy 
  return $RETVAL 
} 
stop() { 
echo -n "Shutting down HAproxy: " 
killproc -p $PID $prog 
RETVAL=$? 
echo 
[ $RETVAL -eq 0 ] &&rm -f /var/lock/subsys/haproxy 
return $RETVAL 
} 
restart() { 
$exec -c -q -f $config 
if [ $? -ne 0 ]; then 
echo "Errors found in configuration file, check it with 'haproxy check'." 
       return 1 
   fi 
stop 
start 
} 
rhstatus() { 
status -p $PID $prog 
} 
# See how we were called. 
case "$1" in 
start) 
        start 
        ;; 
stop) 
        stop 
        ;; 
restart) 
        restart 
        ;; 
status) 
rhstatus 
        ;; 
*) 
        echo $"Usage: haproxy {start|stop|restart|status}" 
        RETVAL=1 
esac 
        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脚本

#! /bin/sh

HOME_PATH=/data/haproxy 
LOG_FILE=${HOME_PATH}/logs/haproxy.log 
date=$(date -d "yesterday" +"%Y-%m-%d")

cp ${LOG_FILE} ${HOME_PATH}/logs/haproxy-${date}.log 
echo > ${LOG_FILE}

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

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

发表评论