You are here: Home / Debian GNU/Linux / Servers / HTTP / Install HAproxy on Debian

Install HAproxy on Debian

by Pierre-Yves Landuré last modified Feb 07, 2014 12:29

HAproxy is a HTTP load balancer designed to be a frontend to a HTTP servers farm. This howto ease its installation on Debian.

This howto is tested on:

  • Debian 7.0 Wheezy

Prerequisite

This howto needs:

Parameters

Provide the name of the load balancing cluster:

CLUSTER_NAME="cluster"

Provide the listening address and port of the load balancing cluster:

CLUSTER_BIND="*:8080"

Provide the members of the load balancing cluster (host:port, one host by line):

MEMBERS="http1.domain.com:8080
http2.domain.com:8080
"

Installation

Detect the distribution name:

DEBIAN_VERSION=$(command lsb_release -cs)

Environment preparation

Install the needed software:

command apt-get -t "${DEBIAN_VERSION}-backports" install "haproxy"

Enable the server:

command sed -i -e 's/^ENABLED=.*$/ENABLED=1/' '/etc/default/haproxy'

Adjust proxy settings:

if [ -z "$(command grep 'option httpclose' '/etc/haproxy/haproxy.cfg')" ]; then
  command sed -i \
      -e '/dontlognull/a\
maxconn 20000
        option httpclose\
        option abortonclose\
        option httpchk\
        option redispatch' \
    '/etc/haproxy/haproxy.cfg'
fi

If needed, raise the timeouts:

command sed -i \
    -e 's/contimeout.*/contimeout 600000/' \
    -e 's/clitimeout.*/clitimeout 180000/' \
    -e 's/srvtimeout.*/srvtimeout 180000/' \
  '/etc/haproxy/haproxy.cfg'

Configuration

Create the cluster frontend configuration:

CONFIG="
### Setup for cluster ${CLUSTER_NAME}
frontend ${CLUSTER_NAME}-in
    bind ${CLUSTER_BIND}
    default_backend ${CLUSTER_NAME}

backend ${CLUSTER_NAME}
balance leastconn"

Create the cluster configuration:

for MEMBER in ${MEMBERS}; do
  MEMBER_HOST="$(command echo "${MEMBER}" | command cut --delimiter=':' --fields=1)"
  MEMBER_PORT="$(command echo "${MEMBER}" | command cut --delimiter=':' --fields=2)"
  MEMBER_IP="$(command getent ahostsv4 "${MEMBER_HOST}" \
      | command cut --delimiter=" " --fields=1 \
      | command tail -n 1)"
  CONFIG="${CONFIG}
    server ${MEMBER_HOST} ${MEMBER_IP}:${MEMBER_PORT} check"
done

Add the generated configuration to the software configuration file:

echo "${CONFIG}" >> '/etc/haproxy/haproxy.cfg'

Reload the configuration:

service haproxy reload

Thanks