swarmkit

SwarmKit是用于在任何规模上编排分布式系统的工具包。 它包括节点发现的原语,基于raft的共识,任务调度等。

其主要优点是:

  • 分布式:SwarmKit使用raft共识算法来协调,不依赖单一故障点执行决策。
  • 安全:群集中的节点通信和成员资格是开箱即用的。 SwarmKit使用相互TLS进行节点认证,角色授权和传输加密,自动执行证书颁发和轮换。
  • 简单:SwarmKit操作简单,可以最大限度地减少基础架构依赖。 它不需要外部数据库来操作。

Overview

运行SwarmKit的机器可以组合在一起,以形成一个Swarm,彼此协调任务。 一旦机器加入,它就成为一个Swarm Node。 节点可以是工作节点或管理器节点。

  • Worker Nodes负责使用执行程序运行任务。 SwarmKit配有一个默认的Docker Container Executor,可以方便地交换出来。
  • Manager Nodes另一方面接受来自用户的规范,并负责将所需状态与实际集群状态协调一致。

operator可以通过宣传一名worker到manager或将manager降级给worker来动态更新节点的角色。

服务中组织任务。 服务是更高级别的抽象,允许用户声明一组任务的所需状态。 服务定义应该创建什么类型的任务以及如何执行它们(例如,始终运行这么多副本)以及如何更新它们(例如滚动更新)。

Features

Some of SwarmKit's main features are:

Orchestration

Desired State Reconciliation: SwarmKit不断地将所需状态与当前的集群状态进行比较,并在必要时对两者进行对照。 例如,如果节点出现故障,SwarmKit将其任务重新排列到不同的节点上。

Service Types: 有不同类型的服务。 该项目目前正在与其中的两个开箱即用

  • Replicated Services are scaled to the desired number of replicas.
  • Global Services run one task on every available node in the cluster.

Configurable Updates: 在任何时候,您可以更改服务的一个或多个字段的值。 进行更新后,SwarmKit通过确保所有任务都使用所需的设置来协调所需的状态。 默认情况下,它执行锁步更新 - 即同时更新所有任务。 这可以通过不同的旋钮进行配置:

  • Parallelism 定义可以同时执行多少次更新。
  • Delay设置更新之间的最小延迟。 SwarmKit将通过关闭以前的任务,启动一个新的任务,等待它转换到RUNNING状态,然后等待其他配置的延迟。 最后,它将转移到其他任务上。

Restart Policies: 业务流程层监控任务,并根据指定的策略对故障作出反应。 操作员可以定义重新启动条件,延迟和限制(给定时间窗口中的最大尝试次数)。 SwarmKit可以决定在不同的机器上重新启动任务。 这意味着错误的节点将逐渐消除其任务。

Scheduling

Resource Awareness: SwarmKit知道节点上可用的资源,并将相应地安排任务。

Constraints: operators可以通过定义约束表达式来限制可以调度任务的节点集。 多个约束找到满足每个表达式的节点,即AND匹配。 约束可以匹配下表中的节点属性。 请注意,engine.label是从Docker Engine收集的,具有操作系统,驱动程序等信息。集群管理员为操作目的添加了node.labels。 例如,一些节点具有安全兼容的标签,可以按照符合要求运行任务。

node.id node's ID node.id == 2ivku8v2gvtg4
node.hostname node's hostname node.hostname != node-2
node.ip node's IP address node.ip != 172.19.17.0/24
node.role node's manager or worker role node.role == manager
node.platform.os node's operating system node.platform.os == linux
node.platform.arch node's architecture node.platform.arch == x86_64
node.labels node's labels added by cluster admins node.labels.security == high
engine.labels Docker Engine's labels

engine.labels.operatingsystem == ubuntu 14.04

Strategies: 目前,该项目正在发布一种扩展策略,只要满足约束和资源需求,就会尝试在最不负载的节点上安排任务。

Cluster Management

  • State Store: 管理器节点保持强烈一致,基于复制(基于raft)和集群极快(内存中读取)视图,从而允许他们在容忍故障的同时做出快速的调度决策。
  • Topology Management: 节点角色(Worker / Manager)可以通过API / CLI调用动态更改。
  • Node Management: 操作员可以改变节点的所需可用性:将其设置为“已暂停”将防止任何进一步的任务被调度到其中,而Drained将具有相同的效果,同时也重新调度其他任务(主要用于维护方案)。

