As the Docker community continues to grow, CoreOS, Kubernetes, Hashicorp and other projects flourish. Flannel as an open-source networking component for containers, it also attracts engineers ' attention. The flannel is simple to use and can be configured with a Multi-host network connection only as a consistent store with ETCD belonging to the CoreOS family.
Network schematics for flanned, derived from Https://github.com/coreos/flannel.
You can see that flannel divides the subnet for each host so that multiple containers on multiple hosts can have separate IPs to communicate with each other. This article refers to flannel-0.6.2 to analyze the concrete implementation of flannel.
You can see the flannel source code mainly contains the following directories: BACKEND:IP packet The specific implementation of the forwarding, including UDP, Vxlan, HOSTGW, GCE ..., the default is UDP. Network: Determine the backend type and write the local env file. Subnet: Interact with ETCD to determine subnet. The source code in these directories is described in detail later. Now let's start with the main.go and look at the flannel start-up process.
1 Func Main () {2 //Glog would log to TMP files by default. Override so all entries3 //can flow into Journald (if running under SYSTEMD)4Flag. Set ("Logtostderr","true")5 //Now parse command line args6 flag. Parse ()7 ifFlag. Narg () >0||Opts.help {8Fmt. fprintf (OS. Stderr,"Usage:%s [option]...\n", OS. args[0])9 flag. Printdefaults ()TenOs. Exit (0) One } A ifopts.version { - FMT. Fprintln (OS. Stderr, version. Version) -Os. Exit (0) the } - -Flagutil. Setflagsfromenv (Flag.commandline,"Flanneld") - + //create Subnetmanager to divide subnets -SM, err: =Newsubnetmanager () + ifErr! =Nil { ALog. Error ("Failed to create Subnetmanager:", Err) atOs. Exit (1) - } - //Register for SIGINT and SIGTERM -Log. Info ("Installing signal Handlers") -SIGs: = Make (chan os. Signal,1) - signal. Notify (sigs, OS. Interrupt, Syscall. SIGTERM) in //set the context that can be cancle -CTX, Cancel: =context. Withcancel (context. Background ()) to varrunfunc func (ctx context. Context) + ifOpts.listen! ="" { - ifOpts.remote! ="" { theLog. Error ("--listen and--remote are mutually exclusive") *Os. Exit (1) $ }Panax NotoginsengLog. Info ("running as server") -Runfunc =func (CTX context. Context) { the remote. Runserver (CTX, SM, Opts.listen, Opts.remotecafile, Opts.remotecertfile, Opts.remotekeyfile) + } A}Else { theNM, err: =Network. Newnetworkmanager (CTX, SM) + ifErr! =Nil { -Log. Error ("Failed to create NetworkManager:", Err) $Os. Exit (1) $ } -Runfunc =func (CTX context. Context) { - nm. Run (CTX) the } - }WuyiWG: =sync. waitgroup{} theWg. ADD (1) - go func () { WuRunfunc (CTX)//running the NetworkManager run function in a new goroutine - WG. Done () About }() $<-sigs - //unregister to get default OS nuke behaviour in case we don ' t exit cleanly - signal. Stop (SIGs) -Log. Info ("Exiting ...") A + //after receiving the signal, drop the cancle function and cancel all relevant sub-context the Cancel () - WG. Wait () $}
As can be seen from the above code, according to the parameters listen and remote can be set flannel for the server mode or client mode, server mode to provide services, and can not join the flannel network. The run function of NetworkManager in client mode is the entry for program execution, and the subnet of flannel is set.
The next article will analyze the networkmanager corresponding source code.
For the usage of Go context, please refer to the following two blog posts.
1190000006744213
http://blog.csdn.net/xiaohu50/article/details/49100433
Flannel source Analysis (i) overview