kubelet启动之启动pod状态管理器(一)

2021-12-14  本文已影响0人  微凉哇

基于kubernetes v1.18.6

概述

pod管理器主要用来将本地pod状态信息同步到apiserverstatusManage并不会主动监控pod的状态,而是提供接口供其他manager进行调用。对于pod状态的变更会推入名为podStatusChannel的通道,statusManage在启动的时候会开辟一个goroutine,用于循环处理podStatusChannelpod状态变更对象。

statusManagepodStatusChannel内对象的处理分为两种方式:

两种方式同步进行

boot-pod-status-manager.png

状态管理器数据结构

type manager struct {
    kubeClient clientset.Interface
    podManager kubepod.Manager
    // Map from pod UID to sync status of the corresponding pod.
    podStatuses      map[types.UID]versionedPodStatus
    podStatusesLock  sync.RWMutex
    podStatusChannel chan podStatusSyncRequest
    // Map from (mirror) pod UID to latest status version successfully sent to the API server.
    // apiStatusVersions must only be accessed from the sync thread.
    apiStatusVersions map[kubetypes.MirrorPodUID]uint64
    podDeletionSafety PodDeletionSafetyProvider
}

状态管理器初始化阶段

在初始化kubelet实例的时候初始化

func NewMainKubelet(kubeCfg *kubeletconfiginternal.KubeletConfiguration,
kubeDeps *Dependencies,
crOptions *config.ContainerRuntimeOptions,
containerRuntime string,
hostnameOverride string,
nodeIP string,
providerID string,
cloudProvider string,
certDirectory string,
rootDirectory string,
registerNode bool,
registerWithTaints []api.Taint,
allowedUnsafeSysctls []string,
experimentalMounterPath string,
experimentalKernelMemcgNotification bool,
experimentalCheckNodeCapabilitiesBeforeMount bool,
experimentalNodeAllocatableIgnoreEvictionThreshold bool,
minimumGCAge metav1.Duration,
maxPerPodContainerCount int32,
maxContainerCount int32,
masterServiceNamespace string,
registerSchedulable bool,
keepTerminatedPodVolumes bool,
nodeLabels map[string]string,
seccompProfileRoot string,
bootstrapCheckpointPath string,
nodeStatusMaxImages int32) (*Kubelet, error) {
...
    klet.statusManager = status.NewManager(klet.kubeClient, klet.podManager, klet)
...
}

sync()处理流程解析

当一个pod状态变更被推入podStatusChannel后,且未到达定时器设置时间(10s间隔),将交由sync()函数处理。

首先我们了解下podStatusChannel存放的对象数据结构:

type podStatusSyncRequest struct {
    podUID types.UID
    status versionedPodStatus
}
type versionedPodStatus struct {
    status v1.PodStatus
    // Monotonically increasing version number (per pod).
    version uint64
    // Pod name & namespace, for sending updates to API server.
    podName      string
    podNamespace string
}

接下来我们来分析下处理流程:

  1. 判断pod是否需要更新。判断方式如下(顺序执行判断逻辑):

PodResourcesAreReclaimed()函数判断以下状态的pod是否已被安全删除(pod可能处于删除中状态,但未删除完毕):

  1. apiserver获取pod实例(入参命名空间、pod名称),若获取不到(可能已被删除),说明不需要同步Pod状态,跳出对当前pod处理流程。
  2. 对比podStatusSyncRequest.podUID与从apiserver查询到的pod uid是否相同,如不相同说明pod可能被删除重建,则不需要同步Pod状态,跳出对当前pod处理流程。
  3. 调用apiserver同步pod最新的status。同步之前比对oldPodStatusnewPodStatus差异,若存在差异调用api-serverpod状态进行更新,并将返回的pod作为newPod,如不存在差异将不会调用api-server进行更新。其中
  1. 调用canBeDeleted()函数(删除pod事件触发的修改pod状态会走该逻辑)。canBeDeleted()函数判断如下:

PodResourcesAreReclaimed()函数判断以下状态的newPod是否已被安全删除(pod可能处于删除中状态,但未删除完毕):

newPod可以被安全删除,调用apiservernewPod执行删除操作,删除成功后将newPodstatusManager.podStatuses(该对象缓存pod状态信息)中删除

sync.png

核心源码

func (m *manager) syncPod(uid types.UID, status versionedPodStatus) {
    if !m.needsUpdate(uid, status) {
        klog.V(1).Infof("Status for pod %q is up-to-date; skipping", uid)
        return
    }

    // TODO: make me easier to express from client code
    pod, err := m.kubeClient.CoreV1().Pods(status.podNamespace).Get(context.TODO(), status.podName, metav1.GetOptions{})
    if errors.IsNotFound(err) {
        klog.V(3).Infof("Pod %q does not exist on the server", format.PodDesc(status.podName, status.podNamespace, uid))
        // If the Pod is deleted the status will be cleared in
        // RemoveOrphanedStatuses, so we just ignore the update here.
        return
    }
    if err != nil {
        klog.Warningf("Failed to get status for pod %q: %v", format.PodDesc(status.podName, status.podNamespace, uid), err)
        return
    }

    // 获取pod真实uid(针对static类型pod的uid需要做转换)
    translatedUID := m.podManager.TranslatePodUID(pod.UID)
    // Type convert original uid just for the purpose of comparison.
    if len(translatedUID) > 0 && translatedUID != kubetypes.ResolvedPodUID(uid) {
        klog.V(2).Infof("Pod %q was deleted and then recreated, skipping status update; old UID %q, new UID %q", format.Pod(pod), uid, translatedUID)
        m.deletePodStatus(uid)
        return
    }

    oldStatus := pod.Status.DeepCopy()
    newPod, patchBytes, unchanged, err := statusutil.PatchPodStatus(m.kubeClient, pod.Namespace, pod.Name, pod.UID, *oldStatus, mergePodStatus(*oldStatus, status.status))
    klog.V(3).Infof("Patch status for pod %q with %q", format.Pod(pod), patchBytes)
    if err != nil {
        klog.Warningf("Failed to update status for pod %q: %v", format.Pod(pod), err)
        return
    }
    if unchanged {
        klog.V(3).Infof("Status for pod %q is up-to-date: (%d)", format.Pod(pod), status.version)
    } else {
        klog.V(3).Infof("Status for pod %q updated successfully: (%d, %+v)", format.Pod(pod), status.version, status.status)
        pod = newPod
    }

    m.apiStatusVersions[kubetypes.MirrorPodUID(pod.UID)] = status.version

    // We don't handle graceful deletion of mirror pods.
    if m.canBeDeleted(pod, status.status) {
        deleteOptions := metav1.DeleteOptions{
            GracePeriodSeconds: new(int64),
            // Use the pod UID as the precondition for deletion to prevent deleting a
            // newly created pod with the same name and namespace.
            Preconditions: metav1.NewUIDPreconditions(string(pod.UID)),
        }
        err = m.kubeClient.CoreV1().Pods(pod.Namespace).Delete(context.TODO(), pod.Name, deleteOptions)
        if err != nil {
            klog.Warningf("Failed to delete status for pod %q: %v", format.Pod(pod), err)
            return
        }
        klog.V(3).Infof("Pod %q fully terminated and removed from etcd", format.Pod(pod))
        m.deletePodStatus(uid)
    }
}
上一篇 下一篇

猜你喜欢

热点阅读