Swarm load balancing with nginx (automatic or needs additional setup)

I’m confused at how this works and I haven’t been able to find any suitable docs on how it works.

This is what I want to do:

I have 6 replicates of my API: 0.0.0.0:8080 across 3 managers. Manager 1 has an nginx setup where the DNS points to.

In the nginx I have this(inside http > server > for the given domain):

 resolver 127.0.0.11;

 location ~ ^/api(.*) {
  set $upstream myswarm_api:8089$1?$args;

  proxy_pass http://$upstream;

  proxy_set_header X-Real-IP $remote_addr;
  proxy_set_header X-Forwarded-For $remote_addr;
  proxy_set_header X-Forwarded-Proto $scheme;
  proxy_set_header Host $http_host;
  proxy_http_version 1.1;

  add_header Cache-Control no-cache;
}

Now my question is, does docker swarm automatically distribute traffic to all 6 replicas or just the first one that’s found to be up? If the later is true I’ve been doing this all wrong.

I see a lot of tutorials using upstream for load balancing across the cluster. I want this to work automatically when the cluster brings on new nodes so I don’t have to reconfigure it constantly.

if you configured your service to be global, each added node will run a single instance of your service automaticly. This is true, unless you specified placement constraints for the service. Though, in your case that would restrict your replicas to 3 and not to 6.

When you use a varialbe for the proxy_pass target, like you did, nginx does a dns lookup for every access, thus depending on your deployment strategy uses the vip or dnsrr to resolve the ip. Thus, the requests are balanced between the container instances.

Hard coding the proxy_pass target (which you didn’t) results in nginx caching the first ip it resolves to. This would do the oposite of what you want.