docker-swarm

    技术2022-07-10  145

    swarm集群部署

    简介部署swarm搭建私有仓库部署集群部署serviceservice伸缩实现 Failover访问 service

    简介

    Swarm的特性: 1.工作节点的注册和发现 2.管理节点收集集群中所有的信息 3.管理节点支持HA 4.管理节点能感知集群的变化,在节点挂掉后重新调度上面的container 5.提供filter和scheduler的调度策略调度集群里的容器

    Swarm概念 swarm:是一组docker引擎的集群 node:是单个docker引擎的实例,可以在一个物理机上也可以在多个 application:是应用 manager node:部署应用的时候会有一个manager node节点 Worker nodes:对应的就是Worker nodes service:然后service是一堆被workder执行的任务 replicated services:是负载均衡节点 global services:则是全局的,在所有节点上都会执行的一个服务 task:一个task就是一个docker的容器,是Swarm的工作单元

    部署swarm

    环境: swarm 192.168.1.10 node1 192.168.1.20 node2 192.168.1.30

    搭建私有仓库

    docker pull registry:2 mkdir -p /opt/data/registry docker run -dit -p 5000:5000 --restart always -v /opt/data/registry/:/var/lib/registry --name registry registry:2 vim /usr/lib/systemd/system/docker.service ##添加--insecure-registry 192.168.1.10:5000 ExecStart=/usr/bin/dockerd -H fd:// --containerd=/run/containerd/containerd.sock --insecure-registry 192.168.1.10:5000 ##其余两台worker同样添加 ##下载httpd打包 docker pull httpd docker tag httpd:latest 192.168.1.10:5000/httpd docker push 192.168.1.10:5000/httpd curl 192.168.1.10:5000/v2/_catalog {"repositories":["httpd","visualizer"]}

    部署集群

    vim /etc/hosts 192.168.1.20 node1 192.168.1.30 node2 ssh-keygen ##一直回车 ssh-copy-id node1 ssh-copy-id node2 ##开启防火墙相应端口 firewall-cmd --add-port=2377/tcp --permanent firewall-cmd --add-port=7946/tcp --permanent firewall-cmd --add-port=7946/udp --permanent firewall-cmd --add-port=4789/udp --permanent firewall-cmd --add-port=4789/tcp --permanent firewall-cmd --reload

    初始化

    docker swarm init --advertise-addr 192.168.1.10 ##会有提示如何加入集群 docker swarm join-token manager ##查看加入集群命令 docker node ls ID HOSTNAME STATUS AVAILABILITY MANAGER STATUS ENGINE VERSION mil1gpiau7ujhfjgnvp5hdzc7 * swarm Ready Active Leader 19.03.12 ##node1与node2 ##加入集群 docker swarm join --token SWMTKN-1-15skd1ufc98589qgpxwzksft7e4ml4bxrkjj2j0lkqooa4pe1p-88tzyad1zi5i77lveqbxmqstn 192.168.1.10:2377 ##swarm docker node ls docker node ls ID HOSTNAME STATUS AVAILABILITY MANAGER STATUS ENGINE VERSION n9okoh4p9gihvr80u9hiwoetu node1 Ready Active 19.03.12 nvnjwoixsql85aveph6247lwe node2 Ready Active 19.03.12 mil1gpiau7ujhfjgnvp5hdzc7 * swarm Ready Active Leader 19.03.12

    下载可视化工具

    docker pull dockersamples/visualizer docker run -dit -p 8888:8080 -e HOST=192.368.1.10 -v /var/run/docker.sock:/var/run/docker.sock 192.168.1.10:5000/visualizer ##访问 ip:8888

    部署service

    ##swarm docker service create --name web_server 192.168.1.10:5000/httpd docker service ls ID NAME MODE REPLICAS IMAGE PORTS n164bmq4u7uc web_server replicated 3/3 192.168.1.10:5000/httpd:latest

    REPLICAS 显示当前副本信息,0/1 的意思是 web_server 这个 service 期望的容器副本数量为 1,目前已经启动的副本数量为 0。也就是当前 service 还没有部署完成。命令 docker service ps 可以查看 service 每个副本的状态。

    docker service ps web_server ID NAME IMAGE NODE DESIRED STATE CURRENT STATE ERROR PORTS 8amf6th0zfh9 web_server.1 192.168.1.10:5000/httpd:latest node2 Running Running 15 seconds ago

    service伸缩

    docker service scale web_server=5 web_server scaled to 5 overall progress: 5 out of 5 tasks 1/5: running [==================================================>] 2/5: running [==================================================>] 3/5: running [==================================================>] 4/5: running [==================================================>] 5/5: running [==================================================>] verify: Service converged docker service ps web_server ID NAME IMAGE NODE DESIRED STATE CURRENT STATE ERROR PORTS 8amf6th0zfh9 web_server.1 192.168.1.10:5000/httpd:latest node2 Running Running 3 minutes ago rog6rlrpke84 web_server.2 192.168.1.10:5000/httpd:latest swarm Running Running 18 seconds ago vt9goq7efi0v web_server.3 192.168.1.10:5000/httpd:latest node2 Running Running 18 seconds ago nlc7x2st1ay8 web_server.4 192.168.1.10:5000/httpd:latest node1 Running Running 18 seconds ago g44z1ksntkk8 web_server.5 192.168.1.10:5000/httpd:latest swarm Running Running 18 seconds ago

    实现 Failover

    ##关闭node1 poweroff ##还是5个服务

    访问 service

    在创建service时添加 --publish-add 暴露端口如:

    docker service create --publish 8080:80 192.168.1.10:5000/httpd

    对于已经创建的service使用更新的命令

    docker service update --publish-add 8080:80 web_server

    验证

    curl 192.168.1.10:8080

    It works!

    curl 192.168.1.20:8080

    It works!

    curl 192.168.1.30:8080

    It works!

    Processed: 0.015, SQL: 9