Dzhuneyt
Dzhuneyt

Reputation: 8711

How to handle port traffic only to Docker Swarm nodes that have the service that exposes that port?

If we imagine that the Docker Swarm consists of node A, B and C. If we imagine that we run a Docker Stack of a single service (for the sake of example), scaled to 2 instances and that service exposes port 80 of the host machine.

How do I make sure that any traffic that hits:

http://A:80
http://B:80
http://C:80

Always lands on a live Docker instance.

Given that there are 2 instances of the service and 3 nodes total, there will always be at least one node that doesn't have the service on it, so it will not expose port 80 (I assume).

Upvotes: 3

Views: 367

Answers (1)

codinghaus
codinghaus

Reputation: 2358

One benefit of using orchestration with e.g. swarm mode is that you must not now anything about single nodes in your swarm. Instead swarm works on a higher level of abstraction than nodes --> on services.

So you tell swarm which nodes it consists of, what services you have and how many instances of containers you want to run inside the swarm for each single service. After configuring that it is swarm's job to decide/know which container runs on which node. Again: you don't care about the single nodes.

So the question is not how to make

http://A:80
http://B:80
http://C:80

(Re)route to a correct/valid node (running a corresponding container with exposed port)

because the only thing you need to know is the name of your service. So you only will call

http://myservice:80

And then swarm mode will decide on which node the request will be forwarded to (http://A:80 or http://B:80 or http://C:80). And if you have 3 nodes, 1 service and 2 replicas for that service swarm will ensure that no requests will be forwarded to the node, on which no container is running because it knows there are only 2 replicas and it knows on which nodes these instances run.

Upvotes: 1

Related Questions