Docker Swarm Service Networking

773 views Asked by At

For a DevOps university project we've been tasked with building a web video streaming service using docker and Jenkins etc. We've got completely free reign over how we do it... Swarm looked interesting so I thought I'd give it a go (as it does replication that'll probably gimme some bonus points).

My background is data stuff (python was great fun) so servers and networking etc. are not my forte. Also, this is all being done locally on my machine.

I'm struggling with communicating with an actual docker service (softinstigate/restheart image to communicate with a Mongodb service).

It's replicated over two docker machines. Then connects to a local network, which in turn connects to another "proxy" overlay network.

So, how do I connect to that specific restheart api service to get some data for a HTML page? Normal containers build etc would be localhost:chosenport. But I believe with docker-machines it's a different IP address? How is the port number defined?

In essence, how does one connect to a specific service within a swarm from, say, a website page?

I found a few articles to read through but couldn't make heads or tails of it at the time. Think I might have tried changing localhost address to the leader swarm node's IP address which didn't seem to work (but it was a while ago).

Docker commands for setup to follow - on my phone at the mo.

# https://technologyconversations.com/2016/07/29/docker-swarm-introduction-tour-around-docker-1-12-series/

#### create machines

for i in 1 2 3; do
echo docker-machine create -d virtualbox node-$i
docker-machine create -d virtualbox node-$i
done

docker-machine ls


#### active machine = node-1
eval $(docker-machine env node-1)


#### node-1 join swarm

echo node-1 joins swarm as manager, token variable saved as $TOKEN
docker swarm init     --advertise-addr $(docker-machine ip node-1)  --listen-addr $(docker-machine ip node-1):2377


#### token value for swarm as a envirnomanet variable
TOKEN=$(docker swarm join-token -q worker)


#### join the relevant worker nodes to the swarm
for i in 2 3; do
echo node-$i to join the swarm
eval $(docker-machine env node-$i)
docker swarm join     --token $TOKEN  $(docker-machine ip node-1):2377
done


#### move vack to node-1
echo moving back to node-1
eval $(docker-machine env node-1)


#### https://github.com/vfarcic/docker-flow-proxy/blob/master/articles/swarm-mode-listener.md


#### create networks
echo starting networks
docker network create --driver overlay webapp-data-store
docker network create --driver overlay proxy


#### create swarm listener monitor on proxy network
echo starting swarm listener service.....
docker service create --name swarm-listener --network proxy \
    --mount "type=bind,source=/var/run/docker.sock,target=/var/run/docker.sock" \
    -e DF_NOTIF_CREATE_SERVICE_URL=http://proxy:8080/v1/docker-flow-proxy/reconfigure \
    -e DF_NOTIF_REMOVE_SERVICE_URL=http://proxy:8080/v1/docker-flow-proxy/remove \
    --constraint 'node.role==manager' \
    vfarcic/docker-flow-swarm-listener


#### create proxy service on proxy network
echo proxy service start up
docker service create --name proxy \
    -p 80:80 -p 443:443 -p 8080:8080 \
    --network proxy \
    -e MODE=swarm \
    -e LISTENER_ADDRESS=swarm-listener vfarcic/docker-flow-proxy


#### RESTHEART SERVICE
echo start the restheart service
docker service create --name video-restheart \
    --network webapp-data-store
    --replicas 3 \
    softinstigate/restheart

# video mongo data store

echo video-db start up
docker service create --name video-db \
    --network webapp-data-store
    --replicas 3 \
1

There are 1 answers

0
Aditya C S On BEST ANSWER

Docker Swarm mode has an internal LoadBalancing. Whenever you create a service with some port published, you can access the service by hitting any of the manager node or worker node. For ex: manager IP 192.168.2.2 worker IP 192.168.2.3

Say service is running in worker node on 9000 port you can access it through 192.168.2.2:9000 or 192.168.2.3:9000. Docker internally will take care of routing the request to correct container.

Please go through this blog to get a full understanding. http://blog.scottlogic.com/2016/08/30/docker-1-12-swarm-mode-round-robin.html.

Also, you can configure a LB for the containers and hit the LB instead of directly hitting the container. You can find an example here...

https://technologyconversations.com/2016/08/01/integrating-proxy-with-docker-swarm-tour-around-docker-1-12-series/