Automatic Load Balancing Of Spring Boot Micro Services In Docker With Haystack

In Spring Boot Micro Services In Docker we saw how to create a Docker container from a Spring Boot Micro Service. In this article we shall look at load balancing the service using Haystack. Haystack is a DNS based load balancer that is integrated with the Docker API automatically creating service groups as containers stop and start.

Haystack monitors the Docker API using a tcp socket. In this case the Docker API is on port 2375 on the Docker host.

docker run \
  -e DOCKER_HOST=tcp://172.16.1.218:2375 \
  --name haystack \
   --detach \
   shortishly/haystack

Build a gsa image following the instructions in this article, starting 3 instances of our Spring Boot Micro Service:

docker run --name srv-001 -d gsa
docker run --name srv-002 -d gsa
docker run --name srv-003 -d gsa

We can confirm that we have 3 gsa services by running docker ps as follows:

$ docker ps -a --format="{{.Names}}"

srv-003
srv-002
srv-001

Haystack automatically creates a service group srv.gsa.services.haystack in DNS. Starting a new gsa container will automatically add it to the service group. Stopping a gsa container automatically will remove it from the service group.

Startup a busy box instance using Haystack’s embedded DNS:

docker run \
  --dns=$(docker inspect --format='{{.NetworkSettings.IPAddress}}' haystack) \
  --tty \
  --interactive \
  --rm busybox /bin/sh

Lookup srv.gsa.services.haystack in DNS:

nslookup srv.gsa.services.haystack

Server:    172.17.0.7
Address 1: 172.17.0.7 cb1i9a6.containers.haystack

Name:      srv.gsa.services.haystack
Address 1: 172.17.0.7 cb1i9a6.containers.haystack

Note that srv.gsa.services.haystack is actually pointing to the Haystack container. This is because Haystack acts as a proxy to HTTP requests, automatically load balancing requests randomly over members of the service group. Issue a wget to the service group and the requests will be load balanced randomly over the members:

# wget -q -O /dev/stdout http://srv.gsa.services.haystack/hello-world
{"id":1,"content":"Hello, Stranger!"}

# wget -q -O /dev/stdout http://srv.gsa.services.haystack/hello-world
{"id":1,"content":"Hello, Stranger!"}

# wget -q -O /dev/stdout http://srv.gsa.services.haystack/hello-world
{"id":2,"content":"Hello, Stranger!"}

# wget -q -O /dev/stdout http://srv.gsa.services.haystack/hello-world
{"id":1,"content":"Hello, Stranger!"}

# wget -q -O /dev/stdout http://srv.gsa.services.haystack/hello-world
{"id":3,"content":"Hello, Stranger!"}

# wget -q -O /dev/stdout http://srv.gsa.services.haystack/hello-world
{"id":2,"content":"Hello, Stranger!"}

# wget -q -O /dev/stdout http://srv.gsa.services.haystack/hello-world
{"id":4,"content":"Hello, Stranger!"}

# wget -q -O /dev/stdout http://srv.gsa.services.haystack/hello-world
{"id":3,"content":"Hello, Stranger!"}

# wget -q -O /dev/stdout http://srv.gsa.services.haystack/hello-world
{"id":4,"content":"Hello, Stranger!"}

# wget -q -O /dev/stdout http://srv.gsa.services.haystack/hello-world
{"id":5,"content":"Hello, Stranger!"}

Congratulations! You now have a Spring Boot Micro Service Docker container that is automatically being load balanced by Haystack. You can add or remove further gsa services and Haystack will automatically update the service pool.

Leave a Reply

Your email address will not be published. Required fields are marked *