swarm集群服务创建网络
集群的基本操作熟悉了,我们就在集群上部署个应用,然后看看swarm集群中的应用操作方法。在swarm集群中创建应用之前需要先创建一个网络,因为swarm集群中的节点是依靠网络来管理节点之间的通信的。
[root@wulaoer.org ~]# docker network create -d overlay wulaoer_net [root@wulaoer.org ~]# docker network ls NETWORK ID NAME DRIVER SCOPE a38745e6b5b8 bridge bridge local a01287537f07 docker_gwbridge bridge local 09784c5005ab host host local pddr4assraqs ingress overlay swarm fe6de3090b26 none null local iwsgekycuzsb wulaoer_net overlay swarm
swarm集群中创建应用
网络创建之后才可以创建应用,下面我们创建一个nginx应用。
[root@wulaoer.org ~]# docker service create --replicas 1 --network wulaoer_net --name wulaoer_web -p 80:80 nginx vlm6qznhp54qma1up7xeuzdq2 overall progress: 1 out of 1 tasks 1/1: running [==================================================>] verify: Service converged
nginx服务已经创建完成,我们先看一下nginx是运行在那个节点上面
[root@wulaoer.org ~]# docker service ps wulaoer_web ID NAME IMAGE NODE DESIRED STATE CURRENT STATE ERROR PORTS nja5m0crefrv wulaoer_web.1 nginx:latest slave Running Running 10 minutes ago
只运行了一个副本,运行在slave节点上,我们测试一下swarm是否能够做节点转发,我们使用curl访问三个节点的80端口,获取状态值。
[root@wulaoer.org ~]# curl -I -m 10 -o /dev/null -s -w %{http_code} 10.211.55.143 200[root@wulaoer.org ~]# curl -I -m 10 -o /dev/null -s -w %{http_code} 10.211.55.144 200[root@wulaoer.org ~]# curl -I -m 10 -o /dev/null -s -w %{http_code} 10.211.55.145 200[root@wulaoer.org ~]#
swarm集群是可以做节点转发的,这里尝试把master节点的docker关闭,其他节点的应用不影响访问。
应用自动转移
我们尝试一下扩展多个容器,扩展不是在原来的基础上扩展,而是扩展后的总数,这里千万要记得。
[root@wulaoer.org ~]# docker service scale wulaoer_web=5 #这里的5是总副本数,如果想启动10个副本必须把5改成10,不能在启动一下,不会在五的基础上在加5 wulaoer_web 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
我们看一下增加的五个容器节点运行在哪个服务器上了。
[root@wulaoer.org ~]# docker service ps wulaoer_web ID NAME IMAGE NODE DESIRED STATE CURRENT STATE ERROR PORTS nja5m0crefrv wulaoer_web.1 nginx:latest slave Running Running 27 minutes ago 8wazfd9562tj wulaoer_web.2 nginx:latest wulaoer.org Running Running 3 minutes ago kmntagm4lgk0 wulaoer_web.3 nginx:latest slave Running Running 5 minutes ago pd8w2f9ivivk wulaoer_web.4 nginx:latest node Running Running 3 minutes ago nsld1eq9qoka wulaoer_web.5 nginx:latest node Running Running 3 minutes ago
随机分配所有节点上都有增加的容器了,我们尝试一下,下线一个节点,五个容器应用是否有什么变化
[root@wulaoer.org ~]# docker node update --availability drain mrtks5bwhpe5k3vskv2kctf66 mrtks5bwhpe5k3vskv2kctf66 [root@wulaoer.org ~]# docker service ps wulaoer_web ID NAME IMAGE NODE DESIRED STATE CURRENT STATE ERROR PORTS q98hcjj9id87 wulaoer_web.1 nginx:latest wulaoer.org Running Running 1 second ago nja5m0crefrv \_ wulaoer_web.1 nginx:latest slave Shutdown Shutdown 2 seconds ago 8wazfd9562tj wulaoer_web.2 nginx:latest wulaoer.org Running Running 6 minutes ago xqyjcwlznjmy wulaoer_web.3 nginx:latest node Running Running 1 second ago kmntagm4lgk0 \_ wulaoer_web.3 nginx:latest slave Shutdown Shutdown 2 seconds ago pd8w2f9ivivk wulaoer_web.4 nginx:latest node Running Running 6 minutes ago nsld1eq9qoka wulaoer_web.5 nginx:latest node Running Running 6 minutes ago
节点下线后自动转到别的节点上了,我们尝试着上线节点,看看是否会重新分配到nginx的应用。
[root@wulaoer.org ~]# docker node update --availability active mrtks5bwhpe5k3vskv2kctf66 mrtks5bwhpe5k3vskv2kctf66 [root@wulaoer.org ~]# docker service ps wulaoer_web ID NAME IMAGE NODE DESIRED STATE CURRENT STATE ERROR PORTS q98hcjj9id87 wulaoer_web.1 nginx:latest wulaoer.org Running Running 2 minutes ago nja5m0crefrv \_ wulaoer_web.1 nginx:latest slave Shutdown Shutdown 2 minutes ago 8wazfd9562tj wulaoer_web.2 nginx:latest wulaoer.org Running Running 9 minutes ago xqyjcwlznjmy wulaoer_web.3 nginx:latest node Running Running 2 minutes ago kmntagm4lgk0 \_ wulaoer_web.3 nginx:latest slave Shutdown Shutdown 2 minutes ago pd8w2f9ivivk wulaoer_web.4 nginx:latest node Running Running 9 minutes ago nsld1eq9qoka wulaoer_web.5 nginx:latest node Running Running 9 minutes ago
节点恢复后不在还原到原来的节点上,而且shutdown状态的应用不能删除,扩展后会继续启用。
应用扩展和伸缩
如果节点不够使用,需要手动增加副本(副本的意思就是容器),使用scale参数,直接在应用名后加扩展后的总数即可。
[root@wulaoer.org ~]# docker service scale wulaoer_web=10 wulaoer_web scaled to 10 overall progress: 10 out of 10 tasks 1/10: running [==================================================>] 2/10: running [==================================================>] 3/10: running [==================================================>] 4/10: running [==================================================>] 5/10: running [==================================================>] 6/10: running [==================================================>] 7/10: running [==================================================>] 8/10: running [==================================================>] 9/10: running [==================================================>] 10/10: running [==================================================>] verify: Service converged [root@wulaoer.org ~]# docker service ps wulaoer_web ID NAME IMAGE NODE DESIRED STATE CURRENT STATE ERROR PORTS q98hcjj9id87 wulaoer_web.1 nginx:latest wulaoer.org Running Running 7 minutes ago nja5m0crefrv \_ wulaoer_web.1 nginx:latest slave Shutdown Shutdown 7 minutes ago 8wazfd9562tj wulaoer_web.2 nginx:latest wulaoer.org Running Running 13 minutes ago xqyjcwlznjmy wulaoer_web.3 nginx:latest node Running Running 7 minutes ago kmntagm4lgk0 \_ wulaoer_web.3 nginx:latest slave Shutdown Shutdown 7 minutes ago pd8w2f9ivivk wulaoer_web.4 nginx:latest node Running Running 13 minutes ago nsld1eq9qoka wulaoer_web.5 nginx:latest node Running Running 13 minutes ago dz9jcfbapp2b wulaoer_web.6 nginx:latest slave Running Running about a minute ago s862hh31sbmm wulaoer_web.7 nginx:latest slave Running Running about a minute ago w5pa73p4uegz wulaoer_web.8 nginx:latest wulaoer.org Running Running about a minute ago dtldonliiy72 wulaoer_web.9 nginx:latest node Running Running about a minute ago prq1yzvkalp2 wulaoer_web.10 nginx:latest slave Running Running about a minute ago
这时你你会发现原来恢复的节点又开始工作了,我们这个操作在针对集群的节点检查,如果slave这个节点的docker关闭了(也可以理解为服务挂了),应用是否会自动的转移,看下面的测试结果:
[root@slave ~]# systemctl stop docker [root@wulaoer.org ~]# docker service ps wulaoer_web ID NAME IMAGE NODE DESIRED STATE CURRENT STATE ERROR PORTS q98hcjj9id87 wulaoer_web.1 nginx:latest wulaoer.org Running Running 11 minutes ago nja5m0crefrv \_ wulaoer_web.1 nginx:latest slave Shutdown Shutdown 11 minutes ago 8wazfd9562tj wulaoer_web.2 nginx:latest wulaoer.org Running Running 18 minutes ago xqyjcwlznjmy wulaoer_web.3 nginx:latest node Running Running 11 minutes ago kmntagm4lgk0 \_ wulaoer_web.3 nginx:latest slave Shutdown Shutdown 11 minutes ago pd8w2f9ivivk wulaoer_web.4 nginx:latest node Running Running 18 minutes ago nsld1eq9qoka wulaoer_web.5 nginx:latest node Running Running 18 minutes ago 6zwwgfexw40j wulaoer_web.6 nginx:latest node Running Running 7 seconds ago dz9jcfbapp2b \_ wulaoer_web.6 nginx:latest slave Shutdown Running 25 seconds ago pke6g8pv0mqq wulaoer_web.7 nginx:latest wulaoer.org Running Running 7 seconds ago s862hh31sbmm \_ wulaoer_web.7 nginx:latest slave Shutdown Running 5 minutes ago w5pa73p4uegz wulaoer_web.8 nginx:latest wulaoer.org Running Running 5 minutes ago dtldonliiy72 wulaoer_web.9 nginx:latest node Running Running 5 minutes ago uv05g749zsmm wulaoer_web.10 nginx:latest wulaoer.org Running Running 7 seconds ago prq1yzvkalp2 \_ wulaoer_web.10 nginx:latest slave Shutdown Running 25 seconds ago
所有应用已经全部转移到正常的节点上了,我们把关闭docker服务的节点在启动一下docker,不过应用并没有恢复到slave节点上,这说明节点只有在改动的时候才会发生变化。下面我们做个测试,在缩减节点的同时,使用一个for循环一直访问某一个节点的80端口,测试一下swarm集群中节点的伸缩对应用是否有影响。
[root@slave ~]# for i in `seq 1000000`;do curl -I -m 10 -o /dev/null -s -w %{http_code} 10.211.55.143;done [root@wulaoer.org ~]# docker service scale wulaoer_web=5
查看slave节点一直是200的状态,所以证明节点的伸缩不会影响到应用的使用。
应用管理
我们在swarm集群中添加了很多应用,时间太长了。忘记以前添加了哪些应用怎么查看?添加的应用下线了,如何删除掉?看下面的如何操作。
[root@wulaoer.org ~]# docker service ls ID NAME MODE REPLICAS IMAGE PORTS i6r7hxzcge25 portainer replicated 0/1 portainer/portainer:latest *:9000->9000/tcp stk7qtdb16zp tomcat_web replicated 1/1 tomcat:latest *:8080->8080/tcp r65fxe69rst5 wulaoer_web replicated 5/5 nginx:latest *:80->80/tcp [root@wulaoer.org ~]# docker service rm i6r7hxzcge25 i6r7hxzcge25
这里的查看涉及到应用的数量,以及应用的版本和端口,不过这里没有一个自动扩容,我们可以使用脚本代替,完全可以在小环境中使用了。如果发布系统直接更新容器版本即可,我们把代码打包放到容器中,直接导出生成镜像然后在swarm集群中拉取镜像文件进行更新,这里的更新是先下一个更新完上线,一个一个的循环,知道全部上线完成。
[root@wulaoer.org ~]# docker service update --image nginx:1.13 wulaoer_web wulaoer_web 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 [root@wulaoer.org ~]# docker service ls ID NAME MODE REPLICAS IMAGE PORTS wqsz7akbjq66 portainer replicated 0/1 portainer/portainer:latest *:9000->9000/tcp stk7qtdb16zp tomcat_web replicated 1/1 tomcat:latest *:8080->8080/tcp r65fxe69rst5 wulaoer_web replicated 5/5 nginx:1.13 *:80->80/tcp
上线的过程不影响用户的使用,整个环境不需要其他的操作,自动完成。还有一种更新是创建新的应用和原应用并存,创建成功后删除旧的应用,这样的好处是在发布的过程中如果有出现问题会自动的回滚,而不需要用户接入,错误率降低很多。
[root@wulaoer.org ~]# docker service update wulaoer_web --replicas 10 --rollback-parallelism 2 --rollback-monitor 20s --rollback-max-failure-ratio .2 --image nginx:1.13 image nginx:1.13 could not be accessed on a registry to record its digest. Each node will access nginx:1.13 independently, possibly leading to different nodes running different versions of the image. wulaoer_web overall progress: 7 out of 10 tasks 1/10: running [==================================================>] 2/10: running [==================================================>] 3/10: running [==================================================>] 4/10: running [==================================================>] 5/10: running [==================================================>] 6/10: running [==================================================>] 7/10: running [==================================================>] 8/10: preparing [=================================> ] 9/10: 10/10: service update paused: update paused due to failure or early termination of task ewcfow914q1s3r1tvttjpt1gb [root@wulaoer.org ~]# docker service ls ID NAME MODE REPLICAS IMAGE PORTS mws14uzcgg92 portainer replicated 0/1 portainer/portainer:latest *:9000->9000/tcp stk7qtdb16zp tomcat_web replicated 1/1 tomcat:latest *:8080->8080/tcp r65fxe69rst5 wulaoer_web replicated 10/10 nginx:1.13 *:80->80/tcp [root@wulaoer.org ~]# docker service ps wulaoer_web | grep "Running" k21ghvlr1ory wulaoer_web.1 nginx:1.13 wulaoer.org Running Running 21 minutes ago sk2qyg8t0zbp wulaoer_web.2 nginx:1.13 wulaoer.org Running Running 21 minutes ago xk4pcanzbj3x wulaoer_web.3 nginx:latest node Running Running less than a second ago gasdzux6x98s wulaoer_web.4 nginx:latest slave Running Running less than a second ago jfksh7xf6e8k wulaoer_web.5 nginx:1.13 wulaoer.org Running Running 43 seconds ago jtmcvac12ear wulaoer_web.6 nginx:1.13 wulaoer.org Running Running 21 minutes ago qrz876zaklez wulaoer_web.7 nginx:1.13 wulaoer.org Running Running 21 minutes ago 5c2wyx2ak3hz wulaoer_web.8 nginx:1.13 wulaoer.org Running Running 21 minutes ago 9m5ztgfqacvi wulaoer_web.9 nginx:1.13 wulaoer.org Running Running 21 minutes ago ojk6rk5hh5au wulaoer_web.10 nginx:1.13 wulaoer.org Running Running 21 minutes ago
上面的参数注解update更新的服务名称是wulaoer_web。增加了副本是--replicas数量是10。同时执行回滚数动作--rollback-parallelism,数量是2。每次回滚间隔设置--rollback-monitor,时间为2s。回滚的故障率设置--rollback-max-failure-ratio如果小于.2就是百分之0.2允许运行。更新的镜像--image,镜像名称是nginx:1.13。
上面的更新没有问题,那就直接上线,如果更新出现问题我们需要进行回滚,那么回滚的操作设置可以参考下面的信息:
[root@wulaoer.org ~]# docker service update --rollback wulaoer_web wulaoer_web rollback: manually requested rollback overall progress: rolling back update: 5 out of 5 tasks 1/5: running [> ] 2/5: running [> ] 3/5: running [> ] 4/5: running [> ] 5/5: running [> ] verify: Service converged [root@wulaoer.org ~]# docker service ls ID NAME MODE REPLICAS IMAGE PORTS wqsz7akbjq66 portainer replicated 0/1 portainer/portainer:latest *:9000->9000/tcp stk7qtdb16zp tomcat_web replicated 1/1 tomcat:latest *:8080->8080/tcp r65fxe69rst5 wulaoer_web replicated 5/5 nginx:latest *:80->80/tcp
我们的swarm集群已经使用的差不多了,目前只是学习,所以运行的环境不太一样,如果我们使用不同的应用,所需要的CPU和内存是不一样的,所以在创建应用时,需要根据不同的配置然后在node节点上运行不同的应用。下面我们尝试一下在某一个节点上增加应用。swarm集群如果想在指定的节点上运行指定的服务,需要给指定的节点大上标签,打完标签,在发布应用时会自动的跑到指定的节点上。
[root@wulaoer.org ~]# docker node update --label-add func=nginx node #给节点打标签 node [root@wulaoer.org ~]# docker service create --name my_nginx --constraint 'node.labels.func == nginx' nginx #根据标签创建应用 gm51gy4hidv8vgm9yfr3co4c3 overall progress: 1 out of 1 tasks 1/1: running [==================================================>] [root@wulaoer.org ~]# docker service ps my_nginx ID NAME IMAGE NODE DESIRED STATE CURRENT STATE ERROR PORTS axoi4v80rdln my_nginx.1 nginx:latest node Running Running less than a second ago
如果这个服务不想使用了,我们需要下线,首先把容器数量减少,我们缩减应用。保证所有应用都不在承担用户的请求,然后直接删除应用即可。
[root@wulaoer.org ~]# docker service ls ID NAME MODE REPLICAS IMAGE PORTS mws14uzcgg92 portainer replicated 0/1 portainer/portainer:latest *:9000->9000/tcp stk7qtdb16zp tomcat_web replicated 1/1 tomcat:latest *:8080->8080/tcp r65fxe69rst5 wulaoer_web replicated 10/10 nginx:1.13 *:80->80/tcp [root@wulaoer.org ~]# docker service scale wulaoer_web=0 wulaoer_web scaled to 0 overall progress: 0 out of 0 tasks verify: Service converged [root@wulaoer.org ~]# docker service ls ID NAME MODE REPLICAS IMAGE PORTS mws14uzcgg92 portainer replicated 0/1 portainer/portainer:latest *:9000->9000/tcp stk7qtdb16zp tomcat_web replicated 1/1 tomcat:latest *:8080->8080/tcp r65fxe69rst5 wulaoer_web replicated 0/0 nginx:1.13 *:80->80/tcp [root@wulaoer.org ~]# docker service rm wulaoer_web wulaoer_web [root@wulaoer.org ~]# docker service ls ID NAME MODE REPLICAS IMAGE PORTS mws14uzcgg92 portainer replicated 0/1 portainer/portainer:latest *:9000->9000/tcp stk7qtdb16zp tomcat_web replicated 1/1 tomcat:latest *:8080->8080/tcp
整个应用已经删除完毕,端口也不在占用,可以部署新的应用,不影响新的应用使用。
页面管理swarm
swarm集群也和Kubernetes一样,也有页面管理操作,我们先创建一个swarm集群的页面管理,然后看一下页面管理是什么样子的。
[root@server09 ~]# docker service create --name portainer --publish 9000:9000 --replicas=1 --constraint 'node.role == manager' --mount type=bind,source=/path/on/host/data,dst=/data portainer/portainer mp4r44jctqmol9gs06zvwd8g3 overall progress: 1 out of 1 tasks 1/1: running [==================================================>] verify: Service converged
swarm集群管理页面portainer已经做好了,下面使用浏览器,访问Leader的IP加端口9000,查看一下:
默认没有用户,需要自己创建,输入管理员admin的密码即可,第二次是验证。
登录后,连接一下Leader,我这个是浏览器自动翻译了,应该是英文的。
我这这里把所有的节点都加上去了,加入方法在这里添加。
页面自动获取swarm集群中创建的服务以及副本的情况。还有网络,更新以及回滚的操作。目前真对swarm集群的操作部分就先写到这了,swarm的页面还是可以的,不过对比一下k8s,那就不太好了。
swarm和k8s相比有很多不同点点,但是在使用上差别不是很大,如果运行的服务流量比较稳定的话可以尝试使用swarm集群,不过国内使用的比较少,可能是先入为主吧,很多人都会舍弃swarm而使用k8s。
您可以选择一种方式赞助本站
支付宝扫一扫赞助
微信钱包扫描赞助
赏