Security

  • Mutual TLS: 所有节点使用相互TLS相互通信。 swarm manager作为根证书颁发机构,向新节点颁发证书。
  • Token-based Join: 所有节点都需要加密令牌来加入群集,从而定义了该节点的角色。 令牌可以根据需要频繁地旋转,而不影响已连接的节点。
  • Certificate Rotation: TLS证书在每个节点上都被透明地旋转和重新加载,允许用户设置旋转发生的频率(当前默认值为3个月,最小为30分钟)。

Build

Requirements:

SwarmKit is built in Go and leverages a standard project structure to work well with Go tooling. If you are new to Go, please see BUILDING.md for a more detailed guide.

Once you have SwarmKit checked out in your $GOPATH, the Makefile can be used for common tasks.

From the project root directory, run the following to build swarmd and swarmctl:

$ make binaries

Build the development environment

要构建Swarmkit,您必须设置一个Go开发环境。 How to Write Go Code包含完整的说明。 当设置正确时,您应该在环境中设置GOROOT和GOPATH。

设置Go开发环境后,请使用go get查看swarmkit:

go get -d github.com/docker/swarmkit

This command installs the source repository into the GOPATH.

没有必要使用去查看SwarmKit项目。 但是,为了使这些指令正常工作,您需要将项目检出到GOPATH的正确子目录:$ GOPATH / src / github.com / docker / swarmkit。

Repeatable Builds

