Header RSS Feed
 
If you only want to see the articles of a certain category, please click on the desired category below:
ALL Android Backup BSD Database Hacks Hardware Internet Linux Mail MySQL Monitoring Network Personal PHP Proxy Shell Solaris Unix Virtualization VMware Windows Wyse

HAProxy: Make httpchk on different port than application
Thursday - Jun 11th 2015 - by - (1 comments)

Between an nginx web servers and its php-fpm upstream servers I wanted to add an additional check if php-fpm and mysql is running correctly on the upstream servers.

The current setup looks like this:

HTTP Request -> Nginx
                 |------> tcp/9000 on PHP Upstream 1 -> local mysql
                 |------> tcp/9000 on PHP Upstream 2 -> local mysql
                 |------> tcp/9000 on PHP Upstream 3 -> local mysql

 The problem is, that Nginx does not have a check function for its upstreams (yet in the open source version - but this seems to come soon).

In order to perform a check on the PHP+MySQL functionality, I placed a HAProxy in between:

HTTP Request -> Nginx
                 | -> tcp/9000 on localhost HAProxy
                                             |------> tcp/9000 on PHP Upstream 1 -> local mysql
                                             |------> tcp/9000 on PHP Upstream 2 -> local mysql
                                             |------> tcp/9000 on PHP Upstream 3 -> local mysql

Now how can I check the functionality of the PHP-FPM upstream servers? For this I installed nginx on the upstream servers, just for the purpose of local monitoring. The .php location (suffixes) are sent to the localhost php-fpm instance.
I wrote a check script in PHP which connects to the local MySQL instance and makes a write and a read operation, just to make sure everything works. If the check script succeeds, it echos "PHP and MySQL up".

Given that I have now nginx and the working check script on the upstream servers, how can I add this check in HAProxy?
A standard HAProxy configuration (which you will see in almost all tutorials) for a backend server looks like this:

listen phpfpm 0.0.0.0:9000
  balance roundrobin
  mode tcp
  option  tcplog
  server phpupstream1 192.168.1.101:9000 check
  server phpupstream2 192.168.1.102:9000 check

The "check" option in the "server" definition just checks if port 9000 is available on the given server. With that alone I'm not able to execute the check script. I need to do my check via http to nginx on the upstream server, therefore I need to use port 80 for the check - but for the check only.
Great softwares offer great solutions. And HAProxy is great and the solution is there: It's simply called "port" and is part of the "server" syntax. It took me a while to figure out that "check" and "port" go together so it ist actually best described as "check port".

By adding "check port" to the server syntax, I am now able to perform checks on port 80 while the application passes the normal requests to port 9000:

listen phpfpm 0.0.0.0:9000
  balance roundrobin
  mode tcp
  option  tcplog
  server phpupstream1 192.168.1.101:9000 check port 80
  server phpupstream2 192.168.1.102:9000 check port 80

But what about my php check script? Here I use httpchk to call the script and await the string "PHP and MySQL up":

listen phpfpm 0.0.0.0:9000
  balance roundrobin
  mode tcp
  option  tcplog
  option httpchk GET /dbcheck.php HTTP/1.1\r\nHost:\ monitoring\r\nConnection:\ close
  http-check expect string PHP and MySQL up
  server phpupstream1 192.168.1.101:9000 check port 80
  server phpupstream2 192.168.1.102:9000 check port 80

Here we go. Now HAProxy does not only check a running tcp port of php-fpm, but rather a full stack of a working php-fpm daemon and mysql server.

 

Add a comment

Show form to leave a comment

Comments (newest first):

Saju Joseph wrote on Oct 28th, 2017:
Hi,
Thanks for explaining "check port" part. I actually had a situation where traffic should be routed if two ports are healthy. I guess with check port, only the port specified will be checked and not the port part of the hostname:port string.

Well, I employed your httpchk method to solve it.

Thanks for explaining the details with examples.


Go to Homepage home
Linux Howtos how to's
Monitoring Plugins monitoring plugins
Links links

Valid HTML 4.01 Transitional
Valid CSS!
[Valid RSS]

7367 Days
until Death of Computers
Why?