0

I am new to docker swarm and want to understand, how docker stack deploy works

version: '3'
services:
 web:
image: nginx
ports:
  - "80:80"
redis:
image: "redis:alpine"

When I run this command, it works perfectly fine

$ docker stack deploy -c docker-compose.yml mystack
 Creating network mystack_default
 Creating service mystack_web
 Creating service mystack_redis

To verify it

$ docker stack ps mystack
ID                  NAME                IMAGE               NODE                          
DESIRED STATE       CURRENT STATE           ERROR               PORTS
rcci590y5c6o        mystack_redis.1     redis:alpine        
plakhera1.example.com   Running             Running 9 seconds ago                       
lty84a77b4my        mystack_web.1       nginx:latest        
plakhera2.mylabserver.com   Running             Running 2 seconds ago 

Now my question is how to make sure that every time I deploy this application/service Redis will come up on plakhera1.example.com and nginx on plakhera2.example.com. I believe currently its picking nodes randomly, so how can I make sure it always pick only those nodes.

Update: The scenario is my web is the basic app but the Redis does a lot of heavy lifting, that why I always want Redis to be on the beefy node, is there is any way I can specify via docker stack deploy that web always boots up on node1 and Redis on node2

Prashant Lakhera
  • 683
  • 1
  • 9
  • 25
  • If you want to look easily at the logs, set up some ELK-like central log server. Other than that I don't see benefit in singling out the hostnames and looking into that at all. – kubanczyk Oct 25 '19 at 18:29
  • @kubanczyk not sure if I understand your answer but my scenario is my web is the basic app but the Redis do lot of heavy lifting, that why I always want redis to be on the beefy node, is there is any way I can specify via docker stack deploy that web always boot's up on node1 and Redis on node2. – Prashant Lakhera Oct 25 '19 at 18:32

1 Answers1

1

I think the quickest way to do this is to set a node.hostname placement constraint on the service. Note I have only tested this with compose v3.7 on Docker version 19.03.2:

version: '3.7'
services:
  redis:
    image: "redis:alpine"
    deploy:
      # replicas: 3
      placement:
        constraints:
          - node.hostname == plakhera1.example.com

Note if you uncomment replicas (which you might in the case of worker nodes or something) all replicas should be deployed to the specified node.

Verify manually with:

docker stack ps mystack

This has the disadvantage of needing to specify the actual hostname in the compose file. I have noted that you can also label nodes and set placements based on these labels however I found the documentation to be slightly poor here, and when doing a quick test it didn't behave as expected but that could just be myself in a rush.

v25
  • 748
  • 1
  • 6
  • 13