堆棧(Stack)的概念:node
堆棧是一組相互關聯的服務,它們共享依賴關係,而且能夠協調和縮放在一塊兒。單個堆棧可以定義和協調整個應用程序的功能,可是很是複雜的應用程序可能但願使用多個堆棧。web
下面咱們就開始完成咱們完成的Flask 程序依賴的Redis服務。redis
從新編輯docker-compose.yml,更新咱們的服務docker
version: "3" services: web: # replace username/repo:tag with your name and image details image: scottcho/flask:v1 deploy: replicas: 5 restart_policy: condition: on-failure resources: limits: cpus: "0.1" memory: 50M ports: - "80:80" networks: - webnet 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 networks: webnet:
註解:flask
volumes
鍵,讓可視化工具訪問Docker的主機套接字文件placement
密鑰,確保這個服務只能在一個swarm管理器上運行進入管理器節點(myvm1)bash
eval $(docker-machine env myvm1)
更新服務app
[root@docker ~ [myvm1]]# docker stack deploy -c docker-compose.yml getstartedlab Updating service getstartedlab_visualizer (id: rkb4ijoal4p6ra2ogjbsev6iw) Updating service getstartedlab_web (id: utklmyoy9wywmokunbi6uxawi)
查看可視化工具ssh
visualizer
正如您所指望的那樣,單個副本正在管理器上運行,而且5個實例web
分佈在整個羣集中。您能夠經過運行docker stack ps <stack>
如下來證明此可視化:工具
docker stack ps getstartedlab
1. 更新docker-compose.ymlspa
version: "3" services: web: # replace username/repo:tag with your name and image details image: flask/scottcho:v1 deploy: replicas: 5 restart_policy: condition: on-failure resources: limits: cpus: "0.1" memory: 50M ports: - "80:80" networks: - webnet 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/docker/data:/data" deploy: placement: constraints: [node.role == manager] command: redis-server --appendonly yes networks: - webnet networks: webnet:
2. 建立容器的掛載目錄
docker-machine ssh myvm1 "mkdir ./data"
3. 更新服務
[root@docker ~ [myvm1]]# docker stack deploy -c docker-compose.yml getstartedlab Updating service getstartedlab_visualizer (id: rkb4ijoal4p6ra2ogjbsev6iw) Updating service getstartedlab_web (id: utklmyoy9wywmokunbi6uxawi)
4. 訪問服務