// cmd/kubelet/app/server.go

-1、func UnsecuredKubeletDeps(s *options.KubeletServer) (*kubelet.KubeletDeps, error)

  • ....
  • 最后调用return &kubelet.KubeletDeps {

    ....

    NetworkPlugins:  ProbeNetworkPlugins(s.NetworkPluginDir, s.CNIConfDir, s.CNIBinDir),

    ....

  }

// cmd/kubelet/app/plugins.go

// ProbeNetworkPlugins collects all compiled-in plugins

0、func ProbeNetworkPlugins(pluginDir, cniConfDir, cniBinDir string) []network.NetworkPlugin

  • 创建allPlugins := []network.NetworkPlugin{}
  • 若cniConfDir为"",则设置cniConfDir为pluginDir
  • 最后调用allPlugins = append(allPlugins, cni.ProbeNetworkPlugins(cniConfDir, cniBinDir)...) ---> cni.ProbeNetworkPlugins()返回一个cniNetworkPlugin为实例的NetworkPlugin接口
  • allPlugins = append(allPlugins, kubenet.NewPlugin(pluginDir))

// pkg/kubelet/kubelet.go

// NewMainKubelet instantiates a new Kubelet object along with the required internal modules.

// No initialization of Kubelet and its modules should happen here.

1、func NewMainKubelet(kubeCfg *componentconfig.KubeletConfiguraion, kubeDeps *KubeletDeps, standaloneMode bool) (*Kubelet, error)

  • ......
  • 调用mode, err := effectiveHairpinMode(componentconfig.HairpinMode(kubeCfg.HairpinMode), kubeCfg.ContainerRuntime, kubeCfg.NetworkPluginName)
  • 调用plug, err := network.InitNetworkPlugin(kubeDeps.NetworkPlugins, kubeCfg.NetworkPluginName, &criNetworkHost{&networkHost{klet}, &network.NoopPortMappingGetter{}}, klet.hairpinMode, klet.nonMasqueradeCIDR, int(kubeCfg.NetworkPluginMTU))
  • 设置binDir := kubeCfg.CNIBinDir,若binDir为"",则设置binDir = kubeCfg.NetworkPluginDir
  • 设置pluginSettings := dockershim.NetworkPluginSettings{

    HairpinMode:      klet.hairpinMode,

    NonMasqueradeCIDR:   klet.nonMasqueradeCIDR,

    PluginName:       kubeCfg.NetworkPluginName,

    PluginConfDir:       kubeCfg.CNIConfDir,

    PluginBinDir:       binDir,

    MTU:           int(kubeCfg.NetworkPluginMTU),

  }

  • 当kubeCfg.ContainerRuntime != "rkt"并且kubeCfg.EnableCRI时:

    • 设置klet.networkPlugin = nil --> kubelet defers to the runtime shim to setup networking
  • 否则,当kubeCfg.ContainerRuntime为"docker"时,创建runtime := dockertools.NewDockerManager(

    ...

    klet.networkPlugin,

    // If using "kubenet", the Kubernetes network plugin that wraps CNI's bridge plugin, it knows how

    // to set the hairpin veth flag so we tell the container runtime to back away from setting it.If the

    // kubelet is started with any other plugin we can't sure it handles the hairpin case so we instruct

    // the docker runtime to set the flag instead.

    klet.hairpinMode == componentconfig.HairpinVeth && kubeCfg.NetworkPluginName != "kubenet",

    ...

  )

Host, NamespaceGetter, PortMappingGetter结构如下所示:

