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