Docker Swarm由兩部分組成:node
官方資料:https://docs.docker.com/swarm/算法
下圖是個典型的Docker Swarm集羣部署圖,來自Docker官網: 接下來照着上圖來搭建一個Docker Swarm集羣。docker
本次實戰一共用到了5臺機器,配置信息所有相同,以下:shell
機器的信息以下表所示: | 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),通常狀況下會參考如下兩個原則:tomcat
接下來的整個部署過程分爲如下幾步:網絡
接下來正式開始吧;tcp
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/17/whats-docker-swarm-advertise-addr/ 2. 控制檯返回信息以下,代表Swarm集羣初始化成功:this
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秒再升級下一個; 2. 在升級過程當中執行命令<font color="blue">docker service ps tomcat</font>查看服務,能夠看到新版本容器逐個啓動的過程:
[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從部署到基本操都已經體驗過一次了,但願您在搭建環境的時候,本文能給您一些參考。