您的位置:首页 > 运维架构 > Docker

Docker Swarm集群实践——管理篇

2016-07-20 09:59 701 查看
前面我们已经部署好了一个Docker Swarm集群环境,接下来,我们就对Swarm集群的相关管理进行简单介绍。

集群调度策略

既然是集群,就是有一个调度策略,也就是该集群包含那么多子节点,我到底是设置一个什么样的策略来进行分配呢?

我们查看Docker官方文档可以看到Swarm的集群调度包含三种策略:

To choose a ranking strategy, pass the 
--strategy
 flag
and a strategy value to the 
swarm manage
 command.
Swarm currently supports these values:
spread

binpack

random


The spread and binpack strategies compute rank according to a node’s available CPU, its RAM, and the number of containers it has. The random strategy uses no computation. It selects a node at random and is primarily intended for debugging.

Your goal in choosing a strategy is to best optimize your cluster according to your company’s needs.

Under the spread strategy, Swarm optimizes for the node with the least number of containers. The binpack strategy causes Swarm to optimize for the node which is most packed. Note that a container occupies resource during its life cycle, including exited state. Users should be aware of this condition to schedule containers. For example, spread strategy only checks number of containers disregarding their states. A node with no active containers but high number of stopped containers may not be selected, defeating the purpose of load sharing. User could either remove stopped containers, or start stopped containers to achieve load spreading. The random strategy, like it sounds, chooses nodes at random regardless of their available CPU or RAM.

Using the spread strategy results in containers spread thinly over many machines. The advantage of this strategy is that if a node goes down you only lose a few containers.

The binpack strategy avoids fragmentation because it leaves room for bigger containers on unused machines. The strategic advantage of binpack is that you use fewer machines as Swarm tries to pack as many containers as it can on a node.


简单总结一下:

random策略:随机选择节点。一般用于开发测试阶段。
spread策略:默认策略,swarm优先选择占用资源(如CPU、内存等)最少的节点,能保证集群中所有节点资源的均匀使用。 
binpack策略:与spread相反,它的目的是尽可能地填满一个节点,以保证更多空余的节点。

具体操作在启动Swarm Manager添加策略参数
root@controller:~# docker run -p 2376:2375 -d swarm manage token://88b70a0603a97f3e51be1d83f471a1df --strategy random
b1f075e99d5a3fab795a2d3c33f8229e3d06a142dd421651e05af08279fb7488
查看一下集群节点信息,可以看到策略已经修改了Random。



接下来,我们创建容器实例就默认随机调度。

Swarm过滤

Swarm集群在创建时候不仅可以通过设定策略进行自动化调度,也可以通过设置过滤条件,进行一些自定义化的设置。Filters分两类:
节点过滤器
容器过滤器


节点过滤器:以Docker Damon的配置或Docker主机的特性为条件进行过滤的,它包括: 

- constraint 

- health

容器过滤器:以Docker容器的配置作为条件过滤,它包括: 

- affinity 

- dependency 

- port

使用
容器过滤器
时,过滤条件将应用于所有容器,包括stopped状态的容器。 容器过滤器中dependency和port两种,会利用docker运行容器时的相关参数自动选择符合条件的节点,不需额外指定参数。

使用Contraint过滤

使用Contraint过滤需要为集群服务器使用tag或者添加label,例如对每一个集群的/etc/default/docker设置lable。

DOCKER_OPTS="-H 0.0.0.0:2375 -H unix:///var/run/docker.sock --label node=controller --insecure-registry 192.168.12.132:5000"

其他机器修改自己的机器名:--label node=本机机器名

重启一下docker服务

我们查看swarm manager节点信息如上,红框标识。

1、我们默认创建N次容器实例

docker -H 192.168.12.132:2376 run -id ubuntu:14.04

查看各个容器节点的docker ps -a,可以看到随机看到在各个机器上生成实例。

2、例如只在设定的某个节点创建实例,例如只在controller132节点创建
root@controller:~# docker -H 192.168.12.132:2376 run -id  -e constraint:node==controller  ubuntu:14.04


我们的限制不仅仅只是过滤指定的节点,还可以实现类似于存储驱动、内核版本,OS等相关的过滤。

storagedriver

executiondriver

kernelversion

operatingsystem


查看controller机器可以看到全部都创建在该机器上
root@controller:~# docker ps -a
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                    NAMES
6bc9f97d5c95        ubuntu:14.04        "/bin/bash"              12 seconds ago      Up 11 seconds                                stoic_golick
9fad12f8c073        ubuntu:14.04        "/bin/bash"              31 seconds ago      Up 30 seconds                                silly_murdock
eb6a03627e7a        ubuntu:14.04        "/bin/bash"              2 minutes ago       Up 2 minutes                                 tiny_wing
b1f075e99d5a        swarm               "/swarm manage token:"   18 minutes ago      Up 18 minutes       0.0.0.0:2376->2375/tcp   angry_mirzakhani
4bc94ec9f4a2        swarm               "/swarm join -addr=19"   19 minutes ago      Up 19 minutes       2375/tcp                 jovial_mcnulty


