Nginx config for external load balancer with swarm

Hello,

The Docker documentation provides a small sample on how to configure HAproxy as external load balancer for a HTTP service running in 3 nodes in a Docker swarm here:

Does anyone have a sample file for nginx which would do exactly the same?

The reason is that HAproxy does not do virtual hosts nor support caching so I would rather prefer using nginx.

Cheers,
H.N.

Maybe these docs can help you set up nginx.

Thanks for the example in the Docker documentation. Unfortunately this example is for a specific service (DTR) and the stream config parameter of nginx does not allow you to pass any server_name parameter so it is not possible to use this example for hosting many domains (what apache calls virtual hosts).

Anyone any other example suited for virtual hosting of many websites with nginx as a load balancer in front of a docker swarm with routing mesh?

You have GOT to use @vfarcic’s Docker Flow: Proxy. It does do virtual hosts, supports Docker Services and is awesome. Check it out at http://proxy.dockerflow.com

2 Likes

Thanks for the hint about Docker Flow. Although it sounds promising, it does not use nginx and hence does not feature any caching. I am looking for a simple nginx config for a few websites which is docker swarm compatible.

Maybe stating the obvious here, but https://github.com/jwilder/nginx-proxy any good?

We use it, but not with swarm, which may be why nobody has mentioned it previously.

Again thanks this container sounds very nice but I am simply looking for an nginx example which is docker swarm ready for use on an already existing nginx server.

If I am hearing you correctly, you are wanting an external Nginx server to load balance to different domains in a swarm. If you have each domain on a different external facing port on the swarm and on your external Nginx server point your domains to the correct port in the swarm, the swarm will auto load balance the incoming requests to the scaled containers for that port. On Nginx, you can also load balance your entry points for a port/domain to different nodes on the swarm for high availability.

If the Nginx server is internal to the swarm, it can reverse proxy to container name, and again the swarm auto load balances as scaled.

That’s exactly what I am doing. I want to have at least one external to Docker nginx load balancer in front of my docker web containers (which are running in a Docker Swarm). So from my understanding it is just a matter of using the upstream and server config parameters from nginx, example:

upstream web1 {
  server 10.0.0.1:20001; # docker manager
  server 10.0.0.2:20001; # docker worker 1
  server 10.0.0.3:20001; # docker worker 2
}

So this is all nice and very simple but I would like to also know if there are any best practices for the nginx config for this specific case. For example what fail_timeout and max_fails should I used best? and what LB algorithm from nginx should I use (ip_hash, least_conn, etc…).

Any recommendations here?

Only an educated guess…no matter what LB algorithm you use externally, internally, swarm will use least_conn to load balance in the swarm. So external LB of least_conn would potentially spread equally in the swarm and spread the point of entry processing over the nodes.

We use Traefik. :wink:

https://traefik.io/

Thanks for the tip about traefik.io seems to be exactly what I need but I have the issue that when updating a service (rolling updates) the traefik reverse proxy gives me a Gateway Timeouts whereas it should not in my opinion. I have opened an issue for that here: https://github.com/containous/traefik/issues/1480

1 Like