// 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. 2015·Fool's Day·NND

    本博文没有主旨,仅仅是记录. ============================ Date:2015/4/1 - April Fool's Day! Addr:ZhongHai ======== ...

  2. 156. Merge Intervals【easy】

    Given a collection of intervals, merge all overlapping intervals.   Example Given intervals => me ...

  3. EM5-PE2B

    1. vocabulary once adv. 一次,曾经 They cocktailed once every week. He once lived in shanghai. twice adv ...

  4. SMP

    SMP(Symmetrical Multi-Processing):对称多处理技术,是指在一个计算机上汇集了一组处理器(多CPU),各CPU之间共享内存子系统以及总线结构. 在smp系统中,所有的CP ...

  5. nyoj 742 子串和再续 类似 HDU 1024

    子串和再续 时间限制:1000 ms  |  内存限制:65535 KB 难度:4 描述 给你一个序列 S1, S2, S3, S4 ... Sx, ... Sn (1 ≤ x ≤ n ≤ 1,000 ...

  6. [Delphi] 常用字符集简介

    转载 http://www.cnblogs.com/yangyxd/articles/4778483.html 字符集 ANSI (ASCII)美国信息互换标准编码 GB 2312信息交换用汉字编码字 ...

  7. 脚本 sh 和 ./ 的区别,exec和source

    如果.不在PATH里面,要执行当前目录下的可执行文件,使用全路径: ./executable-file PATH是环境变量,如果将当前目录“./”添加到环境变量中,那么也可以不用“./”,直接输入当前 ...

  8. ARP-Address Resolution Protocol-地址解析协议

    主要内容摘自:图解TCP/IP ARP是一种解决地址问题的协议.以目标IP地址为线索,用来定位下一个应该接受数据分包的网络设备的mac地址. 如果目标主机不在同一个链路上时,可以通过ARP查找下一跳路 ...

  9. CI 点击图片刷新验证码

    <img src="<?php echo site_url('home/login/code'); ?>" onclick= this.src="< ...

  10. jquery mobile demo

    <!DOCTYPE html> <html> <head> <title>jQuery Mobile Demo</title> <me ...