20、docker swarm

  Swarm是Docker官方提供的一款集羣管理工具,其主要做用是把若干臺Docker主機抽象爲一個總體,而且經過一個入口統一管理這些Docker主機上的各類Docker資源。Swarm和Kubernetes比較相似,可是更加輕,具備的功能也較kubernetes更少一些。node

  swarm架構圖nginx

docker swarm

  多個manager節點就會涉及到狀態的同步,docker swarm採用的是內置的分佈式存儲的數據庫,採用raft協議去同步多個manager之間的狀態,防止腦裂狀況發生。算法

  業務通常都會運行在worker節點上(manager也是能夠運行的),多個worker節點之間數據和狀態會經過gossip的網絡同步。docker

20.1 service和replicas

  docker swarm中,service的概念和docker compose中的service概念是差很少的。shell

  replicas是在service的基礎上作橫向擴展,例如,一個nginx的swevice中有三個nginx的容器,其中每個容器就能夠理解爲一個replica。數據庫

docker swarm

20.2 swarm服務的建立和調度

  swarm中經過manager節點去部署一個service的時候,咱們事先是不知道這個service最終會運行在哪些swarm的cluster節點上的,swarm的scheduler會根據必定的調度算法去計算(好比會根據每臺cluster節點的資源使用率等),將容器調度在相應的節點上。服務器

  swarm服務建立和調度圖:網絡

docker swarm

20.3 建立一個三節點的swarm集羣

  初始化一個集羣,宣告manager節點地址:架構

swarm-manager

docker swarm init --advertise-addr=192.168.205.10

Swarm initialized: current node (xyl16kq5aj71sbqccys1jsoyv) is now a manager.

To add a worker to this swarm, run the following command:

    docker swarm join --token SWMTKN-1-5igsd3yoz1hm8z39q0emtgx4ot0ei31vgv8kkvy1jybxwd8286-0lewqgbggwmhrpclt7myjvuu9 192.168.205.10:2377

To add a manager to this swarm, run 'docker swarm join-token manager' and follow the instructions.

  到此爲止,docker swarm的manager節點建立完成,能夠經過提示在worker節點上運行下面的命令便可在這個manager節點中添加worker節點:分佈式

docker swarm join --token SWMTKN-1-5igsd3yoz1hm8z39q0emtgx4ot0ei31vgv8kkvy1jybxwd8286-0lewqgbggwmhrpclt7myjvuu9 192.168.205.10:2377

swarm-worker1

docker swarm join --token SWMTKN-1-5igsd3yoz1hm8z39q0emtgx4ot0ei31vgv8kkvy1jybxwd8286-0lewqgbggwmhrpclt7myjvuu9 192.168.205.10:2377

This node joined a swarm as a worker.

swarm-manager

  經過docker node ls查看當前的swarm的節點信息:

docker node ls

ID                            HOSTNAME            STATUS              AVAILABILITY        MANAGER STATUS      ENGINE VERSION
xyl16kq5aj71sbqccys1jsoyv *   swarm-manager       Ready               Active              Leader              18.03.1-ce
r2mh5nz1k7d765j6zxj3e1hgf     swarm-worker1       Ready               Active                                  18.03.1-ce

swarm-worker2

docker swarm join --token SWMTKN-1-5igsd3yoz1hm8z39q0emtgx4ot0ei31vgv8kkvy1jybxwd8286-0lewqgbggwmhrpclt7myjvuu9 192.168.205.10:2377

This node joined a swarm as a worker.

swarm-manager

docker node ls

ID                            HOSTNAME            STATUS              AVAILABILITY        MANAGER STATUS      ENGINE VERSION
xyl16kq5aj71sbqccys1jsoyv *   swarm-manager       Ready               Active              Leader              18.03.1-ce
81zzel5tk5im0rmp98xi306qa     swarm-worker1       Ready               Active                                  18.03.1-ce
r2mh5nz1k7d765j6zxj3e1hgf     swarm-worker2       Ready               Active                                  18.03.1-ce

20.4 swarm中service的建立和維護

建立service

  經過docker service create建立一個服務,相似於docker run命令:

docker service create --name demo busybox sh -c "while true;do sleep 3600;done"

i7jn9sbf4xmdv3t6nl8f7ywp3
overall progress: 1 out of 1 tasks
1/1: running   [==================================================>]
verify: Service converged

  查看已有的service:

docker service ls

ID                  NAME                MODE                REPLICAS            IMAGE               PORTS
i7jn9sbf4xmd        demo                replicated          1/1                 busybox:latest

  經過上面的信息咱們不知道demo這個服務的容器運行在哪一臺服務器上,可是,能夠經過docker service ps service_name來查看:

docker service ps demo

ID                  NAME                IMAGE               NODE                DESIRED STATE       CURRENT STATE           ERROR               PORTS
ewkxqwwa7u5n        demo.1              busybox:latest      swarm-manager       Running             Running 4 minutes ago

  能夠發現,demo這個service運行在swarm-manager這個管理節點上。在swarm-manager節點上經過docker ps查看:

docker ps

CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS               NAMES
e80a085916b0        busybox:latest      "sh -c 'while true;d…"   6 minutes ago       Up 6 minutes                            demo.1.ewkxqwwa7u5ndaaigls3xvts3

service 擴展

  咱們經過以前的demo這個service能夠看到有一個REPLICAS顯示的是1/1,這就說明demo這個service是能夠橫向擴展的,service的擴展相似於docker compose。也是經過參數scale來實現:

docker service scale demo=5

demo 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

  查看demo服務:

docker service ls

ID                  NAME                MODE                REPLICAS            IMAGE               PORTS
i7jn9sbf4xmd        demo                replicated          5/5                 busybox:latest

  能夠發現REPLICAS已經由1/1變成了5/5。再次查看這5個容器分別運行在哪些節點之上:

docker service ps demo

ID                  NAME                IMAGE               NODE                DESIRED STATE       CURRENT STATE            ERROR               PORTS
ewkxqwwa7u5n        demo.1              busybox:latest      swarm-manager       Running             Running 14 minutes ago
pxdw4jdn5clq        demo.2              busybox:latest      swarm-manager       Running             Running 3 minutes ago
5iayi08grmzo        demo.3              busybox:latest      swarm-worker1       Running             Running 2 minutes ago
313zmkgrm933        demo.4              busybox:latest      swarm-worker2       Running             Running 3 minutes ago
jawwrmdgrey4        demo.5              busybox:latest      swarm-worker2       Running             Running 3 minutes ago

  經過docker swarm scale建立的橫向擴展(好比scale demo=5),那麼,不管在哪一個節點上強制刪除其中一個或者多個該service的容器(dockerd服務必須在運行),docker swarm scale都會自動在當前的集羣中的其餘容器從新啓動相應個數的容器,保持scale指定的數量。這樣就能夠確保系統穩定,不會出現業務中止的狀況。

刪除service

  刪除一個service只須要經過docker service rm service_name便可,在刪除service以後,docker swarm會自動從當前swarm集羣中刪除該服務以前運行的全部容器(有必定的延時):

docker service rm demo

demo

  刪除服務以後,再次查看該服務狀況:

docker service ps demo
no such service: demo

docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES

  

[root@docker ~]# 
[root@docker ~]# 
[root@docker ~]#
相關文章
相關標籤/搜索