Docker Swarm从部署到基本操作 (2)

在m0、m1、m2其中的任意一台上执行命令docker node ls查看Swarm集群的现状,可见工作节点已经全部就绪:

[root@m0 ~]# docker node ls ID HOSTNAME STATUS AVAILABILITY MANAGER STATUS 0isfyre69mdu1hm11esf1q3dk m2 Ready Active Reachable 7585zt09o2sat82maef0ocf42 * m0 Ready Active Leader i71bcxt1auc804syybroajtan w1 Ready Active slc0hjbs7jh2hdi8ai3wohy23 m1 Ready Active Reachable wqcwcccva3d3mxgi5p423d4fv w0 Ready Active

至此,Swarm集群环境搭建完毕,接下来可以验证了。

验证Swarm集群环境

创建名为tomcat-net的覆盖网络(Overlay Netowork),这是个二层网络,处于该网络下的docker容器,即使宿主机不一样,也能相互访问:

docker network create -d overlay tomcat-net

创建名为tomcat的服务,使用了刚才创建的覆盖网络:

docker service create --name tomcat \ --network tomcat-net \ -p 8080:8080 \ --replicas 3 \ tomcat:7.0.96-jdk8-openjdk

执行命令docker service ls查看当前所有服务:

[root@m0 ~]# docker service ls ID NAME MODE REPLICAS IMAGE kguawc4b5th4 tomcat replicated 3/3 tomcat:7.0.96-jdk8-openjdk

执行命令docker service ps tomcat查看名为tomcat的服务,可见三个容器分别部署在m0、m2、w1机器上:

[root@m0 ~]# docker service ps tomcat ID NAME IMAGE NODE DESIRED STATE CURRENT STATE ERROR PORTS n1gs9f1plce2 tomcat.1 tomcat:7.0.96-jdk8-openjdk w1 Running Running 19 minutes ago q8jyg088ci21 tomcat.2 tomcat:7.0.96-jdk8-openjdk m2 Running Running 19 minutes ago h9ww33dpw56m tomcat.3 tomcat:7.0.96-jdk8-openjdk m0 Running Running 19 minutes ago

执行命令docker service inspect --pretty tomcat查看名为tomcat的服务的详细信息(去掉--pretty可以看到更完整的):

[root@m0 ~]# docker service inspect --pretty tomcat ID: kguawc4b5th4qlwlsv183qtai Name: tomcat Service Mode: Replicated Replicas: 3 Placement: UpdateConfig: Parallelism: 1 On failure: pause Max failure ratio: 0 ContainerSpec: Image: tomcat:7.0.96-jdk8-openjdk@sha256:91eadffb59d9a35ada2d39fcd616a749ac580aa5e834499b7128f27be2e46623 Resources: Networks: tomcat-net Endpoint Mode: vip Ports: PublishedPort 8080 Protocol = tcp TargetPort = 8080

打开浏览器,尝试访问m0、m1、m2、w0、w1这个五个机器的8080端口,都可以成功访问tomcat首页:

在这里插入图片描述

服务模式

服务模式一共有两种:Ingress和Host,如果不指定,则默认的是Ingress;

Ingress模式下,到达Swarm任何节点的8080端口的流量,都会映射到任何服务副本的内部80端口,就算该节点上没有tomcat服务副本也会映射;

Host模式下,仅在运行有容器副本的机器上开放端口,使用Host模式的命令如下:

docker service create --name tomcat \ --network tomcat-net \ --publish published=8080,target=8080,mode=host \ --replicas 3 \ tomcat:7.0.96-jdk8-openjdk 服务扩缩容

执行命令docker service scale tomcat=5将副本数从3调整为5:

[root@m0 ~]# docker service scale tomcat=5 tomcat scaled to 5

执行命令docker service ps tomcat查看名为tomcat的服务,可见每台机器上都分布了一个容器:

[root@m0 ~]# docker service ps tomcat ID NAME IMAGE NODE DESIRED STATE CURRENT STATE ERROR PORTS w32tjahze2fk tomcat.1 tomcat:7.0.96-jdk8-openjdk m2 Running Running 42 minutes ago yj5czwwhrrsh tomcat.2 tomcat:7.0.96-jdk8-openjdk m0 Running Running 42 minutes ago pq40995nbd0k tomcat.3 tomcat:7.0.96-jdk8-openjdk w1 Running Running 42 minutes ago y1y6z1jczel1 tomcat.4 tomcat:7.0.96-jdk8-openjdk m1 Running Running about a minute ago w0dcii8f79os tomcat.5 tomcat:7.0.96-jdk8-openjdk w0 Running Running about a minute ago 滚动升级

当前tomcat服务中,tomcat镜像的tag是7.0.96-jdk8-openjdk,我们来尝试升级到9.0.24-jdk11-openjdk,执行以下命令:

docker service update \ --image tomcat:9.0.24-jdk11-openjdk \ --update-parallelism 1 \ --update-delay 10s tomcat

上述命令有几处需要注意:

a. update-parallelism:每次更新的容器数量,这里设置为1,表示每一个容器升级成功后才去升级下一个;

b. update-delay:每一批升级成功后,升级下一批之前的等待时间,这里表示升级一个容器后等10秒再升级下一个;

在升级过程中执行命令docker service ps tomcat查看服务,可以看到新版本容器逐个启动的过程:

[root@m0 ~]# docker service ps tomcat ID NAME IMAGE NODE DESIRED STATE CURRENT STATE ERROR PORTS w32tjahze2fk tomcat.1 tomcat:7.0.96-jdk8-openjdk m2 Running Running 56 minutes ago yj5czwwhrrsh tomcat.2 tomcat:7.0.96-jdk8-openjdk m0 Running Running 56 minutes ago semuna9awsn7 tomcat.3 tomcat:9.0.24-jdk11-openjdk w1 Running Running 15 seconds ago pq40995nbd0k \_ tomcat.3 tomcat:7.0.96-jdk8-openjdk w1 Shutdown Shutdown about a minute ago y1y6z1jczel1 tomcat.4 tomcat:7.0.96-jdk8-openjdk m1 Running Running 15 minutes ago oot3yex74v4t tomcat.5 tomcat:9.0.24-jdk11-openjdk w0 Running Preparing 5 seconds ago w0dcii8f79os \_ tomcat.5 tomcat:7.0.96-jdk8-openjdk w0 Shutdown Shutdown 3 seconds ago

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/wpffyd.html