Munchkin
Munchkin

Reputation: 4946

Docker Swarm Service Clustering

I want to use docker-compose with Docker Swarm (I use docker version 1.13 and compose with version: '3' syntax). Is each service reachable as a "single" service to the other services? Here is an simplified example to be clear:

version: '3'

services:
  nodejs:
    image: mynodeapp
    container_name: my_app
    ports:
      - "80:8080"
    environment:
      - REDIS_HOST=my_redis
      - REDIS_PORT=6379
    deploy:
      mode: replicated
      replicas: 3
    networks:
      - my_net
    command: npm start

  redis:
    image: redis
    container_name: my_redis
    restart: always
    expose:
      - 6379
    deploy:
      mode: replicated
      replicas: 2
    networks:
      - my_net

networks:
  my_net:
    external: true

Let's say I have 3 VMs which are configured as a swarm. So there is one nodejs container running on each VM but there are only two redis container.

On the VM where no redis is running: Will my nodejs container know about the redis?

Addiitonal questions: When I set replicas: 4 for my redis, I will have two redis container on one VM: Will this be a problem for my nodejs app?

Last question: When I set replicas: 4 for my nodeapp: Will this even work because I now have exposed two times port 80?

Upvotes: 0

Views: 636

Answers (2)

Mariano Di Maggio
Mariano Di Maggio

Reputation: 11

The services have to be stateless. In the case of databases it is necessary to set the cluster mode in each instance, since they are statefull.

Upvotes: 1

Alexander George
Alexander George

Reputation: 871

In the same order you asked:

  1. One service does not see another service as if it is made of replicas. Nodejs will see a unique Redis, which will have one IP, no matter in which node its replicas are located. That's the beauty of Swarm.
  2. Yes, you can have Nodejs in one node and Redis in another node and they will be visible to each other. That's what the manager does; make the containers "believe" they are running on the same machine.
  3. Also, you can have many replicas in the same node without a problem; they will be perceived as a whole. In fact, they use the same volume.
  4. And last, as implication of (1), there will be no problem because you are not actually exposing port 80 twice. Even having 20 replicas, you have a unique entrypoint to your service, a particular IP:PORT direction.

Upvotes: 0

Related Questions