Search code examples
dockerswarmhorizontal-scaling

Docker swarm scaling behaviour on port mapping


I have a swarm composed of three nodes:

$ sudo docker node ls
ID                            HOSTNAME         STATUS              AVAILABILITY        MANAGER STATUS
i12s3zxsn4vu1c98bv3i5idr8     node03           Ready               Active
i2ckxvsju4tmommxim3dbfq7l     node02           Ready               Active
wak4isl46dn7pbo39drrhphju *   node01           Ready               Active              Leader

Then I run 1 replica of nginx on that swarm and map his port to 8080:

$ sudo docker service create --replicas 1 --publish 8080:80 --name nginx nginx
$ sudo docker service ls
ID                  NAME                MODE                REPLICAS            IMAGE               PORTS
neahnb9mvi1i        nginx               replicated          1/1                 nginx:latest        *:8080->80/tcp

From there, i can reach nginx on http://node01:8080

Next, I scale nginx instances to 6:

$ sudo docker service scale nginx=6
$ sudo docker service ls
ID                  NAME                MODE                REPLICAS            IMAGE               PORTS
neahnb9mvi1i        nginx               replicated          6/6                 nginx:latest        *:8080->80/tcp

From there, i'm still able to reach nginx on http://node01:8080.

However, if docker swarm expose several node as a unique host, how does he manage the port during such a scaling operation as all my nginx services are mapped on the same 8080 port? Is there a round robin load balancing between all services instances done by swarm internally and returning the answer on 8080?


Solution

  • I believe the requests to the hosts get assigned in a round-robin type assignment.

    Found this handy article about it http://blog.scottlogic.com/2016/08/30/docker-1-12-swarm-mode-round-robin.html . Checkout the part titled 'INGRESS AND ROUND ROBIN LOAD BALANCING'.