如何使用 client-go API 从 Kubernetes 的 pod 获取事件消息?
时间:2024-04-07
浏览:318
作者:佚名
小伙伴们对Golang编程感兴趣吗?是否正在学习相关知识点?如果是,那么本文《如何使用 client-go API 从 Kubernetes 的 pod 获取事件消息?》,就很适合你,本篇文章讲解的知识点主要包括。在之后的文章中也会多多分享相关知识点,希望对大家的知识积累有所帮助!
问题内容
如何从 pod 获取事件消息,例如使用 client-go kubernetes api 的此命令:
kubectl describe pod spark-t2f59 -n spark Events: Type Reason Age From Message ---- ------ ---- ---- ------- Warning FailedScheduling 104s default-scheduler 0/19 nodes are available: 15 Insufficient cpu, 19 Insufficient memory. Warning FailedScheduling 104s default-scheduler 0/19 nodes are available: 15 Insufficient cpu, 19 Insufficient memory. Warning FailedScheduling 45s default-scheduler 0/20 nodes are available: 16 Insufficient cpu, 20 Insufficient memory. Warning FailedScheduling 34s default-scheduler 0/20 nodes are available: 16 Insufficient cpu, 20 Insufficient memory. Normal NotTriggerScaleUp 97s cluster-autoscaler pod didn't trigger scale-up (it wouldn't fit if a new node is added): 1 Insufficient memory, 1 max node group size reached
有没有办法获得相同的事件输出,但使用client-go而不是kubectl??
正确答案
既然您知道命名空间和 pod 名称,您就可以执行以下操作:
package main
import (
"context"
"fmt"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/rest"
)
func main() {
config, _ := rest.InClusterConfig()
clientset, _ := kubernetes.NewForConfig(config)
events, _ := clientset.CoreV1().Events("spark").List(context.TODO(),metav1.ListOptions{FieldSelector: "involvedObject.name=spark-t2f59", TypeMeta: metav1.TypeMeta{Kind: "Pod"}})
for _, item := range events.Items {
fmt.Println(item)
}
}理论要掌握,实操不能落!以上关于《如何使用 client-go API 从 Kubernetes 的 pod 获取事件消息?》的详细介绍,大家都掌握了吧!如果想要继续提升自己的能力,那么就来关注golang学习网公众号吧!
本文内容来源于互联网,如有侵权请联系删除。
近期热门
11.18
11.15
站内热门
最新文章
11.20
11.20
11.20
11.20
热门专题
