docker中三个概念:
1 、stack
service
container
service是产品级别的container,一个service运行一个镜像,但它定义镜像的运行方式。比如:占用哪个端口、复制生成多少个container等。在docker平台中,通过docker-compose.yml来定义、运行、控制service。
docker-compose.yml格式如下:
version: "3"
services:
web:
# replace username/repo:tag with your name and image details
# image: username/repo:tag
#阿里云镜像仓库
image: registry.cn-qingdao.aliyuncs.com/*****/*****:[镜像版本号]
deploy:
replicas: 5
resources:
limits:
cpus: "0.1"
memory: 50M
restart_policy:
condition: on-failure
ports:
- "4000:80"
networks:
- webnet
networks:
webnet:
2、运行负载均衡service前,先运行
docker swarm init
运行此命令的目的是开启swarm模式,当前的机器为swarm管理者
3、然后运行
docker stack deploy -c docker-compose.yml getstartedlab
4、运行后这个service会运行5个container实例。可通过以下命令查看
docker service ls
docker service ps getstartedlab_web
#查看container实例id
docker container ls -q
5、在浏览器中访问http://localhost:40000 并重复刷新时,展示的containerID会发生变化。以此展示了负载均衡的service
6、关闭程序和swarm
#Take the app down with docker stack rm:
docker stack rm getstartedlab
#Take down the swarm.
docker swarm leave --force
参照资料 https://docs.docker.com/get-started/part3/#take-down-the-app-and-the-swarm