projectharbor 2019-09-13
Docker Swarm由两部分组成:
官方资料:https://docs.docker.com/swarm/
下图是个典型的Docker Swarm集群部署图,来自Docker官网:
接下来照着上图来搭建一个Docker Swarm集群。
本次实战一共用到了5台机器,配置信息全部相同,如下:
机器的信息如下表所示:
IP地址 | 主机名 | 身份 |
---|---|---|
192.168.121.142 | m0 | 管理节点 |
192.168.121.139 | m1 | 管理节点 |
192.168.121.140 | m2 | 管理节点 |
192.168.121.141 | w0 | 工作节点 |
192.168.121.138 | w1 | 工作节点 |
从官方图可见,管理节点集群之间的内部管理协调使用了Raft共识算法,这样就保证了管理节点高可用(HA),一般情况下会参考以下两个原则:
接下来的整个部署过程分为以下几步:
接下来正式开始吧;
docker swarm init \ --advertise-addr 192.168.121.142:2377 --listen-addr 192.168.121.142:2377
关于advertise-addr和listen-addr这两个参数,前者用来指定其他节点连接m0时的地址,后者指定承载swarm流量的IP和端口,它们更详细和深入的区别可以参考文章:https://boxboat.com/2016/08/1...
Swarm initialized: current node (7585zt09o2sat82maef0ocf42) is now a manager. To add a worker to this swarm, run the following command: docker swarm join \ --token SWMTKN-1-5huefb5501cv7p8i2op1am2oevasoqu4te8vpvapndkudvszb4-e8l6755jstd7urpdo5smyi8fv \ 192.168.121.142:2377 To add a manager to this swarm, run 'docker swarm join-token manager' and follow the instructions.
[root@m0 ~]# docker node ls ID HOSTNAME STATUS AVAILABILITY MANAGER STATUS 7585zt09o2sat82maef0ocf42 * m0 Ready Active Leader
现在集群已经建立起来了,接下来我们需要加入更多的管理节点和工作节点;
[root@m0 ~]# docker swarm join-token manager To add a manager to this swarm, run the following command: docker swarm join \ --token SWMTKN-1-5huefb5501cv7p8i2op1am2oevasoqu4te8vpvapndkudvszb4-5tz9d4w7nwzu8r4ozd0ff2aiu \ 192.168.121.142:2377
[root@m0 ~]# docker swarm join-token worker To add a worker to this swarm, run the following command: docker swarm join \ --token SWMTKN-1-5huefb5501cv7p8i2op1am2oevasoqu4te8vpvapndkudvszb4-e8l6755jstd7urpdo5smyi8fv \ 192.168.121.142:2377
两种join token都准备好了,接下来开始加入新节点。
[root@m1 ~]# docker swarm join \ > --token SWMTKN-1-5huefb5501cv7p8i2op1am2oevasoqu4te8vpvapndkudvszb4-5tz9d4w7nwzu8r4ozd0ff2aiu \ > 192.168.121.142:2377 This node joined a swarm as a manager.
[root@m1 ~]# docker node ls ID HOSTNAME STATUS AVAILABILITY MANAGER STATUS 0isfyre69mdu1hm11esf1q3dk m2 Ready Active Reachable 7585zt09o2sat82maef0ocf42 m0 Ready Active Leader slc0hjbs7jh2hdi8ai3wohy23 * m1 Ready Active Reachable
[root@w0 ~]# docker swarm join \ > --token SWMTKN-1-5huefb5501cv7p8i2op1am2oevasoqu4te8vpvapndkudvszb4-e8l6755jstd7urpdo5smyi8fv \ > 192.168.121.142:2377 This node joined a swarm as a worker.
[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集群环境搭建完毕,接下来可以验证了。
docker network create -d overlay tomcat-net
docker service create --name tomcat \ --network tomcat-net \ -p 8080:8080 \ --replicas 3 \ tomcat:7.0.96-jdk8-openjdk
[root@m0 ~]# docker service ls ID NAME MODE REPLICAS IMAGE kguawc4b5th4 tomcat replicated 3/3 tomcat:7.0.96-jdk8-openjdk
[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
[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
docker service create --name tomcat \ --network tomcat-net \ --publish published=8080,target=8080,mode=host \ --replicas 3 \ tomcat:7.0.96-jdk8-openjdk
[root@m0 ~]# docker service scale tomcat=5 tomcat scaled to 5
[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
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秒再升级下一个;
[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
执行命令<font color="blue">docker service rm tomcat</font>即可删除服务:
[root@m0 ~]# docker service rm tomcat tomcat [root@m0 ~]# docker service ls ID NAME MODE REPLICAS IMAGE
至此,Docker Swarm从部署到基本操都已经体验过一次了,希望您在搭建环境的时候,本文能给您一些参考。