Docker Swarm
的scheduler
会选择符合要求的node
来创建container
:
candidates, err := s.selectNodesForContainer(nodes, config, true)
node
定义在scheduler/node/node.go
:
// Node is an abstract type used by the scheduler.
type Node struct {
ID string
IP string
Addr string
Name string
Labels map[string]string
Containers cluster.Containers
Images []*cluster.Image
UsedMemory int64
UsedCpus int64
TotalMemory int64
TotalCpus int64
HealthIndicator int64
}
Cluster.listNodes
方法实现如下:
// listNodes returns all validated engines in the cluster, excluding pendingEngines.
func (c *Cluster) listNodes() []*node.Node {
c.RLock()
defer c.RUnlock()
out := make([]*node.Node, 0, len(c.engines))
for _, e := range c.engines {
node := node.NewNode(e)
for _, pc := range c.pendingContainers {
if pc.Engine.ID == e.ID && node.Container(pc.Config.SwarmID()) == nil {
node.AddContainer(pc.ToContainer())
}
}
out = append(out, node)
}
return out
}
其实就是从Cluster.engines
构建node
列表(因为Cluster.pendingEngines
还处在待定状态)。后续scheduler
就会从这个node
列表中选择合适的node
。