对于完整的开发经验,应该进入$ GOPATH / src / github.com / docker / swarmkit。 从那里,常规的go命令,如go test,应该在每个包中工作(please see Developing if they don't work)。

Docker提供了一个Makefile,以方便支持可重复的构建。 make setup将工具安装到GOPATH上,用于开发SwarmKit:

一旦这些命令在GOPATH中可用,运行make来获得完整的构建:

cd github.com/golang/lint/

go install

cd gordonklaus/ineffassign/
go install

$ make binaries

上面提供了一个可重复的构建,使用载入的./vendor目录的内容。 这包括格式化,审查,起绒,建造和测试。 创建的二进制文件将在./bin中可用。

为常见任务提供了几个目标。 有关详细信息,请参阅Makefile。

Update vendored dependencies

To update dependency you need just change vendor.conf file and run vndr tool:

go get github.com/LK4D4/vndr
vndr

It's possible to update only one dependency:

vndr github.com/coreos/etcd v3.0.9

but it should be in sync with vendor.conf.

Also, you can update dependency from fork for testing:

vndr github.com/coreos/etcd <revision> https://github.com/LK4D4/etcd.git

Regenerating protobuf bindings

This requires that you have Protobuf 3.x or higher. Once that is installed the bindings can be regenerated with:

make setup
make generate

NB: As of version 3.0.0-7 the Debian protobuf-compiler package lacks a dependency on libprotobuf-dev which contains some standard proto definitions, be sure to install both packages. This is Debian bug #842158.

[root@mhc swarmkit]# pwd
/root/GoglandProjects/beegoTest/src/github.com/docker/swarmkit
[root@mhc swarmkit]# ls bin
protoc-gen-gogoswarm  swarm-bench  swarmctl  swarmd  swarm-rafttool


Usage Examples

Setting up a Swarm

These instructions assume that swarmd and swarmctl are in your PATH.

(Before starting, make sure /tmp/node-N don't exist)

Initialize the first node:

$ swarmd -d /tmp/node-1 --listen-control-api /tmp/node-1/swarm.sock --hostname node-1

Before joining cluster, the token should be fetched:

$ export SWARM_SOCKET=/tmp/node-1/swarm.sock  
$ swarmctl cluster inspect default  
ID          : 87d2ecpg12dfonxp3g562fru1
Name        : default
Orchestration settings:
  Task history entries: 5
Dispatcher settings:
  Dispatcher heartbeat period: 5s
Certificate Authority settings:
  Certificate Validity Duration: 2160h0m0s
  Join Tokens:
    Worker: SWMTKN-1-3vi7ajem0jed8guusgvyl98nfg18ibg4pclify6wzac6ucrhg3-0117z3s2ytr6egmmnlr6gd37n
    Manager: SWMTKN-1-3vi7ajem0jed8guusgvyl98nfg18ibg4pclify6wzac6ucrhg3-d1ohk84br3ph0njyexw0wdagx

在两个附加终端中,加入两个节点。 从下面的示例中,使用第一个节点的地址替换127.0.0.1:4242,并使用上面获取的<Worker Token>。 在这个例子中,
<Worker Token>是SWMTKN-1-3vi7ajem0jed8guusgvyl98nfg18ibg4pclify6wzac6ucrhg3-0117z3s2ytr6egmmnlr6gd37n。 如果加入节点在与节点1相同的主机上运行,
请选择其他远程侦听端口,例如--listen-remote-api 127.0.0.1:4343。

If joining as a manager, also specify the listen-control-api.
$ swarmd -d /tmp/node-4 --hostname node-4 --join-addr 127.0.0.1:4242 --join-token <Manager Token> --listen-control-api /tmp/node-4/swarm.sock --listen-remote-api 127.0.0.1:4245

在第四个终端中,使用swarmctl来探索和控制集群。 运行swarmctl之前,将SWARM_SOCKET环境变量设置为在启动管理器时在-listen-control-api中指定的manager套接字的路径。
To list nodes:
$ export SWARM_SOCKET=/tmp/node-1/swarm.sock
$ swarmctl node ls
ID                         Name    Membership  Status  Availability  Manager Status
--                         ----    ----------  ------  ------------  --------------
3x12fpoi36eujbdkgdnbvbi6r  node-2  ACCEPTED    READY   ACTIVE
4spl3tyipofoa2iwqgabsdcve  node-1  ACCEPTED    READY   ACTIVE        REACHABLE *
dknwk1uqxhnyyujq66ho0h54t  node-3  ACCEPTED    READY   ACTIVE
zw3rwfawdasdewfq66ho34eaw  node-4  ACCEPTED    READY   ACTIVE        REACHABLE

Creating Services

Start a redis service:

swarmctl service create --name redis --image consul:0.8.4

[root@mhc bin]# swarmctl service ls
ID                         Name   Image         Replicas
--                         ----   -----         --------
upfohowk6xj4wu0ch6ajvbgx5  redis  consul:0.8.4  1/1
[root@mhc bin]# docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED              STATUS              PORTS                                                        NAMES
00b40ca561f5        consul:0.8.4        "docker-entrypoint.sh"   About a minute ago   Up About a minute   8300-8302/tcp, 8500/tcp, 8301-8302/udp, 8600/tcp, 8600/udp   redis.1.e8rzq3bz3ehf8q9rn5iay6fz4

[root@mhc bin]# swarmctl service inspect redis
ID                : upfohowk6xj4wu0ch6ajvbgx5
Name              : redis
Replicas          : 1/1
Template          
 Container        
  Image           : consul:0.8.4

Task ID                      Service    Slot    Image           Desired State    Last State               Node
-------                      -------    ----    -----           -------------    ----------               ----
e8rzq3bz3ehf8q9rn5iay6fz4    redis      1       consul:0.8.4    RUNNING          RUNNING 2 minutes ago    mhc

Updating Services

You can update any attribute of a service.

For example, you can scale the service by changing the instance count:

[root@mhc bin]# swarmctl service inspect redis
ID                : upfohowk6xj4wu0ch6ajvbgx5
Name              : redis
Replicas          : 1/1
Template          
 Container        
  Image           : consul:0.8.4

Task ID                      Service    Slot    Image           Desired State    Last State               Node
-------                      -------    ----    -----           -------------    ----------               ----
e8rzq3bz3ehf8q9rn5iay6fz4    redis      1       consul:0.8.4    RUNNING          RUNNING 2 minutes ago    mhc
[root@mhc bin]# swarmctl service update redis --replicas 3
upfohowk6xj4wu0ch6ajvbgx5
[root@mhc bin]# swarmctl service ls
ID                         Name   Image         Replicas
--                         ----   -----         --------
upfohowk6xj4wu0ch6ajvbgx5  redis  consul:0.8.4  1/3
[root@mhc bin]# swarmctl service ls
ID                         Name   Image         Replicas
--                         ----   -----         --------
upfohowk6xj4wu0ch6ajvbgx5  redis  consul:0.8.4  3/3
[root@mhc bin]# docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                                                        NAMES
b192f8ea48d8        consul:0.8.4        "docker-entrypoint.sh"   15 seconds ago      Up 12 seconds       8300-8302/tcp, 8500/tcp, 8301-8302/udp, 8600/tcp, 8600/udp   redis.2.7uyq4kf7im0zralfjlwfx2w5e
36c3500f8fb7        consul:0.8.4        "docker-entrypoint.sh"   18 seconds ago      Up 12 seconds       8300-8302/tcp, 8500/tcp, 8301-8302/udp, 8600/tcp, 8600/udp   redis.3.u9t5w4hdbyfvy9k7jrgejxpkn
00b40ca561f5        consul:0.8.4        "docker-entrypoint.sh"   6 minutes ago       Up 6 minutes        8300-8302/tcp, 8500/tcp, 8301-8302/udp, 8600/tcp, 8600/udp   redis.1.e8rzq3bz3ehf8q9rn5iay6fz4

将副本从1更改为6,强制SwarmKit创建5个附加任务,以符合所需的状态。

每个其他字段也可以更改,如图像,args,env,...

让我们从redis更改图像:3.0.5到redis:3.0.6(例如升级):

$ swarmctl service update redis --image redis:3.0.6

默认情况下,所有任务都会同时更新。

可以通过定义更新选项来更改此行为。

例如,为了一次更新任务2,并在更新之间等待至少10秒钟:
$ swarmctl service update redis --image redis:3.0.7 --update-parallelism 2 --update-delay 10s
$ watch -n1 "swarmctl service inspect redis"  # watch the update
这将更新2个任务,等待它们运行,然后再等待10秒,然后再转到其他任务。

可以在创建服务时设置更新选项,稍后更新。 如果update命令没有指定更新选项,则将使用最后一组选项。

Node Management

SwarmKit监视节点运行状况。 在节点故障的情况下,它将任务重新安排到其他节点。

操作员可以手动定义节点的可用性,并可以暂停和排除节点。

我们把节点1放到维护模式中:

$ swarmctl node drain node-1

$ swarmctl node ls
ID                         Name    Membership  Status  Availability  Manager Status
--                         ----    ----------  ------  ------------  --------------
3x12fpoi36eujbdkgdnbvbi6r  node-2  ACCEPTED    READY   ACTIVE
4spl3tyipofoa2iwqgabsdcve  node-1  ACCEPTED    READY   DRAIN         REACHABLE *
dknwk1uqxhnyyujq66ho0h54t  node-3  ACCEPTED    READY   ACTIVE

$ swarmctl service inspect redis
ID                   : 08ecg7vc7cbf9k57qs722n2le
Name                 : redis
Replicas             : 6/6
Update Status
 State               : COMPLETED
 Started             : 2 minutes ago
 Completed           : 1 minute ago
 Message             : update completed
Template
 Container
  Image              : redis:3.0.7

Task ID                      Service    Slot    Image          Desired State    Last State                Node
-------                      -------    ----    -----          -------------    ----------                ----
8uy2fy8dqbwmlvw5iya802tj0    redis      1       redis:3.0.7    RUNNING          RUNNING 23 seconds ago    node-2
7h9lgvidypcr7q1k3lfgohb42    redis      2       redis:3.0.7    RUNNING          RUNNING 2 minutes ago     node-3
ae4dl0chk3gtwm1100t5yeged    redis      3       redis:3.0.7    RUNNING          RUNNING 23 seconds ago    node-3
9fz7fxbg0igypstwliyameobs    redis      4       redis:3.0.7    RUNNING          RUNNING 2 minutes ago     node-3
drzndxnjz3c8iujdewzaplgr6    redis      5       redis:3.0.7    RUNNING          RUNNING 23 seconds ago    node-2
7rcgciqhs4239quraw7evttyf    redis      6       redis:3.0.7    RUNNING          RUNNING 2 minutes ago     node-2

如您所见,每个在节点1上运行的任务由对帐循环重新平衡到节点2或节点3。


原文地址:https://www.cnblogs.com/mhc-fly/p/7692036.html