Jay Taylor's notes

back to listing index

HAProxy Logging in Ubuntu Lucid - Kevin van Zonneveld

[web search]
Original source (kvz.io)
Tags: logging linux haproxy syslog rsyslog udp kvz.io
Clipped on: 2013-07-10

HAProxy Logging in Ubuntu Lucid

Aug 11th, 2010 | Comments

At transloadit we use HAProxy “The Reliable, High Performance TCP/HTTP Load Balancer” so that we can offer different services on 1 port.

For instance, depending on the hostname, a requests to port 80 can be routed to either nodejs (in case of api.transloadit.com), or nginx (in case of www.transloadit.com).

HAProxy has been good to us and setting it up was a breeze. But getting HAProxy to log on Ubuntu Lucid was harder than I thought. All of the tutorials I found either didn’t cover logging, or had deprecated information on it.

Google suddenly stopped being my friend.

HAProxy wants to log

For performance & maintenance reasons HAProxy doesn’t log directly to files. Instead it wants to log against a syslog server. This is a separate Linux daemon that most servers are equiped with already, but HAProxy requires it to listen on UDP port 514, and usually that’s not enabled.

A syslog server:

  • receives log entries
  • decides what’s interesting
  • writes it to disk in a highly optimized way

these aspect can all be configured by you.

If we look at the top of your current /etc/haproxy/haproxy.cfg file, we may find something like:

1
2
3
4
global
        maxconn         10000
        ulimit-n        65536
        log             127.0.0.1       local1 notice

As you can see 127.0.0.1 is where it will try to find a syslog server to log to. On Unbuntu Lucid the default syslog daemon is rsyslogd, so let’s make it accept HAProxy log entries.

rsyslogd welcomes HAProxy

Most google hits I found on logging with HAProxy told me to change the /etc/default/rsyslog file, but that’s completely ignored with the new upstart system. And even if you make it adhere the defaults file (yep, I tried), it will make rsyslogd go down in compatibility mode. Which is not only a shame, but also unnecessary as it turns out.

Using these config lines:

1
2
3
4
5
$ModLoad imudp
$UDPServerAddress 127.0.0.1
$UDPServerRun 514
# Thanks Joeri Blokhuis of DongIT, pointing out that UDPServerAddress needs to
# go before UDPServerRun, or the server will run on 0.0.0.0

rsyslogd will open up it’s UDP port.

Where to put these lines you say? Well, if HAProxy is the only service you need the UDP syslog port for, you could put/uncomment the lot in just one /etc/rsyslog.d/49-haproxy.conf file (Thanks to Gilles for the ‘49-’ prefix):

1
2
3
4
5
6
7
8
9
10
# .. otherwise consider putting these two in /etc/rsyslog.conf instead:
$ModLoad imudp
$UDPServerAddress 127.0.0.1
$UDPServerRun 514

# ..and in any case, put these two in /etc/rsyslog.d/49-haproxy.conf:
local1.* -/var/log/haproxy_1.log
& ~
# & ~ means not to put what matched in the above line anywhere else for the rest of the rules
# http://serverfault.com/questions/214312/how-to-keep-haproxy-log-messages-out-of-var-log-syslog

Now do a quick:

1
restart rsyslog

And you’re done. Check for HAProxy logs in:

1
tail -f /var/log/haproxy*.log

Don’t forget to tweak the debug level in /etc/haproxy/haproxy.cfg, and maybe set up a logrotate right away in /etc/logrotate.d/haproxy:

1
2
3
4
5
6
7
8
9
10
11
12
13
/var/log/haproxy*.log
{
    rotate 4
    weekly
    missingok
    notifempty
    compress
    delaycompress
    sharedscripts
    postrotate
        reload rsyslog >/dev/null 2>&1 || true
    endscript
}

Happy logging!

Imported comments

These were imported from my old blog. Please use disqus below for new comments

Kevin on 2011-04-17 17:01:11
@ Steven Parkes & amp; Paul Charles Leddy: Very good additions, thanks a lot guys.

Paul Charles Leddy on 2011-03-21 22:26:59
Is that logrotate script pointing at the same logs defined in the haproxy.cfg you have sampled here?

Steven Parkes on 2011-03-18 00:31:40
Suggest you add

$UDPServerAddress 127.0.0.1

so that you don’t open a port to the rest of the network.

mandm on 2011-03-03 00:15:19
Thanks for the great article, this helped me setup logs in no time

Kevin on 2011-01-14 18:20:02
Hey Till,
Wow that is really spooky. I haven’t posted that yet, but I was about to. What’s more, I can prove it:
http://dl.dropbox.com/u/343579/coincidence.png

How about that : )

till on 2011-01-14 17:21:02
I’d add that I would setup logrotate along with those files and make sure it rotates at an appropriate rate and also only keeps files so and so long.


# /etc/logrotate.d/haproxy
/var/log/haproxy*
{
rotate 3
daily
missingok
notifempty
delaycompress
compress
}

arthur richards on 2010-11-10 02:26:25
yes! thank you so much! this is exactly what i needed.

Kevin on 2010-10-11 13:56:01
@ Dave Morehouse: You’re welcome : )

Dave Morehouse on 2010-10-04 21:24:10
Kevin - Thanks for the helpful tutorial. I needed to debug my HAProxy configuration and spent all day trying to get HAP logs to show up in syslog. Then I found your article and got it up and running in 5 minutes. This was a great tutorial for getting HAP logs working for those of us that aren’t familiar with syslogs.

Posted by Kevin van Zonneveld Aug 11th, 2010 haproxy, logging, nginx, nodejs, rsyslogd, syslog, transloadit, ubuntu

submit