A stack is a group of interrelated services that share dependencies, and can be orchestrated and scaled together. A single stack is capable of defining and coordinating the functionality of an entire application (though very complex applications may want to use multiple stacks).
Some good news is, you have technically been working with stacks since part 3, when you created a Compose file and used docker stack deploy. But that was a single service stack running on a single host, which is not usually what takes place in production. Here, you will take what you’ve learned, make multiple services relate to each other, and run them on multiple machines.
堆是一组共享依赖相互关联的服务,可以被一起部署、扩展。一个单独的stack有能力定义和协调整个应用的功能。(非常复杂的程序可能会使用多个stack)
好消息是我们已经从第3部分开始用stack工作了。只是之前是单台机器上运行的单一服务,生产环境通常不会是这样。下面运用之前学到的,让多个服务关联到一起,并且运行在多台机器上。
1 发布多个service
1.1 修改docker-compose.yml文件
version: "3"
services:
web:
image: gaojingyuan/testrepo: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:
1.2 运行
先登录
docker login
再执行
docker stack deploy -c docker-compose.yml getstartedlab
1.3 访问visualizer
2 持久化数据
2.1 在docker-compose.yml中加入redis配置
redis:
image: redis
ports:
- "6379:6379"
volumes:
- /home/docker/data:/data
deploy:
placement:
constraints: [node.role == manager]
command: redis-server --appendonly yes
networks:
- webnet
redis在docker库里有官方镜像,授权了一个简单的名字redis
其他默认软件参照
https://docs.docker.com/samples/
2.2 配置redis
a.redis总是再管理机上运行,使用同一个文件系统
b.管理机需要创建/data文件夹
docker-machine ssh myvm1 "mkdir ./data"
3 运行
3.1 再次执行
docker stack deploy -c docker-compose.yml getstartedlab
控制台输出
Updating service getstartedlab_visualizer (id: kxnda4km2x5cvyyt7ylh1nqmi)
Creating service getstartedlab_redis
Updating service getstartedlab_web (id: xs960tu2fgrnzjcpbrjt8ipt4)
3.2 查看是否有redis的service
docker service ls
控制台输出
ID NAME MODE REPLICAS IMAGE PORTS
zgxdz8dwhkhc getstartedlab_redis replicated 1/1 redis:latest *:6379->6379/tcp
kxnda4km2x5c getstartedlab_visualizer replicated 1/1 dockersamples/visualizer:stable *:8080->8080/tcp
xs960tu2fgrn getstartedlab_web replicated 5/5 gaojingyuan/testrepo:v1 *:80->80/tcp
3.3 访问部署的services
网友评论