Install HAProxy on CentOS 6.3

HAProxy is an open source high-performance proxy forwarding software that provides high availability, load balancing and proxy based on TCP and HTTP applications, and supports virtual hosts. It is a free, fast and reliable solution. The most useful is to use it as a front-end load balancer. It can provide layer 4 and layer 7 loads. The most used is layer 7 (mode http). HAProxy is especially suitable for those web sites with heavy load. These sites typically require session persistence or Layer 7 processing in turn. HAProxy runs on current hardware and can fully support tens of thousands of concurrent connections. And its mode of operation makes it easy and secure to integrate into your current architecture, while protecting your web server from being exposed to the network.


Regarding performance, haproxy officially said that under the same conditions lvs>haproxy>nginx, the forwarding of lvs is directly completed in the kernel space, and haproxy and nginx also need to use user space resources, while the resources called by haproxy are relatively small compared to nginx, but





HAProxy is implemented. An event-driven, single-process model that supports a very large number of concurrent connections. A multi-process or multi-threaded model is limited by memory constraints, system scheduler constraints, and ubiquitous locks, and can rarely handle thousands of concurrent connections. The event-driven model does not have these problems because it implements all these tasks on the user-space (User-Space) with better resource and time management. The downside of this model is that these programs typically scale less well on multi-core systems. That's why they have to optimize to make more work per CPU time slice (Cycle).





Software download:

Download address: http://www.haproxy.org/download/1.5/src/haproxy-1.5.5.tar.gz

Dependency package installation:

# yum install gcc gcc-c++ make zlib-devel bzip2-devel openssl -devel

haproxy install:

# tar -zvxf haproxy-1.5.5.tar.gz
# cd haproxy-1.5.5
# uname -a      ## 查看linux内核版本
# make TARGET=linux26 PREFIX=/usr/local/haproxy-1.5.5
# make install PREFIX=/usr/local/haproxy-1.5.5

编辑启动文件:
# vi /etc/init.d/haproxy   --添加以下内容

#!/bin/sh
#
# haproxy
#
# haproxy starting and stopping the haproxy load balancer
#
# chkconfig: 345 55 45
# description: HAProxy is a free, very fast and reliable solution
#               offering high availability, load balancing, and
#               proxying for TCP and  HTTP-based applications
# config:      /etc/haproxy/haproxy.cfg
# pidfile:     /var/run/haproxy.pid
# probe: true


# Source function library.
. /etc/rc.d/init.d/functions

# Source networking configuration.
. /etc/sysconfig/network

# Check that networking is up.
[ ${NETWORKING} = "no" ] && exit 0

[ -f /usr/local/haproxy-1.5.5/sbin/haproxy ] || exit 0

[ -f /etc/haproxy/haproxy.conf ] || exit 0

# Define our actions
checkconfig() {
        # Check the config file for errors
        /usr/local/haproxy-1.5.5/sbin/haproxy -c -q -f /etc/haproxy/haproxy.conf
        if [ $? -ne 0 ]; then
                 echo "Errors found in configuration file."
                return 1
        fi

        # We're OK!
        return 0
}


start() {
        # Check config
        /usr/local/haproxy-1.5.5/sbin/haproxy -c -q -f /etc/haproxy/haproxy.conf
        if [ $? -ne 0 ]; then
                echo "Errors found in configuration file."
                return 1
        fi

        echo -n "Starting HAProxy: "
        daemon /usr/local/haproxy-1.5.5/sbin/haproxy -D -f /etc/haproxy/haproxy.conf -p /var/run/haproxy.pid

        RETVAL=$?
        echo
        [ $RETVAL -eq 0 ] && touch /var/lock/subsys/haproxy
        return $RETVAL
}

stop() {
        echo -n "Shutting down HAProxy: "
        killproc haproxy -USR1

        RETVAL=$?
        echo
        [ $RETVAL -eq 0 ] && rm -f /var/lock/subsys/haproxy
        [ $RETVAL -eq 0 ] && rm -f /var/run/haproxy.pid
        return $RETVAL
}

restart() {
        /usr/local/haproxy-1.5.5/sbin/haproxy -c -q -f /etc/haproxy/haproxy.conf
        if [ $? -ne 0 ]; then
                echo "Errors found in configuration file."
                return 1
        fi

        stop
        start
}

check() {
        /usr/local/haproxy-1.5.5/sbin/haproxy -c -q -V -f /etc/haproxy/haproxy.conf
}

rhstatus() {
        status haproxy
}

reload() {
        /usr/local/haproxy-1.5.5/sbin/haproxy -c -q -f /etc/haproxy/haproxy.conf
        if [ $? -ne 0 ]; then
                echo "Errors found in configuration file."
                return 1
        fi

        echo -n "Reloading HAProxy config: "
        /usr/local/haproxy-1.5.5/sbin/haproxy -f /etc/haproxy/haproxy.conf -p /var/run/haproxy.pid -sf $(cat /var/run/haproxy.pid)

        success $"Reloading HAProxy config: "
        echo
}


# Possible parameters
case "$1" in
  start)
        start
        ;;
  stop)
        stop
        ;;
  status)
        rhstatus
        ;;
  restart)
        restart
        ;;
  reload)
        reload
        ;;
  checkconfig)
        check
        ;;
  *)
        echo "Usage: haproxy {start|stop|status|restart|reload|checkconfig}"
        exit 1
esac

exit 0



authorization:

# chmod +x /etc/init.d/haproxy


Edit the haproxy configuration file:

# mkdir /etc/
haproxy # vi /etc/haproxy/haproxy.conf

global
      maxconn 4096 #Default maximum number of connections
      pidfile /var/run/haproxy.pid #The pid storage path of haproxy
      daemon #Run haproxy in the
      background nbproc 1 #Number of processes
defaults
        log global
        mode http #The category processed (#7 layer http;4 layer tcp)
        retries 3 # If the connection fails for three times, it is considered that the server is unavailable
        option httplog
        option httpclose
        option redispatch #After the server corresponding to serverId hangs, it is forced to be directed to other healthy servers
        option abortonclose #When the server load is very high, it will automatically end the current queue processing for a long time connection
        maxconn 4096 #maximum number of connections
        timeout connect 50000 #connection timeout
        timeout client 50000 #client timeout
        timeout server 50000 #Server timeout
        balance roundrobin #Default load balancing method, polling method

listen mysql_proxy
        bind *:3366 #Listening port
        mode tcp #4-layer mode of tcp
        balance roundrobin
        option tcpka
        option httpchk
        server mysqldb1 172.16.10.70:3306 weight 1 #weight represents the weight
        server mysqldb1 172.16.10.71:3306 weight 1 #weight represents the weight
    
listen stats
       bind *:8888
       mode http
       option httplog
       option httpclose
       balance roundrobin
       stats refresh 5s
       stats uri / #Website health detection URL, used to detect whether the website managed by HAProxy is available, returns 200 normally, returns 503 abnormally
       stats realm Haproxy\ Statistics
       stats auth admin:admin #Account password


Start

and check: # service haproxy start
# netstat -plantu | grep 3366

tcp 0 0 0.0.0.0:3366 0.0.0.0:* LISTEN 23626/haproxy






Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326944217&siteId=291194637