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.
Saju Joseph from 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.
AWS Android Ansible Apache Apple Atlassian BSD Backup Bash Bluecoat CMS Chef Cloud Coding Consul Containers CouchDB DB DNS Database Databases Docker ELK Elasticsearch Filebeat FreeBSD Galera Git GlusterFS Grafana Graphics HAProxy HTML Hacks Hardware Icinga Influx Internet Java KVM Kibana Kodi Kubernetes LVM LXC Linux Logstash Mac Macintosh Mail MariaDB Minio MongoDB Monitoring Multimedia MySQL NFS Nagios Network Nginx OSSEC OTRS Office PGSQL PHP Perl Personal PostgreSQL Postgres PowerDNS Proxmox Proxy Python Rancher Rant Redis Roundcube SSL Samba Seafile Security Shell SmartOS Solaris Surveillance Systemd TLS Tomcat Ubuntu Unix VMWare VMware Varnish Virtualization Windows Wireless Wordpress Wyse ZFS Zoneminder