【问题标题】:Watching Kubernetes Pod, Namespace and Node status via Informer interface通过 Informer 界面查看 Kubernetes Pod、命名空间和节点状态
【发布时间】:2019-12-17 13:59:25
【问题描述】:

有一种方法可以在 K8s pod 状态上设置监视,我可以测试该功能。

podWatchController 然后接收事件并调用相应的处理程序。但是,此机制不适用于命名空间和节点,即 NewListWatchFromClient 构造函数中不存在选项。

请建议如何使用此模式查看节点和命名空间状态(添加、删除、更新)。

    podWatchlist := cache.NewListWatchFromClient(
       k8s.kubeClient.K8sClient.CoreV1().RESTClient(),
       string(v1.ResourcePods),
       v1.NamespaceAll,
       fields.Everything(),
    )

    // K8s Pod watcher controller
    _, podWatchController := cache.NewInformer( // also take a look at NewSharedIndexInformer
       podWatchlist,
       &v1.Pod{},
       0, //Duration is int64
       cache.ResourceEventHandlerFuncs{
           AddFunc: func(obj interface{}) {
               k8s.handleAddPod(obj)
           },
           DeleteFunc: func(obj interface{}) {
               k8s.handleDeletePod(obj)
           },
           UpdateFunc: func(oldObj, newObj interface{}) {
               k8s.handleUpdatePod(oldObj, newObj)
           },
       },
    )

    podStopChan := make(chan struct{})
    go podWatchController.Run(podStopChan)

我找到了另一种基于NewSharedInformerFactory的方法,它为pods、nodes和namespaces提供了watcher;但是,我没有看到处理程序收到任何通知。这种方法可能缺少什么?

对于 Pod:

    // Add watcher for the Pod.
    factory := informers.NewSharedInformerFactory(k8s.kubeClient.K8sClient, 0)
    podInformer := factory.Core().V1().Pods().Informer()
    podInformerChan := make(chan struct{})
    defer close(podInformerChan)

    // Pod informer state change handler
    podInformer.AddEventHandler(cache.ResourceEventHandlerFuncs{
        // When a new pod gets created
        AddFunc: func(obj interface{}) {
            k8s.handleAddPod(obj)
        },
        // When a pod gets updated
        UpdateFunc: func(oldObj interface{}, newObj interface{}) {
            k8s.handleUpdatePod(oldObj, newObj)
        },
        // When a pod gets deleted
        DeleteFunc: func(obj interface{}) {
            k8s.handleDeletePod(obj)
        },
    })

    go podInformer.GetController().Run(podInformerChan)

对于命名空间:

    // Add watcher for the Namespace.
    factory := informers.NewSharedInformerFactory(k8s.kubeClient.K8sClient, 0)
    nsInformer := factory.Core().V1().Namespaces().Informer()
    nsInformerChan := make(chan struct{})
    defer close(nsInformerChan)

    // Namespace informer state change handler
    nsInformer.AddEventHandler(cache.ResourceEventHandlerFuncs {
       // When a new namespace gets created
       AddFunc:    func(obj interface{}) {
           k8s.handleAddNamespace(obj)
       },
       // When a namespace gets updated
       UpdateFunc: func(oldObj interface{}, newObj interface{}) {
           k8s.handleUpdateNamespace(obj)
       },
       // When a namespace gets deleted
       DeleteFunc: func(obj interface{}) {
           k8s.handleDeleteNamespace(obj)
       },
    })
    go nsInformer.GetController().Run(nsInformerChan)

【问题讨论】:

    标签: kubernetes google-kubernetes-engine kubernetes-pod


    【解决方案1】:

    我可以通过以下更改使其工作,即我们必须调用工厂、informer 和控制器的 Run() 方法。

    sharedInformer.Start(podInformerChan)
    
    podInformer.Run(podInformerChan)
    podInformer.GetController().Run(podInformerChan)
    nsInformer.Run(nsInformerChan)
    nsInformer.GetController().Run(nsInformerChan)
    

    但仍有一些错误显示并试图理解它们是什么。目前,他们正指向这条线。

    https://github.com/kubernetes/client-go/blob/master/tools/cache/shared_informer.go#L612

    E0809 15:33:39.187411   79537 shared_informer.go:611] unrecognized notification: <nil>
    E0809 15:33:40.191304   79537 shared_informer.go:611] unrecognized notification: <nil>
    E0809 15:33:48.227933   79537 shared_informer.go:611] unrecognized notification: <nil>
    E0809 15:33:54.231458   79537 shared_informer.go:611] unrecognized notification: <nil>
    

    【讨论】:

    • 除非你搞乱内部,否则错误可能是底层库中的错误?也许在github上检查
    • 我最初看到了大量的通知(也许这是正在构建通知者缓存的时间)。如何停止接收这些初始通知并仅在实际事件之后接收实际状态更改,例如删除 pod。
    猜你喜欢
    • 1970-01-01
    • 2021-11-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-03-27
    • 1970-01-01
    • 2021-12-06
    相关资源
    最近更新 更多