前面我们已经部署好了一个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、内存等)最少的节点,能保证集群中所有节点资源的均匀使用。