Docker-学习总结(集群管理-Docker Stack+Portainer图形)

Meta ·
更新时间:2024-11-14
· 725 次阅读

文章目录Docker Stack概念命令Docker stack 实践实践环境实践过程部署一个简单服务添加监控visualizer滚动更新资源控制图形界面portainer安装前准备安装portainer Docker Stack 概念 技术由来:Docker Swarm在大规模场景下的多服务部署和管理是一件很难的事情,为解决此问题,产生了Docker stack,
Docker Stack基于Docker Swarm之上来完成对多服务进行部署和管理的 实现方式:通过使用与Compose一样的yml文件中定义应用,然后通过 docker stack deploy 命令完成部署和管理 体系结构:Stack 位于 Docker 应用层级的最顶端。Stack 基于服务进行构建,而服务又基于容器 Stack与Compose区别: Docker Stack不支持bulid指令,使用的镜像必须是构建好的,而Docker Compose可以使用,相对而言Docker Compose更适合开发使用 Docker Stack是集成在Docker引擎中的,直接使用命令即可,而Docker Compose 则是一个Python项目,使用Docker API规范操作容器 Docker Stack不支持version 2的yml文件,至少version 3以上。而Docker Compose 对version 2或3都可以处理 Docker Stack 更适合处理多服务也能处理单机服务,而Docker Compose 仅能处理单机服务,所以Docker Stack 可以把Docker Compose的工作都处理了,占据了主导地位。 命令 [root@node1 reg.mydocker.com]# docker stack --help #格式 Usage: docker stack [OPTIONS] COMMAND #Docker stack任务管理 Manage Docker stacks #选项 Options: --orchestrator string Orchestrator to use (swarm|kubernetes|all)|选择协调器是swarm还是k8s或者2者 #子命令 Commands: deploy Deploy a new stack or update an existing stack| 加载一个新的stack任务或者更新某一stack任务 ls List stacks|显示所有stack任务的列表 ps List the tasks in the stack|列出某一个stack的详细任务 rm Remove one or more stacks|删除一个或者多个stack任务 services List the services in the stack|列出某一个stack的所有服务 Docker stack 实践 实践环境
主机 IP 作用
node1 192.168.27.11 安装有Docker(18.09.6)、集群管理节点、可访问私有仓库
node2 192.168.27.12 安装有Docker(18.09.6)、集群工作节点、可访问私有仓库
node3 192.168.27.13 安装有Docker(18.09.6)、集群工作节点、可访问私有仓库
repository 192.168.27.12 搭建有harbor私有仓库,作为集群操作中的镜像使用仓库
实践过程 部署一个简单服务

官方compose文件模板:https://docs.docker.com/compose/compose-file/

制作一个yml文件,开启一个web服务,使用nginx容器,生成3个副本,自定义卷与网络 [root@node1 stack]# vim docker-stack.yml version: '3' services: web: image: library/nginx ports: - "80:80" volumes: - web-data:/usr/share/nginx/html networks: - vm_net deploy: replicas: 3 volumes: web-data: networks: vm_net: stack 运行、查看、测试 [root@node1 stack]# docker stack deploy -c docker-stack.yml my_cluster prots Additional property prots is not allowed [root@node1 stack]# vim docker-stack.yml [root@node1 stack]# docker stack deploy -c docker-stack.yml my_cluster Creating network my_cluster_vm_net Creating service my_cluster_web [root@node1 stack]# docker service ls ID NAME MODE REPLICAS IMAGE PORTS e0eo4mb2xnxa my_cluster_web replicated 3/3 nginx:latest *:80->80/tcp [root@node1 stack]# docker service ps my_cluster_web ID NAME IMAGE NODE DESIRED STATE CURRENT STATE ERROR PORTS uvq3ayobwam1 my_cluster_web.1 nginx:latest node1 Running Running 17 seconds ago ar53a9v7o94t my_cluster_web.2 nginx:latest node3 Running Running 16 seconds ago ulji2jzh25qq my_cluster_web.3 nginx:latest node2 Running Running 16 seconds ago [root@node1 stack]# curl 192.168.27.11 Welcome to nginx! body { width: 35em; margin: 0 auto; font-family: Tahoma, Verdana, Arial, sans-serif; }

Welcome to nginx!

If you see this page, the nginx web server is successfully installed and working. Further configuration is required.

For online documentation and support please refer to nginx.org.
Commercial support is available at nginx.com.

Thank you for using nginx.

添加监控visualizer 修改yml文件 version: '3' services: web: image: library/nginx ports: - "80:80" volumes: - web-data:/usr/share/nginx/html networks: - vm_net deploy: replicas: 6 #副本扩容为6 visualizer: #添加监控visualizer,docker官方模板 image: dockersamples/visualizer ports: - "8080:8080" stop_grace_period: 1m30s volumes: - "/var/run/docker.sock:/var/run/docker.sock" deploy: placement: constraints: [node.role == manager] volumes: web-data: networks: vm_net: stack重载运行,查看 [root@node1 stack]# docker stack deploy -c docker-stack.yml my_cluster Creating network my_cluster_default Updating service my_cluster_web (id: e0eo4mb2xnxagz9tfj323yye1) Creating service my_cluster_visualizer # 更新成功 [root@node1 stack]# docker service ls ID NAME MODE REPLICAS IMAGEPORTS vh66myojbstz my_cluster_visualizer replicated 1/1 dockersamples/visualizer:latest*:8080->8080/tcp e0eo4mb2xnxa my_cluster_web replicated 6/6 nginx:latest*:80->80/tcp # 开启2个服务 [root@node1 stack]# docker service ps my_cluster_web ID NAME IMAGE NODE DESIRED STATE CURRENT STATE ERROR PORTS uvq3ayobwam1 my_cluster_web.1 nginx:latest node1 Running Running 14 minutes ago ar53a9v7o94t my_cluster_web.2 nginx:latest node3 Running Running 14 minutes ago ulji2jzh25qq my_cluster_web.3 nginx:latest node2 Running Running 14 minutes ago w0vgln16y86q my_cluster_web.4 nginx:latest node1 Running Running 2 minutesago i8zdnfyamheh my_cluster_web.5 nginx:latest node3 Running Running 2 minutesago j5pjbv1yqnso my_cluster_web.6 nginx:latest node2 Running Running 2 minutesago #副本数量为6 查看web页面
在这里插入图片描述 滚动更新 修改yml文件,更新内容由容器nginx变为httpd version: '3' services: web: image: library/httpd #容器变更 ports: - "80:80" volumes: - web-data:/usr/local/apache2/htdocs #挂载地址改变 networks: - vm_net deploy: replicas: 6 update_config: #升级规则 parallelism: 2 #每次更新2个副本 delay: 5s #每批次间隔5秒 restart_policy: #副本重启规则 condition: on-failure visualizer: image: dockersamples/visualizer ports: - "8080:8080" stop_grace_period: 1m30s volumes: - "/var/run/docker.sock:/var/run/docker.sock" deploy: placement: constraints: [node.role == manager] volumes: web-data: networks: vm_net: 查看变革效果 [root@node1 stack]# docker stack deploy -c docker-stack.yml my_cluster Updating service my_cluster_web (id: e0eo4mb2xnxagz9tfj323yye1) Updating service my_cluster_visualizer (id: vh66myojbstzpkzqb7w1idoqf) #stack更新成功 [root@node1 stack]# docker service ls ID NAME MODE REPLICAS IMAGEPORTS vh66myojbstz my_cluster_visualizer replicated 1/1 dockersamples/visualizer:latest*:8080->8080/tcp e0eo4mb2xnxa my_cluster_web replicated 6/6 httpd:latest*:80->80/tcp [root@node1 stack]# docker service ps my_cluster_web ID NAME IMAGE NODE DESIRED STATE CURRENT STATE ERROR PORTS t3kkv9bnatzb my_cluster_web.1 httpd:latest node1 Running Running 2 minutes ago uvq3ayobwam1 \_ my_cluster_web.1 nginx:latest node1 Shutdown Shutdown 2 minutes ago 4jipeibbrhvx my_cluster_web.2 httpd:latest node3 Running Running 2 minutes ago ar53a9v7o94t \_ my_cluster_web.2 nginx:latest node3 Shutdown Shutdown 2 minutes ago i10n4vu3ml6a my_cluster_web.3 httpd:latest node2 Running Running 2 minutes ago ulji2jzh25qq \_ my_cluster_web.3 nginx:latest node2 Shutdown Shutdown 2 minutes ago zftgr62l49ae my_cluster_web.4 httpd:latest node1 Running Running 2 minutes ago w0vgln16y86q \_ my_cluster_web.4 nginx:latest node1 Shutdown Shutdown 2 minutes ago bcc84ui7qb7o my_cluster_web.5 httpd:latest node3 Running Running 2 minutes ago i8zdnfyamheh \_ my_cluster_web.5 nginx:latest node3 Shutdown Shutdown 2 minutes ago 9qxvk8z43yg4 my_cluster_web.6 httpd:latest node2 Running Running 2 minutes ago j5pjbv1yqnso \_ my_cluster_web.6 nginx:latest node2 Shutdown Shutdown 2 minutes ago 查看web页面显示变更成功
在这里插入图片描述 资源控制

