博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Kubernetes网络框架
阅读量:4494 次
发布时间:2019-06-08

本文共 5993 字,大约阅读时间需要 19 分钟。

// 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

 

转载于:https://www.cnblogs.com/YaoDD/p/6549198.html

你可能感兴趣的文章
9.2NOIP模拟题
查看>>
整合SpringDataJpa
查看>>
vue过渡
查看>>
tcpreplay 博客目录
查看>>
oracle11g忘记sys密码
查看>>
文件各种上传,离不开的表单
查看>>
mysql查询插入优化
查看>>
hadoop备战:yarn框架的搭建(mapreduce2)
查看>>
微信公众号开发模式开启总结
查看>>
pygame-KidsCanCode系列jumpy-part2-加速度与摩擦力
查看>>
[elk]logstash的grok匹配逻辑grok+date+mutate
查看>>
准备Android面试
查看>>
界面与后台逻辑完全分离,单例模式,接口
查看>>
redis安装及测试
查看>>
38-系统标准模块与第三方模块(1)
查看>>
[转]Android U 盘功能实现和分析
查看>>
dedecms织梦副栏目名称和链接调用
查看>>
iOS 离屏渲染学习笔记
查看>>
iOS Xib布局某些控件显示或隐藏<约束的修改>
查看>>
软件工程第一次作业
查看>>