3、我们可以通过docker -H swarm-manager-ip:port  command来查看集群的容器信息,command可以是docker的相关命令,例如
root@controller:~# docker -H 192.168.12.132:2376 ps -a
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                           NAMES
6bc9f97d5c95        ubuntu:14.04        "/bin/bash"              5 minutes ago       Up 5 minutes                                        controller/stoic_golick
9fad12f8c073        ubuntu:14.04        "/bin/bash"              5 minutes ago       Up 5 minutes                                        controller/silly_murdock
eb6a03627e7a        ubuntu:14.04        "/bin/bash"              7 minutes ago       Up 7 minutes                                        controller/tiny_wing
6f24519e302a        ubuntu:14.04        "/bin/bash"              7 minutes ago       Up 7 minutes                                        docker2/condescending_mcclintock
2ae50d7963ca        ubuntu:14.04        "/bin/bash"              7 minutes ago       Up 7 minutes                                        docker1/goofy_cray
b1f075e99d5a        swarm               "/swarm manage token:"   23 minutes ago      Up 23 minutes       192.168.12.132:2376->2375/tcp   controller/angry_mirzakhani
9065ee5166cd        swarm               "/swarm join -addr=19"   24 minutes ago      Up 24 minutes       2375/tcp                        docker2/small_davinci
0bd66f118d09        swarm               "/swarm join -addr=19"   24 minutes ago      Up 24 minutes       2375/tcp                        docker1/adoring_boyd
4bc94ec9f4a2        swarm               "/swarm join -addr=19"   24 minutes ago      Up 24 minutes       2375/tcp                        controller/jovial_mcnulty


使用health过滤

当节点down机了,或无法与Cluster通信了,表示节点处于unhealth状态。 

health过滤条件在集群启动时指定通过
--filter=health
指定,它强制集群在后续操作选择节点时使用health状态的节点。

使用Affinity Filter
affinity过滤器,用于选择满足以下条件(具有相关性)的节点: 
- container(容器name或id) 
- images(镜像名称) 
- label(容器上的自定义label)

使用-e affinity:container==container_name / container_id –-name container_1可以让容器container_1紧挨着容器container_name / container_id执行,也就是说两个容器在一个node上面执行

当然,Affinity Filter的使用场景还有很多,例如:通过-e affinity:image=image_name命令可以指定只有已经下载了image_name的机器才运行容器


使用dependency过滤

在运行容器时,可以指定以下三种依赖: 

- –volumns-from=dependency 

- –link=dependency:alias 

- –net=container:dependency

使用swarm创建容器时,会自动根据满足这三种依赖的主机环境作为容器运行的节点。


使用端口过滤

在运行容器时,通过
-p
指定主机与容器的端口映射,在swarm环境中,会自动选择指定端口可用(未被占用)的主机,避免端口冲突。

Filters表达式

上述过滤器,constraints和affinity可以通过
-e
参数额外指定Filter表达式。<filter-type>:<key><operator><value>

filter-type: constraints和affinity两种
key: container、node、节点默认tag、节点或容器自定义label
operator:包括`==`、`!=`两种,可在后面加'~'表示软匹配,在条件无匹配时,自动忽略过滤条件
value:字符串表达式(字符、数据、点号、下划线等组成),可以是
全局匹配 —— abc*
正则匹配 —— /node\d/

示例:

constraint:node==node1 matches node node1.
constraint:node!=node1 matches all nodes, except node1.
constraint:region!=us* matches all nodes outside with a region tag prefixed with us.
constraint:node==/node[12]/ matches nodes node1 and node2.
constraint:node==/node\d/ matches all nodes with node + 1 digit.
constraint:node!=/node-[01]/ matches all nodes, except node-0 and node-1.
constraint:node!=/foo[bar]/ matches all nodes, except foo[bar]. You can see the use of escape characters here.
constraint:node==/(?i)node1/ matches node node1 case-insensitive. So NoDe1 or NODE1 also match.
affinity:image==~redis tries to match for nodes running container with a redis image
constraint:region==~us* searches for nodes in the cluster belonging to the us region
affinity:container!=~redis* schedule a new redis5 container to a node without a container that satisfies redis*

更多的参考:https://docs.docker.com/swarm/scheduler/filter/
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: