How to Use xinetd to Build a Quick MySQL Health-Check Service for HAProxy
Learn how to create a lightweight HTTP health‑check service with xinetd that runs a shell script to monitor MySQL status, enabling HAProxy to instantly detect failures and stop routing traffic, complete with step‑by‑step configuration, testing commands, and sample scripts.
Scenario
Using HAProxy to load‑balance MySQL is common; to improve availability, HAProxy should stop forwarding requests as soon as a MySQL instance fails or replication breaks.
How HAProxy Detects MySQL Issues
HAProxy can query an HTTP service that runs a health‑check script; based on the HTTP status (200 for healthy, 503 for unhealthy) HAProxy decides whether the MySQL node is usable.
Solution Overview
Write a shell script that checks MySQL status and exits with 200 (healthy) or 503 (unhealthy) together with a message.
Expose the script via a simple HTTP service.
Configure HAProxy to call this HTTP service and interpret the response.
Quick Way to Expose a Shell Script as HTTP – xinetd
xinetd is an extended Internet daemon that can listen on a port, launch a script when a connection arrives, and return the script’s output directly.
HAProxy → xinetd → mysql‑check script, allowing HAProxy to obtain MySQL status.
xinetd Configuration Example
Open port 9200 and return the output of a test script.
Install xinetd if not present (e.g., yum install xinetd on CentOS 7).
1. Test script (test.sh)
#!/bin/bash
echo `uptime | egrep -o 'up ([0-9]+) days' | awk '{print $2}'`This script prints the number of days the server has been up. Make it executable with chmod +x test.sh.
2. xinetd service definition (/etc/xinetd.d/helloworld)
service helloworld
{
disable = no
port = 9200
socket_type = stream
protocol = tcp
wait = no
user = root
server = /root/test.sh
server_args = test
}Key fields: port specifies the listening port, server points to the script to execute.
3. Add service to /etc/services
# Comment out the original 9200 entry and add:
helloworld 9200/tcp
#wap-wsp 9200/tcp
#wap-wsp 9200/udp4. Restart xinetd
systemctl restart xinetd.service5. Test the service
Use nc localhost 9200 or telnet localhost 9200 to verify the script output is returned.
The service correctly outputs the script result, completing the example.
For a complete HAProxy‑aware MySQL health‑check implementation, see the GitHub article linked in the original post.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Java High-Performance Architecture
Sharing Java development articles and resources, including SSM architecture and the Spring ecosystem (Spring Boot, Spring Cloud, MyBatis, Dubbo, Docker), Zookeeper, Redis, architecture design, microservices, message queues, Git, etc.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
