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
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 composev3.7
onDocker version 19.03.2
: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:
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.