【原创】k8s源码分析-----kubelet(7)containerRuntime

32 篇文章 3 订阅
32 篇文章 0 订阅

本文QQ空间链接:http://user.qzone.qq.com/29185807/blog/1460533683

本文csdn博客链接:http://blog.csdn.net/screscent/article/details/51144087

源码为k8s v1.1.1稳定版本
 

2.6、containerRuntime

1、参数

代码在k8s.io\kubernetes\cmd\kubelet\app中

 

结构体变量

type KubeletServer struct {

...

PodInfraContainerImage         string

RegistryPullQPS                float64

RegistryBurst                  int

    NetworkPluginDir               string

    NetworkPluginName              string

    DockerExecHandlerName          string

CPUCFSQuota                    bool

    // Pull images one at a time.

    SerializeImagePulls bool

...

}

 

默认参数

func NewKubeletServer() *KubeletServer {

    return &KubeletServer{

...

PodInfraContainerImage:      dockertools.PodInfraContainerImage,

        NetworkPluginDir:            "/usr/libexec/kubernetes/kubelet-plugins/net/exec/",

        NetworkPluginName:           "",

DockerExecHandlerName:       "native",

CPUCFSQuota:                 false,

SerializeImagePulls: true,

...

}

}

flag参数

func (s *KubeletServer) AddFlags(fs *pflag.FlagSet) {

...

    fs.StringVar(&s.PodInfraContainerImage, "pod-infra-container-image", s.PodInfraContainerImage, "The image whose network/ipc namespaces containers in each pod will use.")

        fs.Float64Var(&s.RegistryPullQPS, "registry-qps", s.RegistryPullQPS, "If > 0, limit registry pull QPS to this value.  If 0, unlimited. [default=0.0]")        fs.IntVar(&s.RegistryBurst, "registry-burst", s.RegistryBurst, "Maximum size of a bursty pulls, temporarily allows pulls to burst to this number, while still not exceeding registry-qps.  Only used if --registry-qps > 0")

    

 

    fs.StringVar(&s.NetworkPluginName, "network-plugin", s.NetworkPluginName, "<Warning: Alpha feature> The name of the network plugin to be invoked for various events in kubelet/pod lifecycle")

    fs.StringVar(&s.NetworkPluginDir, "network-plugin-dir", s.NetworkPluginDir, "<Warning: Alpha feature> The full path of the directory in which to search for network plugins")

    

 

    fs.StringVar(&s.DockerExecHandlerName, "docker-exec-handler", s.DockerExecHandlerName, "Handler to use when executing a command in a container. Valid values are 'native' and 'nsenter'. Defaults to 'native'.")

    

    fs.BoolVar(&s.CPUCFSQuota, "cpu-cfs-quota", s.CPUCFSQuota, "Enable CPU CFS quota enforcement for containers that specify CPU limits")

    fs.BoolVar(&s.SerializeImagePulls, "serialize-image-pulls", s.SerializeImagePulls, "Pull images one at a time. We recommend *not* changing the default value on nodes that run docker daemon with version < 1.9 or an Aufs storage backend. Issue #10959 has more details. [default=true]")

 

...

}

 

参数比较多。挑比较主要的说

PodInfraContainerImage:设置image镜像的下载地址,主要是防止被墙,设置私有的下载地址

RegistryPullQPS:限制拉取镜像个数,每秒最大的拉取个数

RegistryBurst:限制并发拉取的数量

NetworkPluginName:网络插件的名字

NetworkPluginDir:网络插件目录

DockerExecHandlerName:docker执行命令的方式,一般都是native

2、传递参数

老方式传递参数

 

代码依旧在k8s.io\kubernetes\cmd\kubelet\app 中

在func (s *KubeletServer) KubeletConfig() (*KubeletConfig, error) {

...

return &KubeletConfig{

...

参数较多,此处省略,可以直接去看源码

...

}

}

3、构建

我们直接进入

代码在k8s.io\kubernetes\pkg\kubelet\kubelet.go中

func NewMainKubelet(

runtime支持两种格式,docker和rkt。我们这里只分析docker

 

我们看看构建containerRuntime的主要参数

3.1、dockerClient,

这个之前文章中已经分析过的了

3.2、readinessManager、containerRefManager

1、readinessManager其实就是一个map

代码在k8s.io\kubernetes\pkg\kubelet\container\ readiness_manager.go

2、containerRefManager,其实也是一个map

k8s.io\kubernetes\pkg\kubelet\container\container_reference_manager.go

3.3、machineInfo

获取MachineInfo,主要是从cadvisor获取

3.4 、dockerExecHandler

代码在k8s.io\kubernetes\cmd\kubelet\app中

func (s *KubeletServer) KubeletConfig()

我们这里就看native

代码在k8s.io\kubernetes\pkg\kubelet\dockertools\exec.go

从以上代码来看,其实就是对dockerclient进行了一个exec的封装

 

4、功能

 

我们看下DockerManager结构体

下面是对外暴露的接口

从接口来看基本上涵盖了所有的pod的管理操作

 

挑几个函数看看

ExecInContainer

从下图,我们就可以看到了,最后调用的是execHandler,也就是我们上面3.4讲解的dockerExecHandler

PullImage和IsImagePresent

dockerpuller是在构建的时候初始化的

 

下面是初始化地方

dockerpuller结构体,其中最主要的参数就是dockerclient

除了检测限速,另外最终的还是调用了dockerclient的PullImage的接口

 

 

 

5、小结

DockerManager作为kuberlet的containerRuntime,承担了很多主要工作的封装,所以代码量非常大,在此本文只是做了一个简单的分析,并没有全部分析。

 

 

龚浩华

qq 月牙寂 道长 29185807

2016年4月13日

(版权声明:本文为作者原创,如需转载请通知本人,并标明出处和作者。擅自转载的,保留追究其侵权的权利。)

 

如果你觉得本文对你有帮助,可以转到你的朋友圈,让更多人一起学习。

第一时间获取文章,可以关注本人公众号:月牙寂道长,也可以扫码关注

 

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值