// Host is an interface that plugins can use to access the kubelet.Plugins, other than kubenet, only require
// a way to access namespace information and port mapping information, which they can do directly through
// the embeded interfaces.
type Host interface {
  // NamespaceGetter is a getter for sandbox information.
  NamespaceGetter
  // PortMappingGetter is a getter for sandbox port mapping information.
  PortMappingGetter
  // LegacyHost contains methods that trap back into the Kubelet. Dependence
  // *do not* add more dependencies in this interface. In a post-cri world,
  // network plugins will be invoked by the runtime shim, and should only
  // require GetNetNS and GetPodPortMappings.
  LegacyHost
} // NamespaceGetter is an interface to retrieve namespace information for a given
// sandboxID. Typically implemented by runtime shims that are closely coupled to
// CNI plugin wrappers like kubenet.
type NamespaceGetter interface {
  // GetNetNS returns network namespace information for the given containerID
  GetNetNS(containerID string) (string, error)
} // PortMappingGetter is an interface to retrieve port mapping information for a given
// sandboxID. Typically implemented by runtime shims that are closely coupled to CNI
// plugin wrappers like kubenet.
type PortMappingGetter interface {
  // GetPodPortMappings returns sandbox port mappings information.
  GetPodPortMappings(containerID string) ([]*hostport.PortMapping, error)
}

  

// pkg/kubelet/network/plugins.go

// InitNetworkPlugin inits the plugin that matches networkPluginName. Plugins must have unique names.

2、func InitNetworkPlugin(plugins []NetworkPlugin, networkPluginName string, host Host, hairpinMode componentconfig.HairpinMode, nonMasqueradeCIDR string, mtu int) (NetworkPlugin, error)

  • 当networkPluginName为""时,默认设置plugin := &NoopNetworkPlugin{},再调用plug.Init(host, hairpinMode, nonMasqueradeCIDR,mtu)并返回return plug, nil
  • 否则创建pluginMap := map[string]NetworkPlugin{},遍历plugins,将plugins都插入到pluginMap中
  • 创建chosenPlugin := pluginMap[networkPluginName],若chosenPlugin不为nil,调用chosenPlugin.Init(host, hairpinMode, nonMasqueradeCIDR, mtu)

------------------------------------------------------- 以cni plugin作为例子 -------------------------------------------------------------------

// pkg/kubelet/network/cni/cni.go

func (plugin *cniNetworkPlugin) Init(host network.Host, hairpinMode componentconfig.HairpinMode, nonMasqueradeCIDR string, mtu int) error

  • 调用plugin.nsenterPath, err = plugin.execer.LookPath("nsenter")
  • 将plugin.host赋值为host
  • 创建一个goroutine,每隔十分钟,调用plugin.syncNetworkConfig()周期性地来检测network config的更新

cniNetworkPlugin数据结构如下:

type cniNetworkPlugin struct {
  network.NoopNetworkPlugin
  loNetwork    *cniNetwork
  sync.RWMutex
  defaultNetwork  *cniNetwork
  host        network.Host
  execer       utilexec.Interface
  nsenterPath    string
  pluginDir      string
  binDir        string
  VendorCNIDirPrefix string 
}

  

// pkg/kubelet/network/cni/cni.go

func (plugin *cniNetworkPlugin) SetUpPod(namespace string, name string, id kubecontainer.ContainerID) error

  • 首先调用plugin.checkInitialized()判断plugin是否初始化完成
  • 调用netnsPath, err := plugin.host.GetNetNS(id.ID)获取namespace对应的net ns的路径
  • 调用_, err = plugin.loNetwork.addToNetwork(name, namespace, id, netnsPath)将pod加入lo network
  • 调用_, err = plugin.getDefaultNetwork().addToNetwork(name, namespace, id, netnsPath)将pod加入default network

