This is a quick guide on how to integrate Consul, Registrator and Fabio.
Goal: To automate domain/host mapping to internal container services whenever we make a new deployment.
Requirement: Basic understanding of what these technologies do.
Here’s some quick info in case you have no idea what these are:
Consul. This is like a DNS server but specially made for container services.
Registrator. Automates service registration to Consul. Consul has no builtin ability to detect whenever a new service has been deployed, it’s why a registrator is still needed.
Fabio. Act as the router which uses Consul information to properly direct routes. Normally we use Nginx or HAProxy to direct routes to container services, but since they have no builtin integration with Consul, we have to use something like Fabio to do this.
First, let’s edit the following file.
Place the following content.
DOCKER_OPTS="--dns 172.17.0.1 --dns 18.104.22.168 --dns-search service.consul"
Start the Consul server.
docker run -d -p 8500:8500 -p 172.17.0.1:53:8600/udp -p 8400:8400 gliderlabs/consul-server -node myconsul -bootstrap
Start the Registrator.
docker run -d -v /var/run/docker.sock:/tmp/docker.sock --net=host gliderlabs/registrator -internal consul://localhost:8500
Deploy a simple Nginx server.
docker run -d -p :80 -e "SERVICE_80_NAME=web" -e SERVICE_TAGS=urlprefix-/ -e "SERVICE_80_ID=http1" -e "SERVICE_80_CHECK_HTTP=true" -e "SERVICE_80_CHECK_HTTP=/" --name=nginx1 nginx
Let’s breakdown what these parameters are for.
SERVICE_80_NAME — we named the service as “web” and if we deploy another Nginx server using the same name, Consul will automatically load balance the two instances.
SERVICE_80_ID — this id should be unique for every deployment.
SERVICE_TAGS — the value here is very important for Fabio. Fabio monitors the tags used in the Consul services, and when it detects the urlprefix keyword, it will automatically add a route for that service. The value urlprefix-/ tells Fabio that our trigger route is for
SERVICE_80_CHECK_HTTP & SERVICE_80_CHECK_HTTP — these are also important for Fabio since it only add routes for services that pass the health check. Not adding these parameters will fail the integration.
Let’s start Fabio, but first we need to modify some properties.
Create a file with the following name.
Place this setting.
registry.consul.addr = <consul ip>:8500
docker run -d -p 80:9999 -p 9998:9998 -v $PWD/fabio.properties:/etc/fabio/fabio.properties fabiolb/fabio
Try to deploy another Nginx server.
docker run -d -p :80 -e "SERVICE_80_NAME=web" -e SERVICE_TAGS=urlprefix-/ -e "SERVICE_80_ID=http2" -e "SERVICE_80_CHECK_HTTP=true" -e "SERVICE_80_CHECK_HTTP=/" --name=nginx2 nginx
Check Fabio on the browser.
You should be able to see the route has beed added automatically.
You can now test if your Fabio router is working by visiting your server.
If you use the urlprefix-/foo, then you can test it by going to http://example.com/foo
Alternatives to Fabio:
To be continued…