In this project there is a simple rest backend which will be running inside docker container.
We’ll have multiple instances of this application and we’ll be using HAProxy for load balancing for the backend apps.

The network diagram is as follows

LBDiagram

The project source code can be found at this github link.

The backend app is a single rest controller (/hello) which generates json response with hello message.

In the controller, environment value of message is being used. This variable will be set by docker commands later. It’s just for confirmation of load balancing.

After running mvn package in this project folder, helloserver.jar file will be generated inside the target folder. This jar will be copied to docker image.
This is the Dockerfile of spring boot application.

It’s copying the jar, setting the port to be exposed and setting the run command of the application.

This folder contains the Dockerfile and haproxy.cfg files. We’ll be building an image for haproxy with our custom configuration of docker network.

This is the Dockerfile for haproxy

This will be building a new image with the copy of haproxy.cfg with the haproxy:1.5 image.

This is the config file

bind *:80 – this is setting public network port for haproxy
server server1 hello1:8080 check – server is config keyword. server1 is the internal name . hello1:8080 is the address and port of the backend server. check is for the health check for this server
server server2 hello2:8080 check – same config for server 2 which will be called as hello2

hello1:8080 and hello2:8080 will be resolved by docker’s networking capability.

here’s how to build images
This is for the spring boot image
\helloserver> docker build -t hello/server .

and this is for the haproxy
\helloserver\haproxy> docker build -t hello/haproxy .
You should be seeing these images in your docker image list
\helloserver\haproxy> docker images

REPOSITORY TAG IMAGE ID CREATED SIZE
 hello/haproxy latest c8d6f80893a7 9 seconds ago 136.2 MB
 hello/server latest e589ccbd5a65 52 seconds ago 195.5 MB

Create a custom network for docker
> docker network create hellonetwork

And run the images.
Since the haproxy is configured with servernames (hello1, etc) during initialization, it’ll try to discover those devices. But if there is no running device with name “hello1” in docker, haproxy will not find it and fail to start. To eliminate this problem, run the haproxy after the backend servers are started.

start the backend instance
> docker run -d --net hellonetwork -e message=Server1 --name hello1 hello/server
-d – the daemon mode
--net – setting the net to be used for that image (hellonetwork)
-e – setting environment variable (message=Server1) – rest controller will be reading this parameter
--name – setting the name for this image (hello1)

start another instance from same image with different name and message
> docker run -d --net hellonetwork -e message=Server2 --name hello2 hello/server

since both of the backend servers are up we can start the haproxy

> docker run -d --net hellonetwork -p 80:80 --name haproxy hello/haproxy
-p – the docker will expose the internal port 80 to external 80, so we’ll access to haproxy through 80

the environment is ready to test
> docker-machine ip
192.168.99.100

Open a browser and go to http://192.168.99.100/hello . Refresh it couple of times. You’ll see the response will be {“content”:”hello from server Server1″} or {“content”:”hello from server Server2″}

Thanks