August 01, 2020 (updated at: October 03, 2022)
Receive data from Kubernetes API using curl
Start by running kubectl proxy. This lets us avoid dealing with authentication headers.
kubectl proxyTest the connection with a basic call.
curl http://localhost:8001/api/To check the API groups available, access the root.
curl http://localhost:8001/apis/Based on the group listing, you can infer URLs for resources.
For cluster-scoped resources, use:
/apis/<GROUP>/<VERSION>/<RESOURCETYPE>
/apis/<GROUP>/<VERSION>/<RESOURCETYPE>/<NAME>And, for namespace-scoped resources:
/apis/<GROUP>/<VERSION>/<RESOURCETYPE>
/apis/<GROUP>/<VERSION>/namespaces/<NAMESPACE>/<RESOURCETYPE>
/apis/<GROUP>/<VERSION>/namespaces/<NAMESPACE>/<RESOURCETYPE>/<NAME>To get deployment data, for example, the API Group is “apps”, Version is “v1”, and the resource type is “deployments”. The final request looks like this:
curl http://localhost:8001/apis/apps/v1/deploymentsIf your Kubernetes cluster has a metrics-server, the following request will get node resource usage data:
curl http://localhost:8001/apis/metrics.k8s.io/v1beta1/nodesRancher users
However, if your cluster was created using Rancher, you’ll need “Authorized Cluster Endpoint” enabled, so your requests can reach the API Server directly. Your URL paths will differ slightly from the examples above.
Using your command line, you’ll also need to select the correct context for kubectl before activating the proxy.
# List available contexts
kubectl config get-contexts
# Select the direct API context
# Its name has the format <cluster_name>-<pilot_name>
kubectl config use-context <direct_context>Further reading
Overview of the Kubernetes API