设为首页 收藏本站
查看: 1037|回复: 0

[经验分享] Kubernetes网络框架

[复制链接]

尚未签到

发表于 2018-1-4 16:31:03 | 显示全部楼层 |阅读模式
  // 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,>


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

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-431570-1-1.html 上篇帖子: 【转】Kubernetes初探 下篇帖子: Centos7搭建kubernetes搭建
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表