kubelet源码-Pod处理流程

本文详细介绍了Kubelet在处理Pod时的流程,包括启动入口和Pod的创建及更新逻辑。在启动时,syncLoop处理pod更新事件,通过HandlePodAdditions和HandlePodUpdates处理Pod的添加和更新。Pod的创建涉及podManager、Admit检查、任务分发、probeManager更新以及RuntimeManager的SyncPod操作,包括创建容器、设置网络和同步状态。Kubelet依赖CRI与container runtime交互,如docker的dockershim,进行容器镜像拉取、容器创建和网络配置。
摘要由CSDN通过智能技术生成

一、启动入口

 

kubelet启动时启动了syncLoop来处理pod的更新事件,podUpdate这个channel会wathch三个来源的pod变化,分别是file(本地静态pod)、apiserver、http。

// Run starts the kubelet reacting to config updates
func (kl *Kubelet) Run(updates <-chan kubetypes.PodUpdate) {
    // 省略 ....
   // Start the pod lifecycle event generator.
   kl.pleg.Start()
   // 启动syncLoop主循环
   kl.syncLoop(updates, kl)
}

 

syncLoop里面有个for循环,每次迭代都会执行syncLoopIteration处理podUpdates、pleg、sync等事件

// syncLoop is the main loop for processing changes. It watches for changes from
// three channels (file, apiserver, and http) and creates a union of them. For
// any new change seen, will run a sync against desired state and running state. If
// no changes are seen to the configuration, will synchronize the last known desired
// state every sync-frequency seconds. Never returns.
func (kl *Kubelet) syncLoop(updates <-chan kubetypes.PodUpdate, handler SyncHandler) {
     // 省略 ...
   duration := base
   for {
      if err := kl.runtimeState.runtimeErrors(); err != nil {
         klog.Infof("skipping pod synchronization - %v", err)
         // exponential backoff
         time.Sleep(duration)
         duration = time.Duration(math.Min(float64(max), factor*float64(duration)))
         continue
      }
      // reset backoff if we have a success
      duration = base

      kl.syncLoopMonitor.Store(kl.clock.Now())
      // 循环调用syncLoopIteration处理各种pod事件
      if !kl.syncLoopIteration(updates, handler, syncTicker.C, housekeepingTicker.C, plegCh) {
         break
      }
      kl.syncLoopMonitor.Store(kl.clock.Now())
   }
}

 

二、Pod处理流程

syncLoopIteration中使用select处理各种channel来的事件,对于podUpdate,会根据update类型分别调用HandlePodAdditions、HandlePodUpdates、HandlePodRemoves、HandlePodReconcile,来自pleg、sync、livenessManager update的更新,会调用HandlePodSyncs

// * configCh: dispatch the pods for the config change to the appropriate
//             handler callback for the event type
// * plegCh: update the runtime cache; sync pod
// * syncCh: sync all pods waiting for sync
// * housekeepingCh: trigger cleanup of pods
// * liveness manager: sync pods that have failed or in which one or more
//                     containers have failed liveness checks
func (kl *Kubelet) syncLoopIteration(configCh <-chan kubetypes.PodUpdate, handler SyncHandler,
   syncCh <-chan time.Time, housekeepingCh <-chan time.Time, plegCh <-chan *pleg.PodLifecycleEvent) bool {
   select {
   case u, open := <-configCh:
      // Update from a config source; dispatch it to the right handler
      // callback.
      if !open {
         klog.Errorf("Update channel is closed. Exiting the sync loop.")
         return false
      }

      switch u.Op {
      case kubetypes.ADD:
         klog.V(2).Infof("SyncLoop (ADD, %q): %q", u.Source, format.Pods(u.Pods))
         // After restarting, kubelet will get all existing pods through
         // ADD as if they are new pods. These pods will then go through the
         // admission process and *may* be rejected. This can be resolved
         // once we have checkpointing.
         handler.HandlePodAdditions(u.Pods)
      case kubetypes.UPDATE:
         klog.V(2).Infof("SyncLoop (UPDATE, %q): %q", u.Source, format.PodsWithDeletionTimestamps(u.Pods))
         handler.HandlePodUpdates(u.Pods)
      case kubetypes.REMOVE:
         klog.V(2).Infof("SyncLoop (REMOVE, %q): %q", u.Source, format.Pods(u.Pods))
         handler.HandlePodRemoves(u.Pods)
      case kubetypes.RECONCILE:
         klog.V(4).Infof("SyncLoop (RECONCILE, %q): %q", u.Source, format.Pods(u.Pods))
         handler.HandlePodReconcile(u.Pods)
      case kubetypes.DELETE:
         klog.V(2).Infof("SyncLoop (DELETE, %q): %q", u.Source, format.Pods(u.Pods))
         // DELETE is treated as a UPDATE because of graceful deletion.
         handler.HandlePodUpdates(u.Pods)
      case kubetypes.RESTORE:
         klog.V(2).Infof("SyncLoop (RESTORE, %q): %q", u.Source, format.Pods(u.Pods))
         // These are pods restored from the checkpoint. Treat them as new
         // pods.
         handler.HandlePodAdditions(u.Pods)
      case kubetypes.SET:
         // TODO: Do we want to support this?
         klog.Errorf("Kubelet does not support snapshot update")
      }

      if u.Op != kubetypes.RESTORE {
         // If the update type is RESTORE, it means that the update is from
         // the pod checkpoints and may be incomplete. Do not mark the
         // source as ready.

         // Mark the source ready after receiving at least one update from the
         // source. Once all the sources are marked ready, various cleanup
         // routines will start reclaiming resources. It is important that this
         // takes place only after kubelet calls the update hand
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值