The original of this article QQ Space link: http://user.qzone.qq.com/29185807/blog/1463015882
The original article csdn blog link: http://blog.csdn.net/screscent/article/details/51381242
The previous analysis of Discovery,cluster,node. Then this article analyzes scheduler and APIs.
1, Scheduler
Let's review how the scheduler is built in manage.
Source code in Swarm\manage.go
Strategy and filter are constructed first.
The scheduler is then constructed.
Here's a look at scheduler's source.
Source code in Swarm\scheduler\scheduler.go
The structure is simple, with 3 main members
Here's a quick look at the interface Scheduler provides CreateContainer
First select node and then call the deployment in cluster
The choice has two parts, one is filter, one is strategy Removecontainer
To delete directly through the cluster
Summary
The surface function of scheduler is still very simple. The main thing is how to choose node.
2, Strategy
When it comes to choosing node, let's take a look at strategy.
Source code in Swarm\scheduler\strategy\strategy.go
Through the plug-in way, provides the choice of two modes. The above code is two plug-in initialization registration
Use name to get plugin 2.1, binpacking
Source code in Swarm\scheduler\strategy\binpacking.go
is an empty structure.
So look at the provided interface Placecontainer
Iterate through all of the node and then determine if memory and CPUs meet the criteria
Then the memory and the CPU are calculated fractions, and the qualifying is placed in a queue.
Finally, the queue is sorted and the best one is returned.
2.2, Random
Source code in Swarm\scheduler\strategy\random.go
The structure is still empty.
The following main look at the interface provided by the Placecontainer
Here is a random return node. function is simple
3, Filter
Except strategy, and filter.
Source code in Swarm\scheduler\filter\filter.go
is still designed through plug-in mode, offering a total of 5 types of patterns.
The initialization of the filter is done by names. Unlike strategy here, strategy is only allowed to select one plug-in, but filter can work with multiple
This is the work of filter
3.1, Affinity
Source code in Swarm\scheduler\filter\affinity.go
Affinityfilter is an empty structure, mainly looking at the interface provided by it
First get the affinity settings
The nodes is then followed by traversal processing. The image above is match for container
Then there is the match for image.
The match to the nodes, and then the next round of affinity matching. Loops match until all the affinity are matched completely.
3.2, Health
Source code in Swarm\scheduler\filter\health.go
Go straight through all the nodes and tell the healthy node to get rid of it.
3.3, constraint
Source code in Swarm\scheduler\filter\constraint.go
This is similar to affinity and gets the constraint settings first.
Then loop match for nodes until all the constraint are matched.
3.4, Port
Source code in Swarm\scheduler\filter\port.go
This is also very simple to determine if the port in node is already occupied.
3.5, Dependency
Source code in Swarm\scheduler\filter\dependency.go
Get link information with other container first
And then get the port information to connect to the other container
Then iterate over the nodes and check each nodes container to see if it contains link and net first-off container. Will all contain the return nodes.
3.6 Summary
The filter function is still very clear, and each module function is relatively simple.
4, Api
It's the last place. Let's take a look at how the API is initialized in manage.
Code in Swarm\manage.go
The inside will cluster, Scheduler, all passed into the API
Continue to look at the API below
4.1 Initialization
Code in Swarm\api\server.go
There are a few things on it that are more critical.
1, Context: Contains all the information, Cluster,scheduler,eventhandler, etc.
2, Createrouter: This is the key entrance.
Different listenr are then initialized according to different types of initialization.
4.2 Router
Code in Swarm\api\api.go
A MUX is built in the diagram above. Router (Github.com/gorilla/mux This code is analyzed in the previous Docker source analysis)
It then constructs a lot of the routing for Get requests and handler
Then there is post, delete, Options route, and corresponding handler. Some of these handler are not implemented.