线上Haproxy配置

安装平台:centos 6.3

cd /srv

wget http://haproxy.1wt.eu/download/1.4/src/haproxy-1.4.22.tar.gz

#1.4.22版本比较稳定,现在最新版本是1.4.23

tar zxvf haproxy-1.4.22.tar.gz

cd haproxy-1.4.22

make TARGET=linux26 PREFIX=/usr/local/haproxy install

mkdir /usr/local/haproxy/conf -p

groupadd -r haproxy

useradd -r -M -g haproxy -s /sbin/nologin haproxy

vim /usr/local/haproxy/conf/haproxy.cfg

#写入以下内容,根据自己环境修改IP地址

global

       log     127.0.0.1 local0 info

       maxconn 4096

       user    haproxy

       group   haproxy

       daemon

       nbproc  1

       pidfile /var/run/haproxy.pid

defaults

       log             global

       mode            http

       option          dontlognull

       retries         3

       option          redispatch

       maxconn         20000

       contimeout      5000

       clitimeout      50000

       srvtimeout      50000


#根据自己需求配置,HA_IP为haproxy的IP地址,IP为后端服务器的IP地址,下面只写了两个实例

#mysql服务器

listen  server_mysql_name  HA_IP:3306

       mode tcp

       maxconn 200

       balance roundrobin

       server  mysql_name_A  mysql_IP:3306  check port 3306 inter 5s rise 2 fall 3

       server  mysql_name_B  mysql_IP:3306  check port 3306 inter 5s rise 2 fall 3


#web服务器

listen tomcat-app *:8080

       maxconn 2000

       mode http

       balance source

       option httpclose # disable keep-alive

       option forwardfor

       option httpchk GET /safe

       server server_web_A  IP:8080  check inter 2000 fall 3

       server server_web_B  IP:8080  check inter 2000 fall 3


#监控管理

listen  admin_status

       mode  http

       bind HA_IP:8899

       option httplog

       log global

       stats enable

       stats refresh 10s

       stats hide-version

       stats realm Haproxy\ Statistics

       stats uri  /admin-status

       stats auth  admin:admin

       stats admin if TRUE


#启动脚本

cd /etc/init.d/

vim haproxy

#!/bin/sh

# haproxy

# chkconfig:   35 85 15

# processname: haproxy

# config:      /usr/local/haproxy/conf/haproxy.cfg

# pidfile:     /var/run/haproxy.pid


# Source function library.

. /etc/rc.d/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"

prog=$(basename $exec)


[ -e /etc/sysconfig/$prog ] && . /etc/sysconfig/$prog


lockfile=/var/lock/subsys/haproxy


check() {

   $exec -c -V -f $config

}


start() {

   $exec -c -q -f $config

   if [ $? -ne 0 ]; then

       echo "Errors in configuration file, check with $prog check."

       return 1

   fi


   echo -n $"Starting $prog: "

   # start it up here, usually something like "daemon $exec"

   daemon $exec -D -f $config -p /var/run/$prog.pid

   retval=$?

   echo

   [ $retval -eq 0 ] && touch $lockfile

   return $retval

}


stop() {

   echo -n $"Stopping $prog: "

   # stop it here, often "killproc $prog"

   killproc $prog

   retval=$?

   echo

   [ $retval -eq 0 ] && rm -f $lockfile

   return $retval

}


restart() {

   $exec -c -q -f $config

   if [ $? -ne 0 ]; then

       echo "Errors in configuration file, check with $prog check."

       return 1

   fi

   stop

   start

}


reload() {

   $exec -c -q -f $config

   if [ $? -ne 0 ]; then

       echo "Errors in configuration file, check with $prog check."

       return 1

   fi

   echo -n $"Reloading $prog: "

   $exec -D -f $config -p /var/run/$prog.pid -sf $(cat /var/run/$prog.pid)

   retval=$?

   echo

   return $retval

}


force_reload() {

   restart

}


fdr_status() {

   status $prog

}


case "$1" in

   start|stop|restart|reload)

       $1

       ;;

   force-reload)

       force_reload

       ;;

   checkconfig)

       check

       ;;

   status)

       fdr_status

       ;;

   condrestart|try-restart)

       [ ! -f $lockfile ] || restart

       ;;

   *)

       echo $"Usage: $0 {start|stop|status|checkconfig|restart|try-restart|reload|force-reload}"

       exit 2

esac


chmod 755 /etc/init.d/haproxy

/etc/init.d/haproxy start|stop|restart

你可能感兴趣的:(haproxy配置)