官方文档中:RESOURCES部分

查看没有限制时的状态 [root@node1 stack]# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS cd8318ca5acc httpd:latest "httpd-foreground" 10 minutes ago Up 10 minute 0541ae18272d httpd:latest "httpd-foreground" 10 minutes ago Up 10 minute 860e5f8ca1ea dockersamples/visualizer:latest "npm start" 26 minutes ago Up 25 minute 33713d5af03b dockersamples/visualizer:latest "npm start" 27 minutes ago Exited (0) 2 f234441e886b nginx:latest "nginx -g 'daemon of…" 39 minutes ago Exited (0) 1 0db5d8d7ed5c nginx:latest "nginx -g 'daemon of…" About an hour ago Exited (0) 1 #选择查看容器cd8318ca5acc [root@node1 stack]# cd /sys/fs/cgroup/memory/docker/ #进入cgroup文件下内存 [root@node1 docker]# cd cd8318ca5acc823e25d2fe5ddb2b4a01cc0635f0587cd1e6ec324e55718f5679/ [root@node1 cd8318ca5acc823e25d2fe5ddb2b4a01cc0635f0587cd1e6ec324e55718f5679]# cat memory.limit_in_bytes 9223372036854771712 #查看容器上限 修改yml文件,添加资源限制,stack重新加载 version: '3' services: web: image: library/httpd ports: - "80:80" volumes: - web-data:/usr/local/apache2/htdocs networks: - vm_net deploy: replicas: 6 update_config: parallelism: 2 delay: 5s restart_policy: condition: on-failure resources: #资源限制模块 limits: cpus: '0.50' #cpu使用为50% memory: 50M #限制内容为50M visualizer: image: dockersamples/visualizer ports: - "8080:8080" stop_grace_period: 1m30s volumes: - "/var/run/docker.sock:/var/run/docker.sock" deploy: placement: constraints: [node.role == manager] volumes: web-data: networks: vm_net: [root@node1 stack]# docker stack deploy -c docker-stack.yml my_cluster Updating service my_cluster_web (id: e0eo4mb2xnxagz9tfj323yye1) Updating service my_cluster_visualizer (id: vh66myojbstzpkzqb7w1idoqf) #重新加载 查看限制后的状态
因为更新了容器,所以查看容器ID也会变 [root@node1 stack]# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS 8d6002aee00f httpd:latest "httpd-foreground" 4 minutes ago Up 4 minutes b98bf43f34e3 httpd:latest "httpd-foreground" 4 minutes ago Up 4 minutes cd8318ca5acc httpd:latest "httpd-foreground" 17 minutes ago Exited (0) 4 0541ae18272d httpd:latest "httpd-foreground" 17 minutes ago Exited (0) 4 860e5f8ca1ea dockersamples/visualizer:latest "npm start" 33 minutes ago Up 33 minute 33713d5af03b dockersamples/visualizer:latest "npm start" 34 minutes ago Exited (0) 3 f234441e886b nginx:latest "nginx -g 'daemon of…" About an hour ago Exited (0) 1 0db5d8d7ed5c nginx:latest "nginx -g 'daemon of…" About an hour ago Exited (0) 1 #查看新生成的容器是不是都是50M,容器是8d6002aee00f和b98bf43f34e3 [root@node1 ~]# cd /sys/fs/cgroup/memory/docker/ [root@node1 docker]# ls 860e5f8ca1ea7a2d5de91bd856340792ad037ce8f7f4faac224024daad11760b memory.failcnt memory.kmem.t 8d6002aee00f5ed539fa022899eb3c0dd14968e277ef569e739473cf7a48ca57 memory.force_empty memory.kmem.t b98bf43f34e3e5f5b3d1a6e9246c273991938c500828cd5dd474ec0188087d10 memory.kmem.failcnt memory.kmem.t cgroup.clone_children memory.kmem.limit_in_bytes memory.kmem.t cgroup.event_control memory.kmem.max_usage_in_bytes memory.kmem.u cgroup.procs memory.kmem.slabinfo memory.limit_ [root@node1 docker]# cd 8d6002aee00f5ed539fa022899eb3c0dd14968e277ef569e739473cf7a48ca57/ [root@node1 8d6002aee00f5ed539fa022899eb3c0dd14968e277ef569e739473cf7a48ca57]# cat memory.limit_in_bytes 52428800 [root@node1 8d6002aee00f5ed539fa022899eb3c0dd14968e277ef569e739473cf7a48ca57]# cd .. [root@node1 docker]# cd b98bf43f34e3e5f5b3d1a6e9246c273991938c500828cd5dd474ec0188087d10/ [root@node1 b98bf43f34e3e5f5b3d1a6e9246c273991938c500828cd5dd474ec0188087d10]# cat memory.limit_in_bytes 52428800 #显示2个容器均改为了50M 图形界面portainer 官方网站:https://www.portainer.io/ 安装前准备 portainer运行文件:https://downloads.portainer.io/portainer-agent-stack.yml portainer软件:portainer/portainer 和portainer/agent,在公有DockerHUB中可以下载,存放在本地harbor仓库中 安装portainer [root@node1 opt]# docker stack deploy -c portainer-agent-stack.yml portainer Creating network portainer_agent_network Creating service portainer_agent Creating service portainer_portainer [root@node1 opt]# docker service ls ID NAME MODE REPLICAS IMAGE PORTS 87yizm4zhk2k portainer_agent global 3/3 portainer/agent:latest g63njdqrld84 portainer_portainer replicated 1/1 portainer/portainer:latest *:8000->8000/tcp, *:9000->9000/tcp [root@node1 opt]# docker service ls ID NAME MODE REPLICAS IMAGE PORTS 87yizm4zhk2k portainer_agent global 3/3 portainer/agent:latest g63njdqrld84 portainer_portainer replicated 1/1 portainer/portainer:latest *:8000->8000/tcp, *:9000->9000/tcp web页面查看IP地址:9000端口
第一次登录,设置8位数密码,没截到图
在这里插入图片描述
作者:csdnlb



stack portainer 集群 学习 Docker

需要 登录 后方可回复, 如果你还没有账号请 注册新账号