如何在Golang中使用Kubernetes API获取区域(zone)和Pod信息?

huangapple go评论136阅读模式
英文:

How to get zone and pod information using kubernetes api in golang

问题

我是新手,想要使用 Kubernetes 的 API 进行一些自定义操作。我的目标是获取集群中所有区域(zones)的 Pod,或者特定区域的 Pod。使用 Kubernetes API 和 Golang,我已经能够获取 Pod 列表和节点列表,但是我找不到可以获取区域列表和特定区域内 Pod 列表的接口。

请指导我如何实现这个目标。

英文:

I am new to Kubernetes and I need to do some customizations using API's. My aim is to get pods from all zones of cluster or specific zones. Using Kubernetes API and Golang I am able to get list of Pods and list of Nodes but i am unable to find any interface which will give me list of Zones and interface which will give me list of Pods within zone.

Need guidance on how I can achieve this.

答案1

得分: 1

你可以通过读取节点的labels来获取有关节点区域和区域的信息。你可以在这里查看常用的标签:https://kubernetes.io/docs/reference/labels-annotations-taints/#topologykubernetesiozone。

你可以根据这些信息构建关于节点在区域中的放置的信息。然后,你可以使用字段选择器get pods并过滤输出。

在GitHub上找到了一个示例:https://github.com/kubernetes/client-go/issues/410

nodeName := "my-node"
pods, err := clientset.CoreV1().Pods("").List(metav1.ListOptions{
    FieldSelector: "spec.nodeName=" + nodeName,
})
英文:

You can get info about node Zone and Region by reading its labels. You can check out well-known labels here: https://kubernetes.io/docs/reference/labels-annotations-taints/#topologykubernetesiozone.

You can build from this information about placement nodes in zones. Next you can get pods and filter the output by field selector.
Found example on GH: https://github.com/kubernetes/client-go/issues/410

nodeName := "my-node"
pods, err := clientset.CoreV1().Pods("").List(metav1.ListOptions{
    FieldSelector: "spec.nodeName=" + nodeName,
})

huangapple
  • 本文由 发表于 2021年11月12日 18:32:12
  • 转载请务必保留本文链接:https://go.coder-hub.com/69941455.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定