- Lab
-
Libraries: If you want this lab, consider one of these libraries.
- Cloud
Configuring syslog Logging with HAProxy
HAProxy is well-known for its precise logs, which offer great transparency and are very helpful with managing and troubleshooting complex environments. HAProxy logging is also very configurable, for example, allowing you to distribute different levels of logging to different logs, log to multiple destinations, or to customize what goes in your logs. In this lab, we’re going to get hands-on and configure HAProxy logging. We’re also going to configure `rsyslog` to accept HAProxy log data. Upon completion of this lab, you will be able to configure customized HAProxy logging with `rsyslog`.
Lab Info
Table of Contents
-
Challenge
Configure HAProxy Logging
Configure
rsyslogto Accept HAProxy Log DataWe're going to configure HAProxy to feed log data to the
rsysloginstance running on our HAProxy server.Before we get started, let's take a look at the
/var/logdirectory just to show we have no HAProxy logs.The first thing we need to do is add some configuration code to our
/etc/rsyslog.ddirectory. We'll create a file called99-haproxy.confin this directory.Create a configuration file (
/etc/rsyslog.d/99-haproxy.conf) with the following characteristics:- Listen for log data on 127.0.0.1, port 514, using UDP.
- Log all levels from the
local0facility to the file/var/log/haproxy-traffic.log. - Log only
noticefrom thelocal0facility to the file/var/log/haproxy-admin.log.
Save, exit, and restart the
rsyslogdservice.Let's take another look at the
/var/logdirectory, just to show we have no HAProxy logs.No logs yet! Let's change that.
Configure HAProxy to Send Log Data to
rsyslogNow that
rsyslogis configured to accept log data, we're going to go ahead and configure logging in HAProxy.Modify our HAProxy configuration at
/etc/haproxy/haproxy.cfg:- Replace the existing log entry in the
globalsection to send log data to thersyslogserver at 127.0.0.1, port 514, using thelocal0facility. - Scroll down to the
defaultssection and confirm there is a line,log global, which is the same as puttinglog globalin all thefrontendandbackendsections. - Scroll down to the
frontendssh-insection and confirm we haveoption tcplogset. This provides a log format that's more useful for Layer 4 traffic.
Save and exit, and then restart the
haproxyservice.Let's check out our new logs!
Check if we have HAProxy logs in
/var/log. We should see our new HAProxy log files!Take a look at the contents of the HAProxy log files. Compare them and confirm it appears that we are logging the correct error levels to the correct logs.
We need to generate some traffic to fill our logs with data.
Let's generate some HTTP traffic:
wget --no-check-certificate -O - http://www.site1.com/test.txtwget --no-check-certificate -O - http://www.site2.com/test.txtLet's generate some HTTPS traffic:
wget --no-check-certificate -O - https://www.site1.com/test.txtwget --no-check-certificate -O - https://www.site2.com/test.txtLet's try pulling a file from our SSH container, via HAProxy:
scp -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null -P 2222 [email protected]:/sshfiles/ssh-test.txt .Let's keep doing the
scpoperation until we hit our session limit (10) and log some errors, then take a look at the contents of our HAProxy log files again.We see our traffic logged in the
/var/log/haproxy-traffic.logfile, both HTTP and TCP. -
Challenge
Configure Separate HAProxy Logs for HTTP and TCP
We'd like to have individual logs for the HTTP and TCP traffic, plus a combined traffic log. Let's make some changes.
Separating HTTP and TCP Logs
Modify the
/etc/rsyslog.d/99-haproxy.confas follows:- Add a line in the
frontend http-https-insection to send log data to thersyslogserver at 127.0.0.1, port 514, using thelocal1facility. - Add a line in the
frontend sshd-insection to send log data to thersyslogserver at 127.0.0.1, port 514, using thelocal2facility.
So, we're using the
local0facility to pass log traffic defined on thegloballevel,local1for the HTTP log data, andlocal2for TCP log data.Let's stop
rsyslogand remove the current HAProxy log files, then startsyslogagain.There should be no HAProxy log files in
/var/log. Let's change that. Restart thehaproxyservice.Test HAProxy Logging Again
Let's see if we have all our HAProxy logs now. We should see our new HAProxy log files!
Let's take a look at the contents of our log files. We should see everything starting up. Let's generate some traffic to fill our logs with data!
Let's generate some HTTP traffic:
wget --no-check-certificate -O - http://www.site1.com/test.txtwget --no-check-certificate -O - http://www.site2.com/test.txtLet's generate some HTTPS traffic:
wget --no-check-certificate -O - https://www.site1.com/test.txtwget --no-check-certificate -O - https://www.site2.com/test.txtLet's try pulling a file from our SSH container, via HAProxy:
scp -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null -P 2222 [email protected]:/sshfiles/ssh-test.txt .Let's keep doing the
scpoperation until we hit our session limit (10) and log some errors.Take a look at the contents of the HAProxy log files again. We should see that we have 4 logs now, the
adminlog, one with HTTP traffic only, another with TCP traffic only, and a fourth with combined traffic.Let's see what happens when our backend goes down. Use the
podman stop -acommand to stop all the containers. You can use thepodman ps -acommand to confirm all the containers have been stopped.Take a look at the HAProxy log contents again. We should see the error messages indicating that our web servers are down.
Let's restart all our containers.
Starting all the web containers:
podman start site{1..2}_server{1..3}Starting the SSH container:
podman start sshd-serverTake a look at the HAProxy log contents again. We should see our servers are back up.
Congratulations, Cloud Guru! You just configured HAProxy logging!
- Add a line in the
About the author
Real skill practice before real-world application
Hands-on Labs are real environments created by industry experts to help you learn. These environments help you gain knowledge and experience, practice without compromising your system, test without risk, destroy without fear, and let you learn from your mistakes. Hands-on Labs: practice your skills before delivering in the real world.
Learn by doing
Engage hands-on with the tools and technologies you’re learning. You pick the skill, we provide the credentials and environment.
Follow your guide
All labs have detailed instructions and objectives, guiding you through the learning process and ensuring you understand every step.
Turn time into mastery
On average, you retain 75% more of your learning if you take time to practice. Hands-on labs set you up for success to make those skills stick.