// 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. Redis_发布订阅(Spring Boot)

    目录 前言 生产者和消费者 发布和订阅 Java实现 注意 转至 http://www.tianmaying.com/tutorial/springboot-redis-message 前言 利用Sp ...

  2. 李洪强iOS开发之iOS好文章收集

    李洪强iOS开发之iOS好文章收集 该文收集朋友们转发或自己的写的技术文章,如果你也有相关的好文章,欢迎留言,当好文章多的时候,我会对这些好文章进行分门别类 文章 简述 日期 直播服务配置 使用 ng ...

  3. shader 编程入门(一)

    本系列文章由@浅墨_毛星云 出品,转载请注明出处. 文章链接:http://blog.csdn.net/poem_qianmo/article/details/40723789 作者:毛星云(浅墨)  ...

  4. linux时间同步-NTP服务

    作者:曹世军链接:https://www.zhihu.com/question/30252609/answer/108840850来源:知乎著作权归作者所有.商业转载请联系作者获得授权,非商业转载请注 ...

  5. Unix系统编程(六)write系统调用

    write系统调用将数据写入一个打开的文件. ssize_t write(int fd, void *buffer, size_t count); write调用的参数含义与read调用相类似.buf ...

  6. 大数据处理-Trie树

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

  7. 扩张js的String——trim

    //去掉字符两端的空白字符  String.prototype.Trim=function () {      return this.replace(/(^[\t\n\r]*)|([\t\n\r]* ...

  8. java 理解java的三大特性之继承

    继承定义了类如何相互关联,共享特性.对于若干个相同或者相识的类,我们可以抽象出他们共有的行为或者属相并将其定义成一个父类或者超类,然后用这些类继承该父类,他们不仅可以拥有父类的属性.方法还可以定义自己 ...

  9. 酷狗缓存文件kgtemp的加密方式

    [转自:http://www.cnblogs.com/KMBlog/p/6877752.html] 首先对比了一下缓存文件和下载好的mp3文件,发现缓存文件多了1024个字节,而且对比了几个缓存文件, ...

  10. js 判断数组

    这么基础的东西实在不应该再记录了,不过嘛,温故知新~就先从数据类型开始吧 js六大数据类型:number.string.object.Boolean.null.undefined string: 由单 ...