SwarmKit项目是Docker公司6月7日开源的一个项目,主要用来提供容器集群以及编排能力。如下图所示,SwarmKit通过Containerd类似的方式接入DockerEngine,最终通过新的Docker API对外提供容器集群服务。此特性最终会在Docker 1.12(06/14/2016)版本得到体现。
SwarmKit中有两种角色,Manager和Worker。Manager主要管理节点、调度任务。Worker主要通过Executor来执行任务,当前缺省的Executor为Docker Container Executor。包含了一下特点:
内部细节如下图所示:
3.1 编译
(1)下载代码
$ git clone https://github.com/docker/swarmkit.git
(2)编译(需要go1.6以上的版本)
$ make binaries
编译完成后,会生成两个程序,swarmd和swarmctl,swarmd是一个swarmkit daemon程序,用来运行manager和worker。swarmctl是一个命令行工具,用来访问swarm manger,当然最终在Docker1.12可通过Docker命令访问。swarmctl子命令有node、service、task、network、cluster,分别用来提供节点管理、service管理、任务管理、网络管理、集群管理。
$ swarmctl --help
Control a swarm cluster
Usage:
swarmctl [command]
Available Commands:
node Node management
service Service management
task Task management
version Print version number of swarm.
network Network management
cluster Cluster management
Flags:
-n, --no-resolve Do not try to map IDs to Names when displaying them
-s, --socket string Socket to connect to the Swarm manager (default "/var/run/docker/cluster/docker-swarmd.sock")
3.2 集群初始化(三节点环境)
(1)第一个节点启动Manger
$ swarmd -d /tmp/node-1 --listen-control-api /tmp/manager1/swarm.sock --hostname node-1
(2)其他两个节点运行worker
$ swarmd -d /tmp/node-2 --hostname node-2 --join-addr node-1:4242
$ swarmd -d /tmp/node-3 --hostname node-3 --join-addr node-1:4242
(3)通过swarmctl命令查看节点信息
$ export SWARM_SOCKET=/tmp/manager1/swarm.sock
$ swarmctl node ls
ID Name Status Availability
87pn3pug404xs4x86b5nwlwbr node-1 READY ACTIVE
by2ihzjyg9m674j3cjdit3reo node-2 READY ACTIVE
87pn3pug404xs4x86b5nwlwbr node-3 READY ACTIVE
3.3 创建Service
(1)通过service create命令创建服务
$ swarmctl service create --name redis --image redis:3.0.5
89831rq7oplzp6oqcqoswquf2
(2)查看service
$ swarmctl service ls
ID Name Image Instances
-- ---- ----- ---------
89831rq7oplzp6oqcqoswquf2 redis redis:3.0.5 1
(3)通过inspect查看service详细信息
$ swarmctl service inspect redis
ID : 89831rq7oplzp6oqcqoswquf2
Name : redis
Instances : 1
Template
Container
Image : redis:3.0.5
Task ID Service Instance Image Desired State Last State Node
------- ------- -------- ----- ------------- ---------- ----
0dsiq9za9at3cqk4qx07n6v8j redis 1 redis:3.0.5 RUNNING RUNNING 2 seconds ago node-1
3.4 更新Service
通过service update命令可更新Service,例如Service个数、服务升级、内存、网络等信息。
(1)更新个数
$ swarmctl service update redis --instances 6
89831rq7oplzp6oqcqoswquf2
$ swarmctl service inspect redis
ID : 89831rq7oplzp6oqcqoswquf2
Name : redis
Instances : 6
Template
Container
Image : redis:3.0.5
Task ID Service Instance Image Desired State Last State Node
------- ------- -------- ----- ------------- ---------- ----
0dsiq9za9at3cqk4qx07n6v8j redis 1 redis:3.0.5 RUNNING RUNNING 1 minute ago node-1
9fvobwddp5ve3k0f4al1mhuhn redis 2 redis:3.0.5 RUNNING RUNNING 3 seconds ago node-2
e7pxax9mhjd4zamohobefqpy0 redis 3 redis:3.0.5 RUNNING RUNNING 3 seconds ago node-2
ceuwhcffcavur7k9q57vqw0zg redis 4 redis:3.0.5 RUNNING RUNNING 3 seconds ago node-1
8vqmbo95l6obbtb7fpmvz522f redis 5 redis:3.0.5 RUNNING RUNNING 3 seconds ago node-3
385utv15nalm2pyupao6jtu12 redis 6 redis:3.0.5 RUNNING RUNNING 3 seconds ago node-3
(2)更新镜像,实现升级
$ swarmctl service update redis --image redis:3.0.6
89831rq7oplzp6oqcqoswquf2
$ swarmctl service inspect redis
ID : 89831rq7oplzp6oqcqoswquf2
Name : redis
Instances : 6
Template
Container
Image : redis:3.0.6
Task ID Service Instance Image Desired State Last State Node
------- ------- -------- ----- ------------- ---------- ----
7947mlunwz2dmlet3c7h84ln3 redis 1 redis:3.0.6 RUNNING RUNNING 34 seconds ago node-3
56rcujrassh7tlljp3k76etyw redis 2 redis:3.0.6 RUNNING RUNNING 34 seconds ago node-1
8l7bwrduq80pkq9tu4bsd95p4 redis 3 redis:3.0.6 RUNNING RUNNING 36 seconds ago node-2
3xb1jxytdo07mqccadt06rgi0 redis 4 redis:3.0.6 RUNNING RUNNING 34 seconds ago node-1
16aate5akcimsye9cp5xis1ih redis 5 redis:3.0.6 RUNNING RUNNING 34 seconds ago node-2
dws408a3gz0zx0bygq3aj0ztk redis 6 redis:3.0.6 RUNNING RUNNING 34 seconds ago node-3
SwarmKit是容器集群领域的新生儿,直接集成在Docker Engine中,通过新的docker api提供集群服务。swarm、swarmkit、k8s、mesos容器集群,群雄逐鹿,谁能胜出,让我们拭目以待。
===========================
作者简介:线超博,华为IT云计算架构与设计部高级工程师,从事云计算方向的技术研究,当前主要负责Docker相关技术在云计算领域的技术研究和实践。2015年初开始关注Docker Swarm项目,并积极参与社区贡献,成为国内第一位Docker社区Maintainer
学习交流方式 xianchaobo@gmail.com