堆栈(Stack)的概念:
堆栈是一组相互关联的服务,它们共享依赖关系,并且可以协调和缩放在一起。单个堆栈能够定义和协调整个应用程序的功能,但是非常复杂的应用程序可能希望使用多个堆栈。
下面我们就开始完成我们完成的Flask 程序依赖的Redis服务。
1. 添加一个可视化服务
重新编辑docker-compose.yml,更新我们的服务
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:
注解:
-
volumes
键,让可视化工具访问Docker的主机套接字文件 -
placement
密钥,确保这个服务只能在一个swarm管理器上运行
进入管理器节点(myvm1)
eval $(docker-machine env myvm1)
更新服务
[root@docker ~ [myvm1]]# docker stack deploy -c docker-compose.yml getstartedlab Updating service getstartedlab_visualizer (id: rkb4ijoal4p6ra2ogjbsev6iw) Updating service getstartedlab_web (id: utklmyoy9wywmokunbi6uxawi)
查看可视化工具
visualizer
正如您所期望的那样,单个副本正在管理器上运行,并且5个实例web
分布在整个群集中。您可以通过运行docker stack ps <stack>
以下来证实此可视化:
docker stack ps getstartedlab
添加相关服务-redis
1. 更新docker-compose.yml
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. 访问服务