Shane Rainville
I.T. professional with over a decade of experience, ranging from application development to system & infrastructure administration. He's worked with small startups to large corporate companies, using unique and creative solutions to solve problems.

Overview

Ensuring the backend servers HAProxy is forwarding your users’ requests to are healthy is important. How you check for health is based on the type of service hosted in the backend. Web applications need to be checked differently from database servers. In this tutorial I will show you different ways of doing HAProxy health checks so to help maintain a great user experience.

 

TCP Checks

The standard and most basic check is a TCP check. It merely sends out a simple packet and waits for a reply back from the destination server. This verifies that the network interface on the host being checked is online. What it won’t do is tell whether the service being hosted by the backend server is healthy.

Web Server

The following is an example of a simple web server health check. It consists of two servers that provide a web service to users.

listen myapp2 0.0.0.0:8080
  mode http
  server node1 10.0.0.1:80 check fall 3 rise 2
  server node2 10.0.0.2:80 check fall 3 rise 2
 
TABLE1Check parameters
Check This instructs HAProxy to perform health checks on the node.
Fall 3 Instructs HAProxy to offline the node if 3 consecutive health check failures occur. The value can be set to any number.
Rise 2 If the node is marked offline due to failed health checks, this instructs HAProxy to not mark the node online unless it has two consecutive successful health checks. The value can be set to any number.

 

Network Service

Not every balanced service is a web application. You may also balance network services, such as email servers or caching servers (redis, memcached).
The following is an example of a simple web server health check. It consists of two servers that provide a web service to users.

Mail Server Health Checks

listen smtpcluster1 0.0.0.0:8080
  mode tcp
  server smtp1 10.0.0.1:25 check fall 3 rise 2
  server smtp2 10.0.0.2:25 check fall 3 rise 2
 

Memcached Cluster

listen memcachedcluster 0.0.0.0:8080
  mode tcp
  server memcached1 10.0.0.1:11211 check fall 3 rise 2
  server memcached2 10.0.0.2:11211 check fall 3 rise 2
 

Redis Cluster

listen rediscluster 0.0.0.0:8080
  mode tcp
  server redis1 10.0.0.1:6379 check fall 3 rise 2
  server redis2 10.0.0.2:6379 check fall 3 rise 2
 

 

HTTP Checks

Whereas the TCP check only validates that a network connection can be made, a HTTP check is used when you want to verify that a specific website URL is healthy. If it returns a status 200 or 300 response, everything is good. Anything above that, such as a 500 status repsonse, will be considered bad health and HAProxy will mark the backend server as offline.

Example 1: This check will use use a HEAD request against the index page of your domain using HTTP version 1.0.

listen myapp2 0.0.0.0:8080
  mode http
  option httpchk HEAD / HTTP/1.0
  server node1 10.0.0.1:80 check fall 3 rise 2
  server node2 10.0.0.2:80 check fall 3 rise 2
 

Example 2: This check will use a HEAD request against a specific URL using HTTP version 1.1. It also sets the domain name, which is needed for backends that use virtual hosts.

listen myapp2 0.0.0.0:8080
  mode http
  option httpchk HEAD /health_check.php HTTP/1.1\r\nHost:\ serverlab.com
  server node1 10.0.0.1:80 check fall 3 rise 2
  server node2 10.0.0.2:80 check fall 3 rise 2
 

 

MYSQL Checks

Preparing MySQL

HAProxy requires access to your MySQL server to perform its checks. You will need to create two accounts on the MySQL server for HAProxy. One will be a basic user and the second a user with administrative rights.

  1. Log onto the MySQL server.
  2. Log into MySQL using an account with administrative rights.
    mysql -u root -p
  3. Create the user HAProxy will use to perform checks.
    INSERT INTO mysql.user (Host,User) values ('10.0.0.10','haproxy_check');
  4. Flush privileges to apply new ones.
    FLUSH PRIVILEGES;
  5. Exit out of MySQL console.

 

Install MySQL Client on HAProxy Server

HAProxy requires the MySQL client to be installed before it can communicate with the MySQL servers.

Red Hat \ CentOS

sudo yum install mysql-client

Ubuntu \ Debian

sudo apt-get install mysql-client

 

Configuring HAProxy to Check MySQL

listen mysql-cluster
  mode tcp
  option mysql-check user haproxy_check
  balance roundrobin
  server mysql1 10.0.0.1:3306 check
  server mysql2 10.0.0.2:3306 check
 

© 2014 Shane Rainville