简体   繁体   English

使用 k8s rest api 从特定节点获取 Pod

[英]Get Pods from a Particular node using k8s rest api

is there any k8s rest api endpoint to fetch all the pods and its details of a paricular node.是否有任何 k8s rest api 端点来获取特定节点的所有 pod 及其详细信息。 I use minikube and started a proxy using kubectl proxy --port=7070 & .我使用 minikube 并使用kubectl proxy --port=7070 &启动了一个代理。 We have endpoints like GET /api/v1/namespaces/{namespace-name}/pods .我们有像GET /api/v1/namespaces/{namespace-name}/pods这样的端点。 Do we have any similar endpoint to access pods belonging to a specific node?我们是否有任何类似的端点来访问属于特定节点的 pod? I dont want to use kubectl commands.我不想使用 kubectl 命令。

I dont want to use kubectl commands.我不想使用 kubectl 命令。

kubectl is a REST client for k8s API server . kubectl是 k8s API serverREST客户端。 If you don't want to use it - you will need to perform the same requests manually.如果您不想使用它 - 您将需要手动执行相同的请求。

There is no "legal" way to get information from k8s cluster without talking to API Server .在不与API Server交谈的情况下,没有从 k8s 集群获取信息的“合法”方式。 The API Server is the only source of truth and all controllers are using the API Server to perform required changes. API Server是唯一的真实来源,所有控制器都使用API Server来执行所需的更改。 So, probably, you should rely on the information provided by the API Server .因此,您可能应该依赖API Server提供的信息。

You can use kubectl to get pods for specific node by:您可以使用kubectl通过以下方式获取特定节点的 pod:

kubectl get pods --all-namespaces -o wide --field-selector spec.nodeName=<node>

check this out: Kubernetes API - gets Pods on specific nodes看看这个: Kubernetes API - 在特定节点上获取 Pod

You can also do that without using kubectl - read this: The Kubernetes API你也可以在不使用kubectl情况下做到这kubectl - 阅读: The Kubernetes API

You can use client-go library to talk to the API server .您可以使用client-go库与API server Here is an example.这是一个例子。

import (
     "github.com/golang/glog"

     "k8s.io/api/core/v1"
     "k8s.io/apimachinery/pkg/fields"
     "k8s.io/client-go/kubernetes"
     "k8s.io/client-go/tools/cache"
     "k8s.io/client-go/tools/clientcmd"
)
func main() {
    config, err := clientcmd.BuildConfigFromFlags("", "")
    if err != nil {
        glog.Errorln(err)
    }
    clientset, err := kubernetes.NewForConfig(config)
    if err != nil {
        glog.Errorln(err)
    }
    pods, err := clientset.CoreV1().Pods("").List(context.TODO(), metav1.ListOptions{})
    if err != nil {
        panic(err.Error())
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM