TCP load balancing
By default, dockercloud/haproxy runs in http mode. If you want a linked service to run in a tcp mode, you can specify the environment variable TCP_PORTS, which is a comma separated ports(e.g. 9000, 9001).
For example, if you run:
docker --name app-1 --expose 9000 --expose 9001 -e TCP_PORTS="9000, 9001" your_app
docker --name app-2 --expose 9000 --expose 9001 -e TCP_PORTS="9000, 9001" your_app
docker run --link app-1:app-1 --link app-2:app-2 -p 9000:9000, 9001:9001 dockercloud/haproxy
Then, haproxy balances the load between app-1 and app-2 in both port 9000 and 9001 respectively.
Moreover, If you have more exposed ports than TCP_PORTS, the rest of the ports will be balancing using http mode.
For example, if you run:
docker --name app-1 --expose 80 --expose 22 -e TCP_PORTS=22 your_app
docker --name app-2 --expose 80 --expose 22 -e TCP_PORTS=22 your_app
docker run --link app-1:app-2 --link app-2:app-2 -p 80:80 -p 22:22 dockercloud/haproxy
Then, haproxy balances in http mode at port 80 and balances in tcp on port at port 22.
In this way, you can do the load balancing both in tcp and in http at the same time.
In TCP_PORTS, if you set port that ends with '/ssl', for example 2222/ssl, HAProxy will set ssl termination on port 2222.
Note:
- You are able to set
VIRTUAL_HOSTandTCP_PORTSat the same them, giving more control onhttpmode. - Be careful that, the load balancing on
tcpport is applied to all the services. If you link two(or more) different services using the sameTCP_PORTS,dockercloud/haproxyconsiders them coming from the same service.