I have a DO loadbalancer setup in front of a MySQL Group Replication setup. What I need to do is create a health check for the loadbalancer via an http request.
What I've come up with so far is to create a bash script (code below) that basically checks if MySQL is up and running and then run this via xinetd on port 9201.
However, when I curl to http://ip:port from another server it never reads any output from my bash file and just connection reset by peer (after connecting and timing out).
I'm very new to all this, so I'm not sure if what I'm doing is even correct in the slightest.
service mysqlchk
{
flags = REUSE
socket_type = stream
protocol = tcp
port = 9201
wait = no
user = root
server = /opt/mysqlchk.sh
disable = no
log_type = FILE /var/log/xinetd.log
log_on_success = DURATION EXIT HOST PID USERID
log_on_failure = ATTEMPT HOST USERID
only_from = 0.0.0.0/0
}
Bash (Found online)
MYSQL_HOST="ip"
MYSQL_PORT="3306"
MYSQL_USERNAME="user"
MYSQL_PASSWORD="=password"
#
# We perform a simple query that should return a few results :-p
ERROR_MSG=`/usr/bin/mysql --host=$MYSQL_HOST --port=$MYSQL_PORT --
user=$MYSQL_USERNAME --password=$MYSQL_PASSWORD -e "show databases;"
2>/dev/null`
#
# Check the output. If it is not empty then everything is fine and we
return
# something. Else, we just do not return anything.
#
if [ "$ERROR_MSG" != "" ]
then
# mysql is fine, return http 200
/bin/echo -e "HTTP/1.1 200 OK\r\n"
/bin/echo -e "Content-Type: Content-Type: text/plain\r\n"
/bin/echo -e "\r\n"
/bin/echo -e "MySQL is running.\r\n"
/bin/echo -e "\r\n"
else
# mysql is not fine, return http 503
/bin/echo -e "HTTP/1.1 503 Service Unavailable\r\n"
/bin/echo -e "Content-Type: Content-Type: text/plain\r\n"
/bin/echo -e "\r\n"
/bin/echo -e "MySQL is *down*.\r\n"
/bin/echo -e "\r\n"
fi
What's returned when I curl:
curl -v http://ip:9201/
* Trying ip...
* TCP_NODELAY set
* Connected to ip (ip) port 9201 (#0)
> GET / HTTP/1.1
> Host: ip:9201
> User-Agent: curl/7.58.0
> Accept: */*
>
* Recv failure: Connection reset by peer
* stopped the pause stream!
* Closing connection 0
curl: (56) Recv failure: Connection reset by peer
I'm not sure if the issue is because I'm just responding incorrectly to the request or?
EDIT: Running the bash script manually returns the right output based on if it can connect or not. I don't think there's a syntactical error in the bash itself, just the way it responds to an http request possibly.