Haproxy for Dev

At work we have 1 ip address and many web services to run (ticket management, a wiki, a small army of dev and qa installs of our application, etc). Our solution has been to use dns names when the thing is running on the same server (wiki.domain.com, dev1.domain.com, qa1.domain.com, etc) and different ports to send requests to different servers. This works be requires use to remember the magic port number for everything.

Enter haproxy. Haproxy is a layer 4 and 7 (the important bit) load balancer. It’s really easy to install, really easy to set up, and allows you to load balancing or proxy requests based on domain name or url. To install haproxy all that needed to be done was, “yum install haproxy.” I’m sure you can substitute apt-get or your distros package manager of choice just as easily. All of the configuration happens in /etc/haproxy/haproxy.cfg. This file came pre-loaded with global and defaults sections which I kept and frontend and 2 backend sections that I deleted. The basic setup is that you create a frontend section the corresponds to requests, and backend sections that correspond to servers which handle the requests. For example:

frontend domain
    bind *:80
    acl wiki hdr_beg(host) -i wiki.
    acl dev1 hdr_beg(host) -i dev1.
    use_backend wiki_back if wiki
    use_backend dev1_back if dev1

backend wiki_back
    server wiki1

backend dev1_back
    server dev1-1

The above says that a request for wiki.domain.com (assuming domain.com was being forwarded to the server we set this up on) will be sent to Specifically it’s saying:

  • frontend domain
    • I have a frontend named domain.
  • bind *:80
    • the front end is listening to requests on any ip on port 80
  • acl wiki hdr_beg(host) -i wiki.
    • I’m listening for a hostname beginning (hdr_beg(host)) with wiki (-i wiki.)
  • use_backend wiki_back if wiki
    • If the request is for wiki then I’m going to use some backend named wiki_back
  • backend wiki_back
    • I have some backend named wiki_back
  • server wiki1
    • The backend has a server with ip that’s named wiki1

This isn’t really using a lot of the power of haproxy. For one we could set up load balancing on the backends with a line like, “balance roundrobin”, in a backend block. This is also only looking at the beginning of hostnames. We could set acl that listens for domain.com with, “acl domainDefault hdr_end(host) -i domain.com”. We could set up a acl that listens based on the url such as, “acl logs path_beg /kibana”. And that covers my knowledge of haproxy but haproxy has many more features such as the ability to deal with and change headers.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: