HiveBrain v1.2.0
Get Started
← Back to all entries
snippetdockerMinor

How to create network connection into remote docker stack?

Submitted by: @import:stackexchange-devops··
0
Viewed 0 times
createstackdockerintohowremotenetworkconnection

Problem

I am struggling to set this up correctly. I am trying to run an app with docker stack deploy on a remote machine. The following app works, when I deploy it with docker swarm deploy on my windows notebook. But when I run it on the linux cluster. I cannot connect to it. If can help me to identify the missing piece here? When I run the app locally, I get some output from curl localhost:8000, however, not at the remote machine. There I get curl: (7) Failed to connect to localhost port 8000: Connection refused. I also do not understand why I get a different result (app accessible, not accessible) on local and remote computer, i.e. what configuration is the disrupting element.

I tried pruning all the networks and start again from scratch.

First, I connect to a remote server via ssh:

ssh -qy -L 8000:localhost:8000


Here, I start a web application with docker stack, with this simple yaml file testapp.yml:
version: '3'
services:
db:
image: mysql:5.7
volumes:
- db_data:/var/lib/mysql
restart: always
environment:
MYSQL_ROOT_PASSWORD: somewordpress
MYSQL_DATABASE: wordpress
MYSQL_USER: wordpress
MYSQL_PASSWORD: wordpress

wordpress:
depends_on:
- db
image: wordpress:latest
volumes:
- web_data:/var/www/html
ports:
- "8000:80"
restart: always
environment:
WORDPRESS_DB_HOST: db:3306
WORDPRESS_DB_USER: wordpress
WORDPRESS_DB_PASSWORD: wordpress
WORDPRESS_DB_NAME: wordpress

volumes:
db_data: {}
web_data: {}


And deploy this with

docker swarm deploy -c testapp.yml testapp


Now, I get the following output with sudo docker stack services testapp:

```
ID NAME MODE REPLICAS IMAGE PORTS
93rj1ialspj2 testapp_db replicated

Solution

First of all, thanks for the drawing so I understand what you are trying.

The next step is, when you are working with Docker Swarm all your stacks/services can hit the external networks like Redis, SQL and S3 storage so that is nothing you need to take care of.

When you want traffic inside your cluster you need a proxy in the front, I personally are using NGINX as proxy layer and then you need to create a upstream with a stack of IP's to your Docker Swarm Manager nodes, never sending it to your workers.

Docker Swarm working the way you are sending traffic to a single port number eg. 9000 and this port 9000 is binding to your stack/services on port 80 or what you are needed. Every single replicate will now running on port 9000 for externel traffic from the managers and into your cluster.

Besure you are deploying your stacks only for worker nodes except if its controller software you needed, else you can get a unstable cluster based on my own expires.

If your Redis and SQL running in the cluster (i hope not) but if you want it, you can setup a network for thoes stacks and the add the network internel into your orther stacks, but i will recommend to keep Redis, SQL and somthing like that on the own servers

So the following up on my description here is you need the following.

  • HTTP / HTTPS Proxy to route traffic into a custom port eg. 9000 for your Docker Swarm Managers



  • Expose a port eg. 9000 to your service port eg. 80



  • Setup connection inside the service to use the external Redis and SQL



Thats it, hope i have explain it good enough else you can just ask :)

Expose sample - Docker

docker run -p 9000:6379 –name redis_cont -d redis

allow port 9000 on your host to route into the redis_cont on port 6379

Expose sample - docker-compose.yml
version: "3.7"
services:

redis:
image: "redis:alpine"
ports:
- "9000:6379"


Deploy a redis stack into your Docker Swarm and allow port 9000 on your host(Docker Swarm Cluster) to route into the Redis stack service on port 6379

Context

StackExchange DevOps Q#14621, answer score: 2

Revisions (0)

No revisions yet.