【问题标题】:How kubelet sync events to apiserver?kubelet 如何将事件同步到 apiserver?
【发布时间】:2019-06-01 22:26:42
【问题描述】:

最近在研究kubelet如何同步事件到apiserver,但是找不到代码在哪里。

【问题讨论】:

    标签: kubernetes kubelet kubernetes-apiserver


    【解决方案1】:

    kubelet 的源码在here.

    Kubelet 可以通过多种方式获取本地节点所需的 Pod 配置。最重要的方式是Apiserver。 Kubelet 也可以通过指定文件目录或访问指定的 HTTP 端口来获取 Pod 配置。

    在 Kubelet 启动时,会创建一个 PodConfig 对象。 代码kubernetes/blob/master/pkg/kubelet/config/config.go#L58

    type PodConfig struct {
        pods *podStorage
        mux  *config.Mux
    
        // the channel of denormalized changes passed to listeners
        updates chan kubetypes.PodUpdate
        ...
    }
    

    PodConfig 本质上是 Pod 配置的多路复用器。内置mux可以监听各种Pod配置的源(包括apiserver、file、http),并定期同步源的Pod配置状态。

    代码kubernetes/blob/master/pkg/kubelet/types/pod_update.go#L80:

    type PodUpdate struct {
        Pods   []*v1.Pod
        Op     PodOperation
        Source string
    }
    

    Op 定义 Pod 更改类型。例如,这些值可以是 ADDREMOVE 之类的值。最后所有类型的PodUpdate 都会被注入到podConfigupdates。所以只需要监听update频道就可以获取本地节点的Pod配置更新。

    一旦 Kubelet 启动完成,就会执行 syncLoop 函数。 代码kubernetes/blob/master/pkg/kubelet/kubelet.go#L180

    // 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) {
            ...
            for {
                ...
                if !kl.syncLoopIteration(updates, handler, syncTicker.C, housekeepingTicker.C, plegCh) {
                    break
                }
                ...
        }
    

    整个过程在下面的文章中有详细的解释:Understanding the Kubelet Core Execution Frame

    【讨论】:

    • 这就是 kubelet 从 apiserver 获取配置的方式。但是kubelet是如何将OOM之类的消息记录到apiserver的呢?
    • 也许这会帮助你了解沟通是如何运作的,Master-Node communication
    猜你喜欢
    • 2018-12-04
    • 2022-01-25
    • 2020-06-16
    • 2019-10-02
    • 2021-01-07
    • 2015-09-17
    • 2017-04-19
    • 2018-05-24
    • 2020-02-27
    相关资源
    最近更新 更多