Exposing Kubernetes Metric Server API to Curl from Inside the Pod: A Guide

Exposing Kubernetes Metric Server API to Curl from Inside the Pod: A Guide
In the world of data science, Kubernetes has become a go-to platform for managing containerized applications at scale. One of its many features is the Metric Server, a scalable, efficient source of container resource metrics. However, accessing these metrics from inside a pod can be a challenge. In this blog post, we’ll guide you through the process of exposing the Kubernetes Metric Server API to curl from inside the pod.
Prerequisites
Before we start, ensure you have the following:
- A Kubernetes cluster up and running
- kubectl installed and configured to interact with your cluster
- curl installed in your pod
Step 1: Understanding the Kubernetes Metric Server
The Kubernetes Metric Server collects resource metrics from Kubelets and exposes them via the Metrics API. These metrics can be used by Kubernetes components like the Horizontal Pod Autoscaler or the Kubernetes scheduler.
However, by default, the Metric Server is not accessible from inside a pod. This is because it uses a self-signed certificate and doesn’t trust the Kubernetes API server’s CA certificate.
Step 2: Modifying the Metric Server Deployment
To expose the Metric Server to curl from inside a pod, we need to modify the Metric Server deployment. This involves adding the --kubelet-insecure-tls
and --kubelet-preferred-address-types=InternalIP
flags to the Metric Server’s command.
Here’s how you can do it:
kubectl -n kube-system edit deployment metrics-server
In the spec.template.spec.containers.args
section, add the following:
- --kubelet-insecure-tls
- --kubelet-preferred-address-types=InternalIP
Save and exit the editor. This will trigger a new deployment of the Metric Server with the updated flags.
Step 3: Accessing the Metric Server from Inside a Pod
Now that the Metric Server is configured to accept insecure connections, we can access it from inside a pod.
First, find the Metric Server’s service IP:
kubectl -n kube-system get service metrics-server -o jsonpath='{.spec.clusterIP}'
Next, from inside your pod, use curl to access the Metric Server:
curl https://<metrics-server-service-ip>/apis/metrics.k8s.io/v1beta1/nodes
Replace <metrics-server-service-ip>
with the IP you got from the previous command. You should now see a JSON response with the node metrics.
Conclusion
Exposing the Kubernetes Metric Server API to curl from inside a pod is a straightforward process once you understand the steps involved. It involves modifying the Metric Server deployment to accept insecure connections and then using curl to access the Metric Server from inside a pod.
Remember, this setup is not recommended for production environments due to the insecure connection. For production environments, consider setting up proper certificate management.
We hope this guide has been helpful. Stay tuned for more Kubernetes tips and tricks!
Keywords
- Kubernetes
- Metric Server
- API
- curl
- pod
- data science
- Kubernetes cluster
- kubectl
- Metrics API
- deployment
- service IP
- JSON
- node metrics
- certificate management
Meta Description
Learn how to expose the Kubernetes Metric Server API to curl from inside a pod. This guide provides a step-by-step process for data scientists working with Kubernetes.
About Saturn Cloud
Saturn Cloud is your all-in-one solution for data science & ML development, deployment, and data pipelines in the cloud. Spin up a notebook with 4TB of RAM, add a GPU, connect to a distributed cluster of workers, and more. Join today and get 150 hours of free compute per month.