目录

Life in Flow

知不知,尚矣;不知知,病矣。
不知不知,殆矣。

X

Swarm

分布式系统中多容器管理的困难


生产环境中同一种类型的容器往往散落在各个主机节点上,因此无法回避下列问题:
Swarm

  • 怎么去管理这么多容器?
  • 怎么能方便的横向扩展?
  • 如果容器down了,怎么能自动恢复?
  • 如何去更新容器而不影响业务?
  • 如何去监控追踪这些容器?
  • 怎么去调度容器的创建?
  • 保护隐私数据?

Docker Swarm Mode Architecture

Swarm Architecture
 综上所述,容器编排应运而生,docker的容器编排工具的实现有很多,只不过swarm是内置于docker的容器编排工具,由docker公司内置到docker中。

Service & Replicas

Service & Replicas

  • 在docker swarm模式下Service的概念和Docker Compose中的概念基本一致。
  • 在Replicas横向扩展模式下,每个Replicas都是一个实际的容器,每个容器都会根据调度系统的计算,被分配在不同的node上运行。也就是说,通过Swarm Manager部署去部署一个Service时候,其实是不知道容器最终会运行在哪些swarm节点上的,这取决于Swarm的调度算法的计算结果(例如:根据节点负载)。
    服务的创建与调度

搭建3节点的swam cluster

Swarm Cluster

# 初始化一个swarm集群,在Swarm Manager中: 192.168.31.220
[root@localhost ~]# docker swarm init --advertise-addr=192.168.31.220
Swarm initialized: current node (wx8spu6s1tszpo4hbvkc5v1qt) is now a manager.
To add a worker to this swarm, run the following command:

    docker swarm join --token SWMTKN-1-5mxs3nwkgidt55i7erq1d663xvj94lanh2xdep76b4q7xxg15w-cdgbba27hiqeb0b6buzzv6whq 192.168.31.220:2377

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

# swarm node1 加入 cluster: 192.168.31.240
[root@centos7_two ~]# docker swarm join --token SWMTKN-1-5mxs3nwkgidt55i7erq1d663xvj94lanh2xdep76b4q7xxg15w-cdgbba27hiqeb0b6buzzv6whq 192.168.31.220:2377
This node joined a swarm as a worker.

# swarm node2 加入 cluster: 192.168.31.250
[root@centos7_three ~]# docker swarm join --token SWMTKN-1-5mxs3nwkgidt55i7erq1d663xvj94lanh2xdep76b4q7xxg15w-cdgbba27hiqeb0b6buzzv6whq 192.168.31.220:2377
This node joined a swarm as a worker.

# 在Swarm Manager中 查看Swam Cluster信息
[root@localhost ~]# docker node list
ID                            HOSTNAME                STATUS              AVAILABILITY        MANAGER STATUS      ENGINE VERSION
p4jexd5om3yofpgz3n4vyq2yt     centos7_three           Ready               Active                                  18.09.7
ms9s0pquan1hvue2ubaxn2evl     centos7_two             Ready               Active                                  18.09.7
wx8spu6s1tszpo4hbvkc5v1qt *   localhost.localdomain   Ready               Active              Leader              18.09.7

Service的创建维护和水平扩展

 默认创建一个Service可能会运行在Cluster中的任意一个节点上。

# 创建一个service:create相当于run
[root@localhost ~]# docker service create --name demo busybox sh -c "while true;do sleep 3600;done"
jvomccrnms8cz14jbcthwg6ey
overall progress: 1 out of 1 tasks 
1/1: running   [==================================================>] 
verify: Service converged 

# 查看service:也就是container的位置   relicated  1/1 代表此容器水平扩展的数量
[root@localhost ~]# docker service ls
ID                  NAME                MODE                REPLICAS            IMAGE               PORTS
jvomccrnms8c        demo                replicated          1/1                 busybox:latest      
# 查看service(container)具体运行的位置:centos7_three
[root@localhost ~]# docker service ps demo
ID                  NAME                IMAGE               NODE                DESIRED STATE       CURRENT STATE           ERROR               PORTS
sfy5hefhksyw        demo.1              busybox:latest      centos7_three       Running             Running 2 minutes ago      

