20、docker swarm

  Swarm是Docker官方提供的一款集群管理工具,其主要作用是把若干台Docker主机抽象为一个整体,并且通过一个入口统一管理这些Docker主机上的各种Docker资源。Swarm和Kubernetes比较类似,但是更加轻,具有的功能也较kubernetes更少一些。

  swarm架构图

docker swarm

  多个manager节点就会涉及到状态的同步,docker swarm采用的是内置的分布式存储的数据库,采用raft协议去同步多个manager之间的状态,防止脑裂情况发生。

  业务一般都会运行在worker节点上(manager也是可以运行的),多个worker节点之间数据和状态会通过gossip的网络同步。

20.1 service和replicas

  docker swarm中,service的概念和docker compose中的service概念是差不多的。

  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 ~]# 
posted @ 2019-01-16 22:14  StaryJie  阅读(758)  评论(0编辑  收藏  举报