Linux shell implements HTTP service

  
 

Requirement scenario


Using the proxy server HAProxy to load balance Mysql is a common solution. To improve the availability, when there is a problem with Mysql, such as server failure, or data replication Interrupted, it is best to let HAProxy know immediately, and then stop forwarding requests to it

HAProxy How do I know if there is a problem with Mysql?

Solutions

(1) Write a shell script to check the state of mysql, and then output the result, for example, when the status is normal, return status code 200 and correct information, otherwise return status code 503 and Error message

(2) Implement an HTTP service. After the request is connected, call the above check script and return the check result.

(3) HAProxy accesses the HTTP service according to the returned result information. To determine if this mysql is available

How to quickly and easily implement an HTTP service that can call shell scripts?

The more general solution is xinetd

xinetd is a Linux daemon, fully called extended interent daemon, extended network daemon

xinetd can open a port and wait for connection You can tell xinetd which script to run. When a connection comes in, xinetd will execute the script and then directly return the contents of the script output

HAProxy -> xinetd -> mysql-check script, HAProxy will get MySQL status information


xinetd configuration case


The following implementation of a simple xinetd example, open 9200 port, return a test script Output content

If there is no xinetd on the machine, install it first. Under centos7, you can use the command yum install xinetd

(1) test script

#!/bin/bash

echo `uptime

Copyright © Windows knowledge All Rights Reserved