Linux Shell Implementation HTTP Service sample code _linux shell

Source: Internet
Author: User
Tags haproxy

First, the preface

Using a proxy server haproxy load balancing for MySQL is a common scenario, to improve availability, when a MySQL problem occurs, such as a server failure, or data replication is interrupted, it is best to let haproxy know immediately, and then stop forwarding requests to it

Haproxy How do you know if Mysql is a problem?

Second, the solution to the idea

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

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

(3) Haproxy accesses this HTTP service to determine whether the MySQL is available based on the returned result information

How to quickly implement an HTTP service that can invoke a shell script?

The more common solution is xinetd.

XINETD is the daemon of Linux, all called Extended interent daemon, extended Network daemon

XINETD can open a port, wait for a connection, and you can tell xinetd which script to run, and when a connection comes in, XINETD executes the script and returns the contents of the script output directly.

HAProxy -> xinetd -> mysql-checkScript, Haproxy Gets the status information of MySQL

Configuration Case for xinetd

Let's implement a simple xinetd example, open port 9200, and return the output of a test script

If there is no xinetd on the machine, install it first, CENTOS7 can use the commandyum install xinetd

(1) Test script

#!/bin/bash

echo ' uptime | egrep-o ' Up ([0-9]+] days ' | awk ' {print $} '

This script is used to output the number of days online for the current server

chmod +x to add executable permissions to a script

(2) Writing xinetd service

For example, the service name, helloworld file name /etc/xinetd.d/helloworld , content is

Service HelloWorld

{

disable = no

port  = 9200

socket_type   = Stream

Protocol    = TCP

Wait      = no

user      = root

Server     =/root/test.sh

Server_args   = Test

}

port Specify the port on which to listen

server Specify the script to execute

(3) Join the list of services

Modify/etc/services

Find the location that defines Port 9200, comment out the original, add the helloworld service

HelloWorld   9200/tcp

#wap-wsp     9200/tcp

#wap-wsp     9200/udp

(4) Restart xinetd

The restart command under CentOS is:

Systemctl Restart Xinetd.service

(5) test

Test with nc telnet two commands, respectively.

NC localhost 9200


telnet localhost 9200


Can correctly output script execution, complete the sample

Summarize

Linux shell Implementation of the content of HTTP services to this is basically the end of the hope for everyone's work and learning can help, if there are questions you can leave a message to discuss.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.