Use internal docker network when using HAProxy loadbalancer

A common setup is to use HAProxy as a load balancer behind two or more web servers.
In a docker test environment, you can first run the two web servers and finally run the HAProxy docker, like this:

docker run -d -it --name apache1 xxxx/apache
docker run -d -it --name apache2 xxxx/apache
docker run -d -it -p 80:80 --link apache1:apache1 --link apache2:apache2 --name haproxy xxxx/haproxy

Within  the haproxy, you can use apache1 and apache2 to forward the web requests to.

This setup has a problems however.
When apache2 will not start for some reason, you cannot start the haproxy container because it expects a running container with the name apache2.

This can be solved by using the internal network to forward the web requests.
The internal network is typically 172.17.42.1 and can be used by all docker containers.

We can configure the two apache servers to use e.g. port 81 and 82 on the internal network like this:

docker run -d -it -p 172.17.42.1:81:80 –name apache1 xxxx/apache
docker run -d -it -p 172.17.42.1:82:80 –name apache2 xxxx/apache

Now haproxy can acces the web servers at 172.17.41.1:81 and 172.17.41.1:81 without any configuration.

So we can start haproxy like this (without –link):

docker run -d -it -p 80:80 --name haproxy xxxx/haproxy

 

This way we can start haproxy when when one of the apache containers are not running.

This method can also be used when two containers needs to able be access each other (e.g. when you would setup a master-master mysql replication).

Geef een reactie

Gelieve met een van deze methodes in te loggen om je reactie te plaatsen:

WordPress.com logo

Je reageert onder je WordPress.com account. Log uit /  Bijwerken )

Google+ photo

Je reageert onder je Google+ account. Log uit /  Bijwerken )

Twitter-afbeelding

Je reageert onder je Twitter account. Log uit /  Bijwerken )

Facebook foto

Je reageert onder je Facebook account. Log uit /  Bijwerken )

Verbinden met %s