1

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.

barath
  • 15
  • 8

2 Answers2

2

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

Vüsal
  • 2,580
  • 1
  • 12
  • 31
  • 1
    if you must use REST APIs you can pass `-v9` to the above command to make it verbose. v9 prints the HTTP requests as proper `curl` requests that you can copy and try. – Sarath Sadasivan Pillai Sep 02 '20 at 10:16
1

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())
    }
}
Rui
  • 447
  • 4
  • 9