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:
global
maxconn 10000
ulimit-n 65536
log 127.0.0.1 local1 notice
In your backends or default config, refer to global
:
defaults
log global
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:
$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):
# .. 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:
$ restart rsyslog
And you’re done. Check for HAProxy logs in:
$ 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
:
/var/log/haproxy*.log
{
rotate 4
weekly
missingok
notifempty
compress
delaycompress
sharedscripts
postrotate
reload rsyslog >/dev/null 2>&1 || true
endscript
}
Happy logging!
=========================================================================
How to enable logging of haproxy in rsyslog
After installing the HAproxy 1.4 in CentOS 6.4 bydefault logging of haproxy was not enable.To enable the logging of HAProxy you have to enable it in rsyslog(In CentOS 6.4 minimal installation,rsyslog version 5.2 is shipped).
To setup logging in HAproxy,follow the given below steps
Step 1: In Global Section of haproxy.cfg put the value log 127.0.0.1 local0
.Like given below
global log 127.0.0.1 local0
Step 2: Create new haproxy configuration file in /etc/rsyslog.d . Here we are keeping the log in localhost or in other words we should say HAproxy server
Note:
- local0.=info -/var/log/haproxy.log defines the http log will be saved in haproxy.log
- local0.notice -/var/log/haproxy-status.log defines the Server status like start,stop,restart,down,up etc. will be saved in haproxy-status.log
- UDPServerRun 514 means opening UDP port no. 514 to listen haproxy messages
vi /etc/rsyslog.d/haproxy.conf $ModLoad imudp $UDPServerRun 514 $template Haproxy,"%msg%\n" local0.=info -/var/log/haproxy.log;Haproxy local0.notice -/var/log/haproxy-status.log;Haproxy ### keep logs in localhost ## local0.* ~
Step 3: Now restart the HAproxy service
/etc/init.d/haproxy restart
After restarting the haproxy service two logs will be created itself i.e haproxy.log and haproxy-status.log
Step 4: Check your logrotae file for haproxy also so that logs keep on rotating and compress itself. If bydefault it is not present in /etc/logrotate.d directory then create a new file called haproxy and paste the given below code.
Here I am keeping the logs upto 120 days hence
rotate 120 and daily
is written in file.cat /etc/logrotate.d/haproxy /var/log/haproxy.log { missingok notifempty sharedscripts rotate 120 daily compress postrotate reload rsyslog >/dev/null 2>&1 || true endscript }