# 在 centos7_three 节点中查看
[root@centos7_three ~]# docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS               NAMES
112843852d0c        busybox:latest      "sh -c 'while true;d…"   9 minutes ago       Up 9 minutes                            demo.1.sfy5hefhksywi36rhst1zq06w

# 对指定service做水平扩展: 自带恢复能力,如果其中有一个replicated故障,会自动在Cluster中新增加一个replicated节点。 让分子和分母一直可以确保相等。   5ready/5num
[root@localhost lb-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 


# 查看service	 replicated          5/5     	已经ready的数量/要扩展的数量
[root@localhost lb-scale]# docker service ls
ID                  NAME                MODE                REPLICAS            IMAGE               PORTS
jvomccrnms8c        demo                replicated          5/5                 busybox:latest      
# 查看指定的容器
[root@localhost lb-scale]# docker service ls

ID                  NAME                MODE                REPLICAS            IMAGE               PORTS

jvomccrnms8c        demo                replicated          5/5                 busybox:latest      

[root@localhost lb-scale]# docker service ps demo
ID                  NAME                IMAGE               NODE                    DESIRED STATE       CURRENT STATE            ERROR               PORTS
sfy5hefhksyw        demo.1              busybox:latest      centos7_three           Running             Running 15 minutes ago                       
mh1ero6kazrw        demo.2              busybox:latest      centos7_three           Running             Running 2 minutes ago                        
jpynv8ne89xc        demo.3              busybox:latest      centos7_two             Running             Running 2 minutes ago                        
w705h9lauvrv        demo.4              busybox:latest      localhost.localdomain   Running             Running 2 minutes ago                        
mf9y8mg15we5        demo.5              busybox:latest      localhost.localdomain   Running             Running 2 minutes ago      

#  删除指定的Service(后台需要一系列复杂的操作,稍有延迟性)
[root@localhost lb-scale]# docker service rm demo
demo

# 离开ji'qu
docker swarm leave --force


Swarm部署wordpress

# 创建overlay网络:用于不同节点上的容器通信
[root@localhost ~]# docker network create -d overlay demo
2xaj5rg3s1ng1fj3xhju365tq
[root@localhost ~]# docker network ls | grep demo
2xaj5rg3s1ng        demo                overlay             swarm

# 创建mysql 服务
[root@localhost ~]# docker service create --name mysql --env MYSQL_ROOT_PASSWORD=root --env MYSQL_DATABASE=wordpress --network demo --mount type=volume,source=mysql-data,destination=/var/lib/mysql mysql
[root@localhost ~]# docker service ls
ID                  NAME                MODE                REPLICAS            IMAGE               PORTS
w5bgs2khjfdc        mysql               replicated          1/1                 mysql:latest        
[root@localhost ~]# docker service ps mysql
ID                  NAME                IMAGE               NODE                    DESIRED STATE       CURRENT STATE            ERROR               PORTS
ifhi4fb7g0wj        mysql.1             mysql:latest        localhost.localdomain   Running             Running 40 seconds ago              
[root@localhost ~]# docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED              STATUS              PORTS                 NAMES
32a3ea0323d4        mysql:latest        "docker-entrypoint.s…"   About a minute ago   Up About a minute   3306/tcp, 33060/tcp   mysql.1.ifhi4fb7g0wjgensmtfnj0u4l


# 创建wordpress 服务
[root@localhost ~]# docker service create --name wordpress -p 80:80 --env WORDPRESS_DB_PASSWORD=root --env WORDPRESS_DB_HOST=mysql --network demo wordpress

# 访问 centos7_three        http://192.168.31.250  发现可以与mysql通信
[root@localhost ~]# docker service ps wordpress
ID                  NAME                IMAGE               NODE                DESIRED STATE       CURRENT STATE            ERROR               PORTS
bblw244vhc28        wordpress.1         wordpress:latest    centos7_three       Running             Running 10 minutes ago      


作者:Soulboy