// 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. Xcode7 beta 网络请求报错:The resource could not be loaded because the App Transport Security policy requir

          今天升级Xcode 7.0 bata发现网络访问失败.输出错误信息 The resource could not be loaded because the App Transport S ...

  2. iOS swift 关于自定义表情键盘

    目录 输入框 键盘监听 键盘切换 表情装载 表情加载 表情输入 表情输出 表情显示 结束语 demo下载 demo图片: 输入框 为了让输入框能够随着用户输入内容变化自动变化高度,这里的输入框使用UI ...

  3. Mysql下Union注入Bypass安全狗过程

    文章转载于:http://www.0aa.me/index.php/archives/95/ 一次众测发现个注入,然后有安全狗就顺带看了下安全狗. 先fuzz看看安全狗拦截什么关键词union sel ...

  4. golang模板语法简明教程

    [模板标签] 模板标签用"{{"和"}}"括起来   [注释] {{/* a comment */}} 使用“{{/*”和“*/}}”来包含注释内容   [变量 ...

  5. Xcode模拟iPhone教程!

    iOS 开发者常常会使用模拟器来进行调试,当然这就少不了Mac电脑中的Xcode软件了,今天PC6小编就给大家带来在Mac系统下如何快速启动iOS模拟器的使用教程: 一.如何启动iOS模拟器 1.在L ...

  6. 【Android Studio】之构建项目报错

    问题1: 报错: Could not download fastutil.jar (it.unimi.dsi:fastutil:7.2.0): No cached version available ...

  7. FAT,FAT32,NTFS单目录文件数量限制

    http://hi.baidu.com/huaxinchang/item/5ba53ba9b29631756dd4551b —————————————————————————————————————— ...

  8. pl/sql 实例精解 05

    本章主要讨论 case 语句 1: case [SELECTOR] 2: when expression 1 then statement 1; 3: when expression 2 then s ...

  9. 获取openid

    请将文件MP_verify_IbWggk5i4gWgrast.txt上传至mp.weixin.qq.com指向的web服务器(或虚拟主机)的目录 怎么处理的?//将txt文件上传到你填的域名的根目录 ...

  10. $.messager.show扩展:指定位置显示

    扩展了个$.messager.showBySite,根据舍得的位置显示$.messager.show.代码如下: /** * 指定位置显示$.messager.show * options $.mes ...