Using Fabio, the zero-configuration load-balancer

Consul

  • Fabio uses the Consul Service Catalog to dynamically create routes to backend services.
  • Fabio determines the routes based on the tags

Start Consul

  • Using docker containers so we can create a 3-node cluster.
  • The ports from the master server will be shared so they are accessible from the underlying host.
  • The Consul UI will be available at http://localhost:8500/ui
docker run -d --name consul0  \
        -p 8300:8300 \
        -p 8301:8301 \
        -p 8301:8301/udp \
        -p 8302:8302 \
        -p 8302:8302/udp \
        -p 8500:8500 \
        -p 8600:8600 \
        -p 8600:8600/udp \
        consul \
        agent -server \
        -client=0.0.0.0 -ui \
        -bootstrap \
        -log-level warn

docker run -d --name consul1 --link consul0 consul \
       agent -server -client=0.0.0.0 -ui \
             -join consul0 -log-level warn

docker run -d --name consul2 --link consul0 consul \
       agent -server -client=0.0.0.0 -ui \
             -join consul0 -log-level warn

Start a backend web service

  • Hello is a very simple web service that will respond to any request.
docker pull devokun/hello

docker stop hello ; docker rm hello

docker run  -d \
            --name hello \
            \
            -p 6001:6001 \
            -e HTTP_PORT=6001 \
            \
            devokun/hello

curl http://0.0.0.0:6001/

Register the web service with the Consul Service Catalog

##
## Find the Container IP
##

for IDN in $(docker ps --format '{{ .ID }}-{{ .Names }}'); do
  ID=$(echo   "$IDN" | cut -f 1 -d "-")
  NAME=$(echo "$IDN" | cut -f 2 -d "-")
  IP=$(docker inspect ${ID} --format '{{ .NetworkSettings.IPAddress }}')
  echo "${ID} ${IP} ${NAME}"
done

##
## Gotcha
##   Service Address can not be 0.0.0.0
##   The Healthcheck URL must be accessible from the consul service.
##

read -r -d '' REGISTER_SERVICE << EOM
{
  "Name":    "hello",
  "address": "127.0.0.1",
  "port":    6001,
  "tags": ["global", "hello", "urlprefix-hello.gestalts.net/"],
  "checks": [{
     "http":     "http://172.17.0.2:6001/",
     "interval": "5s"
  }]
}
EOM

curl -v -XPUT -d "$REGISTER_SERVICE" http://127.0.0.1:8500/v1/agent/service/register

List Services in Consul

curl http://127.0.0.1:8500/v1/agent/services

curl http://127.0.0.1:8500/v1/agent/services | jq '.hello.ID'

De-Register a Service

SERVICE_ID="hello"
curl -v -XPUT http://127.0.0.1:8500/v1/agent/service/deregister/${SERVICE_ID}

Consule UI

Start Fabio

  • Admin Portal: http://0.0.0.0:9998/health
  • Fabio HTTP proxy listening on :9999
  • Fabio assumes that the consul agent is running on localhost and will connect to it to query the service catalog.
wget https://github.com/fabiolb/fabio/releases/download/v1.5.9/fabio-1.5.9-go1.10.2-linux_amd64

linux_amd64 /usr/local/sbin/fabio

chmod 0755 /usr/local/sbin/fabio

fabio

Fabio UI

categories: fabio | docker | consul | reverse-proxy | devops |