- Lab
- A Cloud Guru
Exploring Basic HAProxy Monitoring Techniques
It’s not enough to just set up an HAProxy installation, we need to keep an eye on the status of the environment and the servers in our backend. Whether this means keeping an eye on statistics or the contents of logs, HAProxy has you covered. In this lab, we’re going to get hands-on with managing an HAProxy environment. We’re going to explore some monitoring options that are installed along with HAProxy. Upon completion of this lab, you will know how to leverage some included features to monitor your HAProxy environment.
Path Info
Table of Contents
-
Challenge
Interact Directly with the HAProxy stats Socket
-
Look at and monitor the status of our HAProxy installation by directly interacting with the
stats
socket. -
Look at the socket, using
file /var/lib/haproxy/stats
. -
Let's try some non-interactive commands. Pull raw data from the
stats
socket, usingnc
, by sending theshow stat
command to thestats
socket.It returns a lot of information in a format that's not really human-friendly, but it can be reformatted.
-
Add some formatting using
cut
andcolumn
, and a looping mechanism usingwatch
. Pick as many or as few fields as you like. Play around with the formatting and see what you can come up with.You should see that we're getting statistics for the fields we specified in the
cut
command, then usingcolumn
to make a nicely formatted table. Thewatch
command refreshes every second until we interrupt it withCTRL-C
. -
Try an interactive connection using
nc
and the/var/lib/haproxy/stats
socket.This will connect us to the socket and allow us to enter one command.
-
Use the
prompt
command to get an interactive interface. If you'd like help, use?
. Sending an empty line or using thequit
command followed byENTER
will get you out of the prompt.
So, this is the most direct, hands-on way to do it, but it's a lot of overhead for daily use, unless we put a framework around it. Is there a better way?
-
-
Challenge
Using the HAProxy stats Web Interface
The
/var/lib/haproxy/stats
socket is extremely useful, but manual interaction is a bit cumbersome. What if we had a better way to leverage it?-
Add a code block to the end of the
/etc/haproxy/haproxy.cfg
file that starts an HAProxystats
web server on port8050
for all addresses at the URI of/
. -
Hide the version of HAProxy in the statistics report for better security.
-
Restart the
haproxy
service. -
Connect to the
stats
web page on port8050
on the public IP (or DNS) of the lab server and check things out.We've got a nice, clean, web-based interface that presents our statistics in an easy-to-read format. If we want to update our statistics, we're just a refresh away.
-
-
Challenge
Examining HAProxy Logs Using HALog
Another handy utility we can use to get a handle on what's happening in our HAProxy environment is the inlcuded
halog
command-line utility.Before we dig deeper into the logs, we need to put more data in.
-
Launch 2 ApacheBench sessions:
ab -n 100 -c 10 https://www.site1.com/ > ~/ab_site1.log > /dev/null 2>&1 &
ab -n 100 -c 10 https://www.site2.com/ > ~/ab_site2.log > /dev/null 2>&1 &
-
Pull HTTP traffic using
curl
:for conn in `seq 1 100` ; do curl -k https://www.site1.com/ ; done > /dev/null 2>&1 &
for conn in `seq 1 100` ; do curl -k https://www.site2.com/ ; done > /dev/null 2>&1 &
-
Pull HTTP traffic using
wget
:for conn in `seq 1 100` ; do wget --no-check-certificate -O - https://www.site1.com/test.txt ; done > /dev/null 2>&1 &
for conn in `seq 1 100` ; do wget --no-check-certificate -O - https://www.site2.com/test.txt ; done > /dev/null 2>&1 &
-
Pull
scp
traffic:for conn in `seq 1 100` ; do bash -c 'scp -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null -P 2222 [email protected]:/sshfiles/ssh-test.txt . &' ; done > /dev/null 2>&1 &
Now that our logs are filled with data, we can proceed!
-
Extract data from the
/var/log/haproxy-combined-traffic.log
file usinghalog
:- Pull data on per-server HTTP statistics
- List URLs by the number of HTTP requests
- List URLs with 429 errors
- List URLs by the number of errors generated
- Try some of your own!
So,
halog
is a handy tool for parsing HAProxy logs to present the information contained within in an easy-to-read format that tells us exactly what we're looking for.Congratulations, Cloud Guru! You mastered 3 helpful monitoring methods that come with HAProxy!
-
What's a lab?
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.
Provided environment for hands-on practice
We will provide the credentials and environment necessary for you to practice right within your browser.
Guided walkthrough
Follow along with the author’s guided walkthrough and build something new in your provided environment!
Did you know?
On average, you retain 75% more of your learning if you get time for practice.