Kubernetes网络框架的更多相关文章

  1. Kubernetes网络概念初探

    ------------恢复内容开始------------ Kubernetes网络是Kubernetes中一个核心概念.简而言之,Kubernetes网络模型可以确保集群上所有Kubernetes ...

  2. 关于Unity的网络框架

    注:Unity 5.1里引入了新的网络框架,未来目标应该是WOW那样的,现在还只是个P2P的架子. 网络的框架,无非是如何管理网络数据的收发,通信双方如何约定协议.之前做的框架与GameObject无 ...

  3. 事件驱动之Twsited异步网络框架

    在这之前先了解下什么是事件驱动编程 传统的编程是如下线性模式的: 开始--->代码块A--->代码块B--->代码块C--->代码块D--->......--->结 ...

  4. GJM : Unity3D 常用网络框架与实战解析 【笔记】

    Unity常用网络框架与实战解析 1.Http协议          Http协议                  存在TCP 之上 有时候 TLS\SSL 之上 默认端口80 https 默认端口 ...

  5. Android网络框架源码分析一---Volley

    转载自 http://www.jianshu.com/p/9e17727f31a1?utm_campaign=maleskine&utm_content=note&utm_medium ...

  6. 【从0到1】android网络框架的选型参考

    项目会使用到 socket tcp 级的网络访问,想选取一个使用较成熟异步网络框架, 提到的网络框架: 1. volley, 2. xutils. 3. android 4. netty, 5. mi ...

  7. Twsited异步网络框架

    Twisted是一个事件驱动的网络框架,其中包含了诸多功能,例如:网络协议.线程.数据库管理.网络操作.电子邮件等. Twisted介绍:http://blog.csdn.net/hanhuili/a ...

  8. Android中android-async-http开源网络框架的简单使用

    android-async-http开源网络框架是专门针对Android在Apache的基础上构建的异步且基于回调的http client.所有的请求全在UI线程之外发生,而callback发生在创建 ...

  9. 深入mongoDB(1)--mongod的线程模型与网络框架

    最近工作需要开始研究mongoDB,我准备从其源代码角度,对于mongod和mongos服务的架构.sharding策略. replicaset策略.数据同步容灾.索引等机制做一个本质性的了解.其代码 ...

随机推荐

  1. C# 使用IP端口网络打印图片

    /// <summary> /// POSPrinter的摘要说明. /// 此类处理网络打印,使用了IP端口. /// </summary> public class Net ...

  2. 线程的sleep()方法和yield()方法有什么区别?

    1.sleep()方法给其他线程运行机会时不考虑线程的优先级,因此会给低优先级的线程以运行的机会 2.yield()方法只会给相同优先级或更高优先级的线程以运行的机会 3.线程执行sleep()方法后 ...

  3. jquery 操作input radio 单选框

    1.jquery选中单选框 2.jquery 取消单选框 3.判断是否选中 4.设置不可编辑

  4. Swift培训

    本篇是Swift内部培训整理的PPT材料,主要内容来源于苹果Swift编程语言官方教程,参考了网上的一些视频课程内容.在教程介绍完之后,本人附带实现了一个项目开发中的常用场景的Demo:基于导航栏和T ...

  5. git版本管理之git-ssh 配置和使用

    1.设置用户名和邮箱 $ git config --global user.name "gsx-gh" $ git config --global user.email " ...

  6. Flex上传文件Java端解决中文乱码问题

    1.Flex端进行编码 public function encodeStr(str:String, charSet:String):String { var result:String =" ...

  7. 大数据处理-Trie树

    大数据处理--Trie树 1.1.什么是Trie树 Trie树,即字典树,又称单词查找树或键树,是一种树形结构,是一种哈希树的变种.典型应用是用于统计和排序大量的字符串(但不仅限于字符串),所以经常被 ...

  8. RMAN Restore, Recovery

    Complete recovery: rman target / nocatalog startup mount; restore database; recover database; alter ...

  9. Java异常框架设计

    什么是异常? 异常(exception)应该是异常事件(exceptional event)的缩写.异常定义:异常是一个在程序执行期间发生的事件,它中断正在执行的程序的正常的指令流.当在一个方法中发生 ...

  10. 第八篇:文件共享和使用 dup 函数创建新描述符的区别

    前言 文件共享是指同时打开一个文件 用 dup 函数能对指定文件描述符再创建一个新的描述符,且这个新的描述符和旧的描述符指向的是同一个文件. 这两种行为有什么区别呢?下面给出的两张文件系统的图形象的解 ...