api 的 Kubernetes 监视 Pod 事件
Kubernetes Watch Pod Events with api
我们对 运行 将某些命令作为 pods 和服务感兴趣,因为它们会启动或停止。在 yml 文件中使用生命周期挂钩对我们不起作用,因为这些命令不是可选的。我们已经考虑 运行 设置一个使用 watch api 的 watcher pod 来 运行 这些命令。但是我们无法弄清楚如何使用 watch api 以便它不会一次又一次地发送相同的事件。有没有办法告诉手表 api 自连接打开后只发送新事件?如果期望有状态监视 api 是不合理的,是否可以向它传递时间戳或单调递增的 id 以避免获取已经看到的事件?
基本上我们现在正在做的事情是 运行使用守护进程与 api 通信的 pod。我们可以找到事件作为流。但是我们对 运行 创建或删除 pod 时的一些任务感兴趣。
我建议使用 kube 存储库中的 client。
为什么 lifecycle hooks 不适用于您的用例?
我找到了答案。以防还有人在看。
有一个更好的系统可以使用 pkg/controller/framework
package
来监视资源和处理带有自定义任务的事件
我找到了这样的步骤,
1. initiate a framework.NewInFormer
2. Run the controller
3. the NewInFormer loads with your custom event handlers that will call when the events occured.
如果它是集群内的,你可以在 golang 中这样做:
package main
import (
"fmt"
"time"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/pkg/api/v1"
"k8s.io/client-go/tools/cache"
"k8s.io/client-go/pkg/fields"
"k8s.io/client-go/rest"
)
func main() {
config, err := rest.InClusterConfig()
if err != nil {
panic(err.Error())
}
clientset, err := kubernetes.NewForConfig(config)
if err != nil {
panic(err.Error())
}
watchlist := cache.NewListWatchFromClient(clientset.Core().RESTClient(), "pods", v1.NamespaceDefault,
fields.Everything())
_, controller := cache.NewInformer(
watchlist,
&v1.Pod{},
time.Second * 0,
cache.ResourceEventHandlerFuncs{
AddFunc: func(obj interface{}) {
fmt.Printf("add: %s \n", obj)
},
DeleteFunc: func(obj interface{}) {
fmt.Printf("delete: %s \n", obj)
},
UpdateFunc:func(oldObj, newObj interface{}) {
fmt.Printf("old: %s, new: %s \n", oldObj, newObj)
},
},
)
stop := make(chan struct{})
go controller.Run(stop)
}
你说生命周期钩子命令不是可选的,但它们确实是可选的。
Hook handler implementations
Containers can access a hook by implementing and registering a handler for that hook. There are two types of hook handlers that can be implemented for Containers:
Exec - Executes a specific command, such as pre-stop.sh, inside the cgroups and namespaces of the Container. Resources consumed by the command are counted against the Container.
HTTP - Executes an HTTP request against a specific endpoint on the Container.
从这里开始:https://kubernetes.io/docs/concepts/containers/container-lifecycle-hooks/
运行 kube 代理无需身份验证即可使用 curl
kubectl proxy
用手表列出所有事件;
curl -s 127.0.0.1:8001/api/v1/watch/events
运行 curl 以观察事件并使用 jq 过滤它以用于 pod 启动和停止。
curl -s 127.0.0.1:8001/api/v1/watch/events | jq --raw-output \ 'if .object.reason == "Started" then . elif .object.reason == "Killing" then . else empty end | [.object.firstTimestamp, .object.reason, .object.metadata.namespace, .object.metadata.name] | @csv'
我们对 运行 将某些命令作为 pods 和服务感兴趣,因为它们会启动或停止。在 yml 文件中使用生命周期挂钩对我们不起作用,因为这些命令不是可选的。我们已经考虑 运行 设置一个使用 watch api 的 watcher pod 来 运行 这些命令。但是我们无法弄清楚如何使用 watch api 以便它不会一次又一次地发送相同的事件。有没有办法告诉手表 api 自连接打开后只发送新事件?如果期望有状态监视 api 是不合理的,是否可以向它传递时间戳或单调递增的 id 以避免获取已经看到的事件?
基本上我们现在正在做的事情是 运行使用守护进程与 api 通信的 pod。我们可以找到事件作为流。但是我们对 运行 创建或删除 pod 时的一些任务感兴趣。
我建议使用 kube 存储库中的 client。 为什么 lifecycle hooks 不适用于您的用例?
我找到了答案。以防还有人在看。
有一个更好的系统可以使用 pkg/controller/framework
package
我找到了这样的步骤,
1. initiate a framework.NewInFormer
2. Run the controller
3. the NewInFormer loads with your custom event handlers that will call when the events occured.
如果它是集群内的,你可以在 golang 中这样做:
package main
import (
"fmt"
"time"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/pkg/api/v1"
"k8s.io/client-go/tools/cache"
"k8s.io/client-go/pkg/fields"
"k8s.io/client-go/rest"
)
func main() {
config, err := rest.InClusterConfig()
if err != nil {
panic(err.Error())
}
clientset, err := kubernetes.NewForConfig(config)
if err != nil {
panic(err.Error())
}
watchlist := cache.NewListWatchFromClient(clientset.Core().RESTClient(), "pods", v1.NamespaceDefault,
fields.Everything())
_, controller := cache.NewInformer(
watchlist,
&v1.Pod{},
time.Second * 0,
cache.ResourceEventHandlerFuncs{
AddFunc: func(obj interface{}) {
fmt.Printf("add: %s \n", obj)
},
DeleteFunc: func(obj interface{}) {
fmt.Printf("delete: %s \n", obj)
},
UpdateFunc:func(oldObj, newObj interface{}) {
fmt.Printf("old: %s, new: %s \n", oldObj, newObj)
},
},
)
stop := make(chan struct{})
go controller.Run(stop)
}
你说生命周期钩子命令不是可选的,但它们确实是可选的。
Hook handler implementations
Containers can access a hook by implementing and registering a handler for that hook. There are two types of hook handlers that can be implemented for Containers:
Exec - Executes a specific command, such as pre-stop.sh, inside the cgroups and namespaces of the Container. Resources consumed by the command are counted against the Container.
HTTP - Executes an HTTP request against a specific endpoint on the Container.
从这里开始:https://kubernetes.io/docs/concepts/containers/container-lifecycle-hooks/
运行 kube 代理无需身份验证即可使用 curl
kubectl proxy
用手表列出所有事件;
curl -s 127.0.0.1:8001/api/v1/watch/events
运行 curl 以观察事件并使用 jq 过滤它以用于 pod 启动和停止。
curl -s 127.0.0.1:8001/api/v1/watch/events | jq --raw-output \ 'if .object.reason == "Started" then . elif .object.reason == "Killing" then . else empty end | [.object.firstTimestamp, .object.reason, .object.metadata.namespace, .object.metadata.name] | @csv'