Docker Swarm 是 Docker 的集羣管理工具。它將 Docker 主機池轉變爲單個虛擬 Docker 主機。 Docker Swarm 提供了標準的 Docker API,全部任何已經與 Docker 守護程序通訊的工具均可以使用 Swarm 輕鬆地擴展到多個主機。html
支持的工具包括但不限於如下各項:node
如上圖所示,swarm 集羣由管理節點(manager)和工做節點(work node)構成。web
docker swarmdocker
master主機 | node1主機 | node2主機 |
192.168.1.1 | 192.168.1.2 | 192.168.1.4 |
#初始化docker swarm集羣 [root@master ~]# docker swarm init --advertise-addr 192.168.1.1 Swarm initialized: current node (iygbj3p7fy5r5iamh8vnmfiy9) is now a manager. To add a worker to this swarm, run the following command: docker swarm join --token SWMTKN-1-27agu7b7s7uadjmenr0u4xjn8wq054g10thqrs6evx4tedry6v-438686bomufj7hux98opa5mmz 192.168.1.1:2377 To add a manager to this swarm, run 'docker swarm join-token manager' and follow the instructions.
上邊返回的結果告訴咱們:初始化成功,而且,若是想要添加work節點運 行下面的命令: shell
docker swarm join --token SWMTKN-1-27agu7b7s7uadjmenr0u4xjn8wq054g10thqrs6evx4tedry6v-438686bomufj7hux98opa5mmz 192.168.1.1:2377apache
這裏執行的節點只能在24小時有效!如需永久以下命令便可!npm
添加manager節點運行命令以下:vim
docker swarm join-token manager後端
#查看docker swarm加入的節點,此命令只能在manager節點執行! [root@master ~]# docker node ls ID HOSTNAME STATUS AVAILABILITY MANAGER STATUS ENGINE VERSION iygbj3p7fy5r5iamh8vnmfiy9 * master Ready Active Leader 20.10.3 5fb4hsoxyl1sesv8rg8niya3n node1 Ready Active 18.06.1-ce 9ga8eol8vodhhodtlnad35s4u node2 Ready Active 18.06.1-ce
經常使用命令bash
#將節點離開集羣 [root@node1 ~]# docker swarm leave Node left the swarm. [root@node2 ~]# docker swarm leave Node left the swarm. #在master查看 [root@master ~]# docker node ls ID HOSTNAME STATUS AVAILABILITY MANAGER STATUS ENGINE VERSION iygbj3p7fy5r5iamh8vnmfiy9 * master Ready Active Leader 20.10.3 5fb4hsoxyl1sesv8rg8niya3n node1 Down Active 18.06.1-ce 9ga8eol8vodhhodtlnad35s4u node2 Down Active 18.06.1-ce
#在加入swarm集羣,先生成join令牌 docker swarm join-token [manager | worker]:生成令牌,能夠是 manager身份或worker身份。 [root@master ~]# docker swarm join-token worker To add a worker to this swarm, run the following command: docker swarm join --token SWMTKN-1-27agu7b7s7uadjmenr0u4xjn8wq054g10thqrs6evx4tedry6v-438686bomufj7hux98opa5mmz 192.168.1.1:2377 #複製到加入的節點便可。 #刪除node節點,刪除節點爲down的狀態才能夠進行刪除 [root@master ~]# docker node ls ID HOSTNAME STATUS AVAILABILITY MANAGER STATUS ENGINE VERSION iygbj3p7fy5r5iamh8vnmfiy9 * master Ready Active Leader 20.10.3 5fb4hsoxyl1sesv8rg8niya3n node1 Down Active 18.06.1-ce ycfgtitwkdyo9gnh98uy4td4z node1 Down Active 18.06.1-ce pm3w9jfhntp2e7ft8psnk5wth node2 Down Active 18.06.1-ce [root@master ~]# docker node rm pm3w9jfhntp2e7ft8psnk5wth ycfgtitwkdyo9gnh98uy4td4z 5fb4hsoxyl1sesv8rg8niya3n pm3w9jfhntp2e7ft8psnk5wth ycfgtitwkdyo9gnh98uy4td4z 5fb4hsoxyl1sesv8rg8niya3n [root@master ~]# docker node ls ID HOSTNAME STATUS AVAILABILITY MANAGER STATUS ENGINE VERSION iygbj3p7fy5r5iamh8vnmfiy9 * master Ready Active Leader 20.10.3
1 #overlay:覆蓋型網絡。 2 #建立一個類型爲overlay的網絡,network網卡名稱爲docker 3 [root@master ~]# docker network create -d overlay --attachable docker 4 qv2bik9lw7xv77kene9mi3vux 5 --attachable參數:必須加,不然容器不能夠。 6 此網絡在worker節點不能夠查看,但可用,manager能夠! 7 [root@master ~]# docker network ls 8 NETWORK ID NAME DRIVER SCOPE 9 c1ef1eb53a9a bridge bridge local 10 qv2bik9lw7xv docker overlay swarm 11 804db36d48a5 docker_gwbridge bridge local 12 b61e22116929 host host local 13 r0bvaffd6kuj ingress overlay swarm 14 8a24d0c21f6c none null local 15 #測試分別在3臺主機開啓容器,使用docker網絡 16 #建立容器 17 [root@master ~]# docker run -it --name a --network docker busybox:latest 18 19 [root@node1 ~]# docker run -it --name a2 --network docker busybox:latest 20 21 [root@node2 ~]# docker run -it --name a3 --network docker busybox:latest 22 # 23 [root@master ~]# docker run -it --name a --network docker busybox:latest 24 docker: Error response from daemon: Invalid container name (a), only [a-zA-Z0-9][a-zA-Z0-9_.-] are allowed. 25 See 'docker run --help'. 26 [root@master ~]# docker run -it --name a1 --network docker busybox:latest 27 / # 28 / # ping a2 29 PING a2 (10.0.1.4): 56 data bytes 30 64 bytes from 10.0.1.4: seq=0 ttl=64 time=0.823 ms 31 64 bytes from 10.0.1.4: seq=1 ttl=64 time=0.327 ms 32 ^C 33 --- a2 ping statistics --- 34 2 packets transmitted, 2 packets received, 0% packet loss 35 round-trip min/avg/max = 0.327/0.575/0.823 ms 36 / # ping a3 37 PING a3 (10.0.1.6): 56 data bytes 38 64 bytes from 10.0.1.6: seq=0 ttl=64 time=1.674 ms 39 64 bytes from 10.0.1.6: seq=1 ttl=64 time=0.535 ms 40 ^C 41 --- a3 ping statistics --- 42 2 packets transmitted, 2 packets received, 0% packet loss 43 round-trip min/avg/max = 0.535/1.104/1.674 ms
[root@master ~]# docker run -d -p 8080:8080 -e HOST=192.168.1.1 -e PORT=8080 -v /var/run/docker.sock:/var/run/docker.sock --name visualizer dockersamples/visualizer b793e13b3da9ab9f4c6a19c88a4274ba752b2e87c659294de6571d8d55884872 [root@master ~]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES b793e13b3da9 dockersamples/visualizer "npm start" 5 seconds ago Up 4 seconds (health: starting) 0.0.0.0:8080->8080/tcp visualizer
搭建一個私有倉庫,從私有倉庫中進行下載鏡像
#運行私有倉庫服務 ]# docker pull registry:2 ]# docker run -itd --name registry --restart=always -p 5000:5000 -v /registry:/var/lib/registry registry:2 -v: 掛載目錄。 宿主機的目錄:容器內的目錄。 #//由於咱們使用的私有鏡像,在上傳或下載的時候,都須要咱們將鏡像的名 稱直接重命名,要註明它私有倉庫的IP地址:暴露端口。切記是必須,不然 將識別不到倉庫而上傳或下載失敗。 docker tag 原有鏡像 修改鏡像名稱 #編輯docker的配置文件,指定私有倉庫地址 vim /usr/lib/systemd/system/docker.service ExecStart=/usr/bin/dockerd --insecure-registry 192.168.1.1:5000 #修改完docker配置文件,重啓docker //驗證私有倉庫是否配置成功 能夠用以下命令: ]# docker info Insecure Registries: 192.168.1.1:5000 127.0.0.0/8
#節點操做如出一轍
要求:基於httpd鏡像,更改主訪問界面內容。鏡像tag版本爲v1.v2.v3,對 應主機面內容爲v一、v二、v3
FROM 683a7aad17d3 #基於683鏡像(683爲本機httpd的鏡像) RUN echo v1 > /usr/local/apache2/htdocs/index.html #run運行的shell命令將v1寫入到 /usr/local/apache2/htdocs/index.html #v二、v3修改便可
要求: 副本數量爲3個。服務的名稱爲:web
#docker swarm運行httpd:v1鏡像生成容器名稱爲web,replicas副本集爲3個(至關於3個容器),映射容器內部80的端口 [root@master ~]# docker service create --replicas 3 --name web -p 80 192.168.1.1:5000/httpd:v1 elfsbdlxbnyrxq326863m1vq9 overall progress: 3 out of 3 tasks 1/3: running 2/3: running 3/3: running verify: Service converged #不寫network默認使用ingress,ingress網卡提供爲後端的的container的統一入口。
擴容與縮容直接直接經過scale進行設置副本數量。 [root@master ~]# docker service scale web=6 web scaled to 6 overall progress: 6 out of 6 tasks 1/6: running 2/6: running 3/6: running 4/6: running 5/6: running 6/6: running verify: Service converged
#將鏡像更新爲 192.168.1.1:5000/httpd:v2 更新的副本集是web [root@master ~]# docker service update --image 192.168.1.1:5000/httpd:v2 web web overall progress: 6 out of 6 tasks 1/6: running 2/6: running 3/6: running 4/6: running 5/6: running 6/6: running verify: Service converged #平滑的升級 [root@master ~]# docker service update --image 192.168.1.1:5000/httpd:v3 --update-parallelism 2 --update-delay 1m web 解釋: --update-parallelism:每一次更新副本集的數量 --update-delay:中間間隔時間 m分鐘 s秒 #回滾,這是swarm不如k8s的一個點,回滾只能是上一層 [root@master ~]# docker service rollback web web rollback: manually requested rollback overall progress: rolling back update: 6 out of 6 tasks 1/6: running 2/6: running 3/6: running 4/6: running 5/6: running 6/6: running verify: Service converged