The original text of the QQ space Link: http://user.qzone.qq.com/29185807/blog/1462949671 The original article csdn blog link: http://blog.csdn.net/screscent/ article/details/51374524
1, the construction process
Let's look at the construction of cluster first.
Code in Swarm\manage.go
Initialization of a cluster object
Update nodes
In addition, the cluster object is also included in the Scheduler and API server.
Explain the importance of cluster
2, Cluster
Code in Swarm\cluster\cluster.go
First look at the structural body
The two most important things in the room.
1, store, this previous article has been analyzed
2, nodes, the part that holds the node information in the cluster and communicates with the node
Look at the Newcluster function.
Now let's take a look at some of the interfaces cluster provides
Updatenodes
This is an important interface for docking with Discovery for node updates
The incoming node, traverse, connect (connect), and add (AddNode)
AddNode
Add node and store the information through the map to determine whether to add it repeatedly. function is simple
Events
Used to set the EventHandler. In the build process rollup, set up a Loghandler
Deploycontainer
Deploy a container
Create a container through node and save the information in the store after it is successfully created. Destroycontainer
Destroy a container.
Destroys container from node and, when successful, removes its information from the store.
Summary
Cluster's function is simple, it belongs to a simple management function. The specific action is still in node.
3, Node
Let's look at the structure first.
Code in Swarm\cluster\node.go
Some of the more critical things are
Containers: Used to record containers deployed on the current node
Images: Used to record mirror information on the current node
Client: This is the key, the interface for node connection
Now let's look at the container structure.
Code in Swarm\cluster\container.go
Let's look at some of the operations of node NewNode
Create node
Is the initialization of some basic parameters
Connect
After the node is built, connect is called to the node
A dockerclient is built in the code above to connect to node
For the process in the code above, let's do an analysis of one of the following
Updatespecs
In fact, this is where the members in the node structure are initialized.
Gets the Id,name,cpus, and so on, and initializes it to the node structure body
refreshcontainers
Update the container information on the node.
Obtain the container list information via dockerclient.
Updatecontainer
In the above refreshcontainers, just get to the container list, the real update is in Updatecontainer this function
After obtaining the container information, determine if the record is already there.
If there is no record, the dockerclient will be called again. Inspectcontainer get container information.
refreshimages
Used to update images information.
Call Dockerclient, get images list, and update
Refreshloop
This is a dead loop.
There are two Chan signals.
1, container Information update sync signal. The following figure is the source of the signal
2, Force timeout container Information Update Sync signal, the following figure is the timeout time
Continue with the refresh loop
If an error occurs, the images is refreshed.
If you still have an error, judge the node disconnet and print the log.
If err is not, then the healthy is determined, and if not, the reset is done. Call Stopallmonitorevents First, then call startmonitorevents
Handler
Now let's look at handler. This is critical. This provides an interface for some operations. The callback used for dockerclient. We need to see the dockerclient source.
4, Dockerclient
Code in Github.com\samalba\dockerclient\dockerclient.go
Look at the structure first.
As you can see from the above, it is a package for httpclient newdockerclient
Constructs the docketclient structure body, its most main or constructs the HttpClient
startmonitorevents
Let's take a look at what this function is for.
Here's a bit of a sign. And then called the GetEvents
Here, like node, a GET request for events was launched to obtain the event. By decoding, get event information, and then call the node in the Dockerclient to handler for event callback processing. stopallmonitorevents
Just set a bit of a flag
listimages