前提
- dockers version:1.13或者更高(17.03.1-ce)
- 安装
Docker Compose
理解service
在一个分布式应用中,不同的部分都被称为service
。
services
实际上是containers in production
。一个service
启动一个image
,它规定了image
的运行方式(使用哪些端口,需要多少个container的复制集才能满足service等)。
docker-compose.yml
文件可以很简单的定义、运行、规划services
。
docker-compose.yml
可以把该文件保存在任何位置。确保image
已经上传。
version: "3"
services:
web:
# replace username/repo:tag with your name and image details
image: username/repository:tag
deploy:
replicas: 5
resources:
limits:
cpus: "0.1"
memory: 50M
restart_policy:
condition: on-failure
ports:
- "80:80"
networks:
- webnet
networks:
webnet:
- 从
registry
拉取image
- 运行5个
image
的实例作为服务,称为web
,限制每个使用最多10%的CPU和50M的内存 - 如果停止运行立即重新启动
container
- 映射本地80端口到
web
的80端口 - 指导
web
的containers
通过负载均衡网络webnet
共享80端口。(在内部,containers
will publish toweb
’s port 80 at an ephemeral port) - 使用默认设置定义
webnet
网络
运行新的负债均衡的app
docker swarm init
# app name: getstartedlab
docker stack deploy -c docker-compose.yml getstartedlab
# See a list of the five containers you just launched
docker stack ps getstartedlab
Scale(规划) the app
可以修改replicas
,保存,重新运行docker stack deploy
:
docker stack deploy -c docker-compose.yml getstartedlab
Docker will do an in-place update, no need to tear the stack down first or kill any containers.
摧毁app和swarm
docker stack rm getstartedlab
This removes the app, but our one-node swarm is still up and running (as shown by docker node ls
). Take down the swarm with docker swarm leave --force
.
docker stack ls # List all running applications on this Docker host
docker stack deploy -c <composefile> <appname> # Run the specified Compose file
docker stack services <appname> # List the services associated with an app
docker stack ps <appname> # List the running containers associated with an app
docker stack rm <appname> # Tear down an application
网友评论