haproxy

Install

  • Download haproxy and change directory to download directory
  • Run make and make install as root

Make

To choose TARGET run

uname -a

Output:

Linux somehost 2.6.32-358.11.1.el6.x86_64 #1 SMP Wed May 15 10:48:38 EDT 2013 x86_64 x86_64 x86_64 GNU/Linux

Considering the above was kernel 2.6, using TARGET as below.

make TARGET=linux2628 USE_PCRE=1 USE_LIBCRYPT=1 USE_LINUX_SPLICE=1 USE_LINUX_TPROXY=1

Some options listed may not work - feel free to remove them such as USE_PCRE=1

Make Install

make install

haproxy typically gets installed at /usr/local/sbin/haproxy

Configuration

To configure, create a configuration file at chosen location. Preferred: /etc/haproxy/haproxy.cfg

  • Load balance on port 8000
  • Sticky sessions between multiple servers (someserver1 & someserver2)
  • Add administration (port 7999)
global
    daemon
    maxconn 4096

defaults
    mode http
    timeout connect 5000ms
    timeout client 50000ms
    timeout server 50000ms

frontend http-in
    bind *:8000
    default_backend servers

backend servers
    retries 2
    fullconn 4096
    balance roundrobin
    cookie SERVERID insert indirect nocache
    server server01 someserver1:8080 check cookie s01
    server server02 someserver1:8081 check cookie s02
    server server03 someserver1:8082 check cookie s03
    server server04 someserver2:8090 check cookie s04
    server server05 someserver2:8091 check cookie s05
    server server06 someserver2:8092 check cookie s06

listen stats :7999
    mode http
    stats enable
    stats hide-version
    stats realm Haproxy\ Statistics
    stats uri /

Start

Simple way to start is as below. This runs as a daemon, since daemon is specified in the configuration. If not, add option -D.

haproxy -f /etc/haproxy/haproxy.cfg

Logging

Edit /etc/rsyslog.conf and uncomment:

$ModLoad imudp
$UDPServerRun 514

Edit /etc/rsyslog.d/haproxy.conf and append line:

local2.* /var/log/haproxy.log

Restart rsyslog:

service rsyslog restart

Change permissions on log file:

chmod o+r /var/log/haproxy.log
chmod g+r /var/log/haproxy.log

Update /etc/haproxy/haproxy.cfg

Edit /etc/haproxy/haproxy.cfg:

Add line in global section:

    log 127.0.0.1 local2

Add lines to frontend http-in section:

    option httplog
    log global

Restart haproxy

Logging Resources


QR Code
QR Code tech:others:haproxy (generated for current page)