简体   繁体   中英

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. I use minikube and started a proxy using kubectl proxy --port=7070 & . We have endpoints like GET /api/v1/namespaces/{namespace-name}/pods . Do we have any similar endpoint to access pods belonging to a specific node? I dont want to use kubectl commands.

I dont want to use kubectl commands.

kubectl is a REST client for k8s API server . 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 . The API Server is the only source of truth and all controllers are using the API Server to perform required changes. So, probably, you should rely on the information provided by the API Server .

You can use kubectl to get pods for specific node by:

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

check this out: Kubernetes API - gets Pods on specific nodes

You can also do that without using kubectl - read this: The Kubernetes API

You can use client-go library to talk to the 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())
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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