Apache http server with mod_jk and dynamic loadbalacing of new containers
An apache http server that demonstrates how new tomcat instances are included into mod_jk loadbalancing on the fly.
Apache has a virtual host that is bound to a tomcat cluster via mod_jk.
The cluster is defined in a workers.properties file. This file is refreshed, if new tomcat instances for that cluster are stopped or started.
The apache server relies on a running consul instance and the registrator from progrium.
A tomcat instance are started with additional information so that registrator is able to store this information in consul.
To identify a new container as a member of the tomcat-cluster the container is started with
-e "SERVICE_TAGS=tomcat-service"(convenience script provided).
The name of the container is set with
--name $JVM\_ROUTE. Where JVM_ROUTE = "tomcat_HOSTNAME_PORT".
Tomcat is started with
This identifies to which service this instance belongs. Tomcat instances of one service are load balanced.
tomcats jvm route is set with
Registrator picks up this information when a new container starts and adds the information to consul.
The apache container start a go program that reads the information of the new tomcat instance from consul, updates its workers.properties file and restarts the webserver.
This is a very simple loadbalancing solution for docker and you can achieve much more sophisticated solutions with kubernates, mesos, ....
But if you´d like to have a look at how little code is neccessary to listen to events and adopt the workers.properties file, please check out
To start experimenting:
This substitutes the Placeholder HOST_IP and CURDIR in the file docker-compose.yml-template with your real hostip and current Path (necessary for the registrator to find the consul instance.
I haven´t been able to find a better solution, I would greatly appreciate any suggestion for impovement.
docker-compose upif you have docker-compose installed (TODO, write a script to start the containers with bash)
- Point your Browser to
http://YOURIP:49132/jkstatuswhere YOURIP could be 127.0.0.1 when running linux or the IP of your docker daemon if you are running boot2docker.
You should see an empty jkstatus page, as no tomcat isntances have been started yet.
./runTomcatInstance.sha few time to start a new tomcat instances in a docker container. The tomcat instances are started with a sample web-application that displays the container ip and allows to create/destroy a session.
- Reload http://YOURIP:49132/jkstatus in your browser to see how the modjk loadbalancer has dynamically included the new instances into the loadbalancing.
- Enter the url http://YOURIP:49132/Dude/DudeServlet in your browser to access the webapplication.
As long as you don´t login, reloading or pressing the noop button should direct you to different servers in a round robin fashion (at least if you have created >1 tomcat instances). Creating more tomcat instances increases the range of servers, that will display their ip address.
As soon as you login, you should stick to one server if you reload the page or press the noop-button.
If you logout, the browser still sends the JSESSIONID-header and unfortunately, you are stuck with the server you worked with. If you delete the cookie in the browser, round robin works again.