1. Knowledge Base
  2. Martini
  3. Configuration and Deployment

How configure a Fabio load balancer for Consul and Martini

Fabio is a fast, modern, zero-conf load-balancing HTTP(S) and TCP router for deploying applications managed by Consul.  Register your services in Consul, provide a health check and Fabio will start routing traffic to them. No configuration required.

Note: These instructions apply to Ubuntu Linux. The steps will vary depending on the operating system you are using but the principles remain the same.

Prerequisites

  1. A Consul Cluster with working leader-voter entity (a minimum of 3 servers)
  2. Open these ports in the firewall (ufw), you can do this by running this command:
sudo ufw allow 9999
sudo ufw allow 9998

Installing and Configure Fabio Load Balancer

To demonstrate Fabio as a fast, zero-configuration load balancer, this documentation will focus on running Fabio at default values. Alternatively, if you opt to use different ports or set your own configuration for Fabio, you can check their detailed documentation here.


First, change the directory to /usr/local/bin and download the latest binary.

cd /user/local/bin
sudo wget -O fabio https://github.com/fabiolb/fabio/releases/download/v1.6.2/fabio-1.6.2-linux_amd64

Download the latest Fabio binary



Give the appropriate permissions to Fabio and create a Fabio startup script.

sudo chmod 755 fabio
sudo nano /etc/systemd/system/fabio.service

Fabio startup script


Enable the fabio.service, reload system daemon, and start the service. Verify the status of fabio.

sudo systemctl enable fabio
sudo systemctl daemon-reload
sudo systemctl start fabio
sudo systemctl status fabio

 

Once Fabio is verified running, you can see Fabio in the Consul Admin UI under services. To access the Fabio Admin UI in your browser, enter the IP:port ${localhost/ip address}:9998 or the custom value that you defined if you choose to create dedicated properties files for Fabio.

Fabio admin UI

Using Health Checks and URLprefixes

Register a health check in Consul as described here. Make sure the health check is passing since Fabio will only watch services with a passing health check.


Here’s an example of a health check you can use

TCP health check


You can see the status on the Consul Admin UI under ${node name} > health-checks whether is passed or not.

Health check on Consul services

Next is to add a corresponding urlprefix in conjunction with the health check you’ve used. Copy the format as shown in the image below then save for the changes to take effect.

Modified health check with urlprefix tag


For reference, here are some of the urlprefixes available:

Sample urlprefix uses


To refresh Consul without restarting it by running this command.

consul reload 


As shown in the image below, Fabio successfully established a routing table with the urlprefix “/” as the source. Basically, what it does is distribute all the incoming traffic to all of the servers in the routing table.

Fabio Routing Table

How requests are routed by Fabio

At default, Fabio listens on port 9999, you can also change this by using custom values by creating dedicated properties files for Fabio. Each time you request localhost:9999, Fabio will redirect you to one of the available servers in the routing table. The default routing selection is executed randomly but it can also be configured as round robin. 

Redirected traffic using urlprefix tag


You can effectively use Fabio by creating routing tables for specific services using the urlprefix tag. For example, you can add a dedicated routing table for /api or /api/appname instead of using the generic “/” urlprefix. Then each time a request is received on that specific urlprefix, Fabio will route it on any of the available servers in the routing table.

Fabio routing table with additional entries for /api



In an event that a server becomes unavailable, all incoming traffic will be distributed to the remaining servers. This can also be viewed in the Fabio admin UI

Updated routing table with 2 remaining servers