Alright, let’s get straight to the point: building reliable software is critical. It’s not just a matter of nice-to-haves; it’s about building something that stands the test of time and isn’t prone to constant crashes. Now, this requires us to keep a diligent watch on the software’s health. And when it comes to systems based on microservices, where there are numerous components working in tandem, this becomes even more important. Enter health checks.
Among the common ways of performing health checks, using commands like curl or wget comes to mind if you’re on a Unix-like operating system. However, with pared-down images, we may not have these tools at our disposal. So, we need something compact, portable, and effective. Well, I present to you a simple solution: a bash script to check the health of a container.
Here’s how the script looks:
#!/bin/bash
# This script performs a basic HTTP health check on a given host and port.
# It sends a simple HTTP GET request and checks for a "200 OK" response.
# The script accepts two optional command-line arguments:
# 1. The port to check (default: 8080)
# 2. The host to check (default: localhost)
# If the host responds with "200 OK", the script exits with status code 0.
# If the host responds with anything else, the script exits with status code 1.
port_default=8080
host_default=localhost
port=${1:-$port_default}
host=${2:-$host_default}
exec 3<>/dev/tcp/${host}/${port}
echo -e "GET / HTTP/1.1\r\nhost: ${host}\r\nConnection: close\r\n\r\n" >&3
while IFS= read -r line
do
# echo "$line"
if [[ "$line" =~ "200 OK" ]]; then
echo "HTTP 200 OK response received from ${host}"
exit 0
fi
done <&3
echo "No HTTP 200 OK response received from ${host}"
exit 1
This nifty bash script conducts a basic HTTP health check on any specified host and port. It dispatches an HTTP GET request and awaits a “200 OK” response.
How to use it?
Using this script is a piece of cake. During your build, just copy the script in and set the HEALTHCHECK
to execute it with bash:
FROM bitnami/nginx
# ...
COPY [ "./healthcheck.sh", "/opt/healthcheck.sh" ]
HEALTHCHECK --interval=10s --timeout=3s --start-period=5s \
CMD ["bash", "/opt/healthcheck.sh"]
And, if you wish, you can even mount it and specify the health check in your compose file.
The Good
- Straightforward: No frills attached. Just a plain and simple script. No dependency issues as it only requires bash. A perfect fit for slim containers.
- Portable: It’s a bash script; it’s at home in any Unix-like system. This adds to its versatility.
- Customizable: It lets you adjust the host and port parameters as per your requirements.
- Light and efficient: It does what it’s designed for - health checking, and does it quite efficiently.
The Not-So-Good
- Too simple: It only checks for a 200 OK response. It doesn’t interpret other response codes which could also indicate the service is functioning.
- Limited error handling: It’s not very adept at managing errors. It doesn’t check if the port and host are operational, or if it has connected as intended.
- Only TCP: This script is only suitable for the TCP protocol.
Final Thoughts
Yes, the script may seem basic, but hold on! It’s a worthy contender in situations where you need a lightweight, quick health check client. Want to make it more robust? Enhance its error handling, make it recognize more HTTP response codes, or even make it compatible with other protocols such as UDP. Despite its simplicity, this script perfectly embodies the beauty of practical programming in the world of container management and monitoring.