Load balance anything with HAProxy

Recently I wanted to load balance a TCP service i.e. not http, in the past I’ve used ultramonkey but there doesn’t seem to be any maintained Redhat/Centos packages.

After some googling I found that haproxy can balance non-http services but examples of non-http configurations are few and far between, this blog post lead me to my solution, so after the jump I have a haproxy.cfg which will load balance smtp round-robin style across 4 servers, you also get a webstats interface listening on http://IP:8080/haproxy?stats (username = me, password = password).

By changing the port numbers from 25 (SMTP) you can effectively load balance any TCP app :)

global
        log 127.0.0.1   local0
        log 127.0.0.1   local1 notice
        maxconn 4096
        chroot /var/lib/haproxy
        user haproxy
        group haproxy
        daemon
        #debug
        #quiet

defaults
        log     global
        mode    http
        option  httplog
        option  dontlognull
        option redispatch
        retries 3
        maxconn 2000
        contimeout      5000
        clitimeout      50000
        srvtimeout      50000

listen stats :8080
        balance
        mode http
        stats enable
        stats auth me:password

listen smtp :25
        mode tcp
        option tcplog
        balance roundrobin

        server smtp 192.168.0.1:25 check
        server smtp1 192.168.0.2:25 check
        server smtp2 192.168.0.3:25 check
        server smtp3 192.168.0.4:25 check

Redhat/Centos rpm packages of haproxy are avilable from Fedora’s EPEL Project

rgds,

Nick


4 thoughts on “Load balance anything with HAProxy

  1. Nick,

    I was wondering if you could make me a VM I could download and would pay you for setting it up for me.

    I need port 25 too.

    Thank you,

    Gary

  2. Hi,

    I have two web servers (Oracle EBS). i have tested the load balancing and its working fine. when i access the virtual ip, it redirects to physical host, (that is fine) but physical host changes the URL to fqdn (also working).

    When i try to shutdown/stop the physical node. on client side the browser cache the VIP to physical host (FQDN) so every time it hits to the down node. whereas if open anther (alternate browser IE or Firefox) the HA-proxy will redirects to another active node.

    how to solve this problem ?

    thanks

    • Hello,
      Sounds like you need your FQDN to resolve to the HAProxy VIP so that all requests are routed through HAProxy rather than re-directing the browser to the physical node.
      Cheers,
      Nick

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>