Docker Community Forums

Share and learn in the Docker community.

Docker Swarm published port limilation

(Madhurbhardwaj) #1

Hi,

I’m new to this community and anted to know how this service discovery and routing mesh will work.

Let’s say I have three worker nodes and one manager for my dev environment , and I want to deploy two different APIs on this cluster with 5 instance of each service , if I want to publish them on same port 80 how the routing will happen , as per my understanding when I’ll publish the port it will be on host so only three instance can run on this cluster as I have only three worker nodes,
what if I want to scale it to 5 or 10 instances…

will swarm will create service instance with random port number , how I’ll consume these ports from out side, how to load balance them…

thanks,
Madhur

(Sreenivas Makam) #2

Hi
With Docker and service discovery, each service has their own IP and the service IP gets mapped to different containers in that service using load balancing.
With routing mesh, you pick a free port that is exposed on all swarm hosts that is mapped to the service.
This is the flow with routing mesh.
Host:NAT -> service IP -> Containers

To your specific questions:
You can create many as instances of containers for a service even though you have 3 worker nodes since each container runs in its own network namespace.
Routing mesh host publish port number has to be unique. Multiple services can use the same port to expose them outside.

For more details on Service discovery, load balancing and routing mesh, you can refer to my blog(https://sreeninet.wordpress.com/2016/07/29/service-discovery-and-load-balancing-internals-in-docker-1-12/)

Regards
Sreenivas

1 Like
(Madhurbhardwaj) #3

Many thanks @smakam, that was really helpful,
As I’m using windows container and Routing mesh is not available in windows server 2016 ,
I’m using following command to create multiple instance of my service
docker service create --name web --publish mode=host,target=80 --replicas=5
but random port are opened on cluster (1 master and 2 worker) like …
50451->80/tcp -worker
45978->80/tcp - master
41172->80/tcp - worker
12494->80/tcp - master
25919->80/tcp-master

I can access my service with these ports number with machine name but
these ports are randomly generated , how to access them from out side or load balanced them , somebody suggest Nginx

1 Like
(Sreenivas Makam) #4

You can specify the port that you want to expose manually like:
docker service create --name web --mode=global --publish mode=host,target=80, published=8080 --replicas=5

Above, published port on host is 8080. Also, note the global mode that is used where allows service to be exposed on all nodes.
You can then use nginx to connect to all your Swarm instances publish port 8080 and this can do the load balancing.

regards
Sreenivas

#5

Hi, Any solution to this question on Windows Docker Swarm?
Because, we can create services but with randoms ports, we need to define a range of ports and deploy it in order.
We need to create a service with replicas in a range of ports (8081 - 8084) for example, --publish mode=host,target=80 but we need to define a range like: --replicas 4 --publish mode=host,target=80,publish=8081,8082,8083,8084