Introduction
Fabio is a modern, self-configuring load balancer and proxy server for TCP and HTTP-based applications. It seamlessly integrates with Consul, dynamically updating its routing table in response to service changes without requiring restarts or manual configuration. This makes Fabio ideal for distributing incoming HTTP(S) and TCP traffic to your frontend services with ease. Simply register your services in Consul with appropriate urlprefix-
tags, and Fabio will handle the rest.
Expected Outcome
By combining Fabio with Consul for service discovery and dynamic configuration, you can expect:
- Automated Service Discovery: Consul automatically detects and registers your services, providing Fabio with real-time information about their availability and location.
-
Simplified Service Registration: Register your services in Consul using simple
urlprefix-
tags to define routing paths, and Fabio will automatically handle traffic distribution. - Dynamic Routing Updates: Fabio leverages Consul's service discovery data to dynamically update its routing tables, ensuring traffic is always directed to healthy service instances.
- Zero Downtime Reloads: Changes in Consul automatically trigger updates in Fabio without requiring restarts or manual intervention, ensuring continuous availability for your applications.
- Centralized Configuration: Manage your entire service infrastructure through Consul, providing a single source of truth for service discovery and configuration.
This powerful combination simplifies load balancing, enhances service resilience, and enables you to build a more dynamic and scalable infrastructure.
Prerequisites
Before you begin, ensure you have the following:
- Consul with CLI access: A running Consul cluster.
- Fabio: Fabio installed on your system. You can find installation instructions in the Fabio quickstart guide
-
Backend service: A backend service running and accessible. This guide uses a simple
counting-service
as an example. You can download or compile it as described in the guide. - Basic understanding of Consul service registration and health checks: Familiarity with how to register services and configure health checks in Consul will be helpful.
-
jq: The
jq
command-line JSON processor installed. This is used to format the output of Consul API calls.
Use Case
This guide is ideal for developers and operators who want to leverage Consul for service discovery and Fabio for dynamic load balancing. You'll learn how to register services in Consul, configure health checks, and integrate Fabio to automatically route traffic to healthy service instances.
Procedure
-
Start a Backend Service
- Download the Counting Service:
-
Linux: Download the
counting-service
binary from here. -
Darwin: Compile the
counting-service
from the this source code.
-
Linux: Download the
- Run the Counting Service:
$ ./counting-service
- This will start the service, accessible at
http://<the ip of the host where the service is running>:<port configured to run the service>
. For this example, we'll usehttp://172.20.20.28:5000
.
- This will start the service, accessible at
- Download the Counting Service:
-
Register the Service and Health Check with Consul
- Create a Service Definition File (e.g.,
counting.json
):{ "service": { "name": "counting", "id": "counting-1", "port": 5000, "check": { "http": "http://localhost:5000/health", "method": "GET", "interval": "1s", "timeout": "1s" } } }
- Register the Service:
$ consul services register counting.json
- Verify the Health Check:
$ curl -s http://localhost:8500/v1/health/checks/counting | jq .
- Ensure the output shows a "passing" status for the health check.
- Create a Service Definition File (e.g.,
-
Start Fabio
- Start Fabio:
$ fabio
- Verify Fabio Registration:
$ consul catalog services
- You should see
Fabio
listed among the services.
- You should see
-
Check Fabio's Health: Access
http://<IP address of your Fabio host>:9998/health
in your browser (e.g.,http://172.20.20.28:9998/health
).
- Start Fabio:
-
Configure Fabio to Monitor the Service
- Add a
urlprefix-
Tag: In thecounting.json
file, add atags
field with aurlprefix-
tag to announce the service path:{ "service": { "name": "counting", "tags": ["urlprefix-/foo"], "id": "counting-1", "port": 5000, "check": { "http": "http://localhost:5000/health", "method": "GET", "interval": "1s", "timeout": "1s" } } }
- Re-register the Service:
$ consul services register counting.json
- Test Access via Fabio:
$ curl -i http://localhost:9999/foo
- You should get a successful response from the counting service.
- Add a
-
Verify Service in Consul and Fabio UI
- Consul UI: Check the Consul UI to see the registered service.
- Fabio UI: Access
http://<IP address of your Fabio host>:9998/routes
(e.g.,http://172.20.20.28:9998/routes
) to see the routing table in Fabio.
-
Check Fabio's Health in Consul
- Use Consul's Health API:
$ curl -s http://localhost:8500/v1/health/checks/fabio | jq .
- The output should show "passing" status for Fabio's health checks.
- Use Consul's Health API:
-
Stop the Backend Service
- Stop the Counting Service:
$ ps -ef | grep counting $ kill -9 <process ID of counting-service>
- Verify Service Failure in Consul:
$ curl -s http://localhost:8500/v1/health/checks/counting | jq .
- The output should show a "critical" status.
- Verify Fabio's Response:
$ curl -i http://localhost:9999/foo
- You should get a
404 Not Found
error, as Fabio will have removed the unhealthy service from its routing table.
- You should get a
- Stop the Counting Service:
Additional Information
- Fabio: A Stateless Load Balancer Presentation
- Consul Tutorial: Monitor your application health with distributed checks
- GitHub for Fabio
- Consul Tutorial: Register your services to Consul
- Fabio Quickstart