Docker Community Forums

Share and learn in the Docker community.

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

(Dedupely) #1

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: 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):


 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.