版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/u012299594/article/details/83116545
Stack其实就是相互关联的一组service,一般地,应用的所有service就放在一个stack里,通过.yml文件就可以一键完成应用部署,当然更复杂的应用可能会service拆分到多个stack中。在前面的笔记中。我们部署了单个service的stack,这节的demo在这个stack里部署更多的service。
Note
本文demo参考docker官方文档,有条件的朋友建议直接看原文Link:https://docs.docker.com/get-started/part5/#persist-the-data
Demo
修改hello-service.yml文件,在里面新增两个service:visualizer和Redis。
visualizer:
image: dockersamples/visualizer:stable
ports:
- "8080:8080"
volumes:
- "/var/run/docker.sock:/var/run/docker.sock"
deploy:
placement:
constraints: [node.role == manager]
networks:
- webnet
redis:
image: redis
ports:
- "6379:6379"
volumes:
- "/home/hunk/docker:/data"
deploy:
placement:
constraints: [node.role == manager]
command: redis-server --appendonly yes
networks:
- webnet
node.role == manager指定这个service的实例均运行在swarm manager节点上,不会分到worker节点
部署应用
root@hunk-virtual-machine1:/home/hunk/docker# docker stack deploy -c hello-service.yml hello-service
Updating service hello-service_visualizer (id: pjb8l0antomhh6riulm3k3o7n)
Creating service hello-service_redis
Updating service hello-service_web (id: y46ique6nw7taei64dfm44c6s)
root@hunk-virtual-machine1:/home/hunk/docker# docker service ls
ID NAME MODE REPLICAS IMAGE
633175oy620e hello-service_redis replicated 1/1 redis:latest
pjb8l0antomh hello-service_visualizer replicated 1/1 dockersamples/visualizer:stable
y46ique6nw7t hello-service_web replicated 5/5 hebostary/gohead:demo1
root@hunk-virtual-machine1:/home/hunk/docker# docker stack ls
NAME SERVICES
hello-service 3
访问hello-service_visualizer
访问hello-service_web