Docker stack应用:
1)Docker 层级关系中的最高层次——stack,一个stack就是一组有关联的服务的组合,可以一起编排,一起管理。
早期,使用service来发布服务。但是service每次只能发布一个service。
yaml可以发布多个服务,但是使用docker-compose的时候,只能在一台主机上发布。
借助docker swarm同时发布多服务,
是docker 生态service发布的最高层次。
案例:
实现远程管理容器主机及在swarm集群内容器主机运行容器监控:
1)远程管理docker host
2)监控docker host上运行的容器
编写docker-compose.yml文件:
[root@swarm-1 ~]# cat docker-compose.yml
vsesion: "3"
services:
nginx:
image: nginx:latest
ports:
- "80:80"
deploy:
mode: replicated
replicas: 4
visualizer:
image: dockersamples/visualizer
ports:
- "9001:8080"
volumes:
- "/var/run/docker.sock:/var/run/docker.sock"
deploy:
replicas: 1
placement:
constraints: [node.role == manager]
portainer:
image: portainer/portainer
ports:
- "9000:9000"
volumes:
- "/var/run/docker.sock:/var/run/docker.sock"
deploy:
replicas: 1
placement:
constraints: [node.role == manager]
docker stack 运行多服务:
[root@swarm-1 ~]# docker stack --help
Usage: docker stack COMMAND
Manage Swarm stacks
Commands:
config Outputs the final config file, after doing merges and interpolations
deploy Deploy a new stack or update an existing stack
ls List stacks
ps List the tasks in the stack
rm Remove one or more stacks
services List the services in the stack
Run 'docker stack COMMAND --help' for more information on a command.
[root@swarm-1 ~]#
[root@swarm-1 ~]# docker stack deploy -c docker-compose.yaml stack-demo
Creating network stack-demo_default
Creating service stack-demo_nginx
Creating service stack-demo_visualizer
Creating service stack-demo_portainer
[root@swarm-1 ~]#
说明:其中stack-demo_default 这个是创建的网络。
验证是否创建成功:
[root@swarm-1 ~]# docker stack ls
NAME SERVICES
stack-demo 3
[root@swarm-1 ~]# docker stack services stack-demo
ID NAME MODE REPLICAS IMAGE PORTS
51n7fc35y4om stack-demo_nginx replicated 4/4 nginx:latest *:80->80/tcp
vh8zlntxz8qz stack-demo_portainer replicated 1/1 portainer/portainer:latest *:9000->9000/tcp
k0q49sihlbmn stack-demo_visualizer replicated 1/1 dockersamples/visualizer:latest *:9001->8080/tcp
[root@swarm-1 ~]#
说明:REPLICAS:4/4的意思是:期望值是4个,启动了4个。
[root@swarm-1 ~]# docker stack ps stack-demo ID NAME IMAGE NODE DESIRED STATE CURRENT STATE ERROR PORTS
nkcl95hzmbxo stack-demo_nginx.1 nginx:latest swarm-2 Running Running 3 minutes ago
jvwpf1um0f5m stack-demo_nginx.2 nginx:latest swarm-3 Running Running 3 minutes ago
uu0s6hdguh34 stack-demo_nginx.3 nginx:latest swarm-1 Running Running 3 minutes ago
mtn8qliw65gp stack-demo_nginx.4 nginx:latest swarm-1 Running Running 3 minutes ago
ujfh7q7xho7b stack-demo_portainer.1 portainer/portainer:latest swarm-1 Running Running 2 minutes ago
mzpsqfkhlaai stack-demo_visualizer.1 dockersamples/visualizer:latest swarm-1 Running Running about a minute ago
[root@swarm-1 ~]#
浏览器访问http://192.168.17.10:9000
可视化管理容器的portainer.io
创建用户: