
Learn how to set up port forwarding in OpenLens to access Prometheus running inside a Kubernetes cluster. OpenLens is a popular open-source Kubernetes IDE that allows you to manage and troubleshoot Kubernetes clusters with a sleek and user-friendly interface.
Prometheus is an open-source monitoring and alerting toolkit designed for reliability and scalability, widely used for collecting and querying metrics from various services in Kubernetes clusters.
In some scenarios, you may need to access Prometheus from OpenLens (or any Kubernetes tool) for monitoring purposes.
However, since Prometheus may not be exposed publicly, you’ll need to use port forwarding to connect OpenLens (or kubectl) to Prometheus running inside your Kubernetes cluster.
In this guide, we will walk you through the process of port forwarding Prometheus within a Kubernetes cluster and accessing it from OpenLens.
In the case of Prometheus, port forwarding lets you access the Prometheus web interface locally, even though it’s running inside the Kubernetes cluster.
Prerequisites
Before you begin, make sure you have the following:
- Kubernetes cluster running (locally, on a cloud provider, or on any other environment).
- kubectl installed and configured to access your Kubernetes cluster.
- OpenLens installed and connected to your Kubernetes cluster.
- Prometheus deployed within your Kubernetes cluster. If Prometheus is not installed, you can follow this guide to install Prometheus using Helm.
Step 1: Identify Prometheus Pod in Your Kubernetes Cluster
First, you need to identify the Prometheus pod running in your Kubernetes cluster. You can use kubectl or OpenLens to do this.
Using kubectl
If you’re using kubectl, run the following command to list all the pods in the monitoring namespace (or the namespace where Prometheus is installed):
kubectl get pods -n monitoring
Look for the Prometheus pod, which will likely be named something like prometheus-k8s-xxxxxx.
Using OpenLens
- Open OpenLens and navigate to your Kubernetes cluster.
- Go to the Pods section in the monitoring namespace.
- Find the Prometheus pod.
Take note of the Pod name for the next step.
Step 2: Set Up Port Forwarding Using kubectl
Now that you’ve identified the Prometheus pod, the next step is to forward a port from your local machine to the Prometheus pod in the Kubernetes cluster.
Using kubectl
Run the following command to forward port 9090 (the default Prometheus web interface port) from the Prometheus pod to your local machine:
kubectl port-forward -n monitoring pod/prometheus-k8s-xxxxxx 9090:9090
In this command:
- Replace
prometheus-k8s-xxxxxxwith the name of your Prometheus pod. 9090:9090forwards the local port9090to the remote port9090where Prometheus is running.
You should see output similar to this, indicating that the port is being forwarded:
Forwarding from 127.0.0.1:9090 -> 9090
You can now access Prometheus on your local machine by navigating to http://localhost:9090 in your browser.
Using OpenLens (Optional for Visual Representation)
In OpenLens, there’s an option to manage port forwarding directly. This is useful if you prefer a GUI for managing your Kubernetes resources.
- In OpenLens, navigate to your Kubernetes Cluster.
- Go to Pods under the monitoring namespace.
- Right-click the Prometheus pod and choose Port Forwarding.
- Set the local port to
9090and the remote port to9090. - Click Start Port Forwarding.
Step 3: Access Prometheus in Your Browser
Once the port forwarding is set up, you can access Prometheus locally by visiting:
http://localhost:9090
This will bring up the Prometheus web interface, where you can start querying metrics, viewing the status of the system, or configuring alerts.
Step 4: Monitoring and Troubleshooting
Now that you have access to Prometheus via the port forwarding, you can perform several tasks:
- Query Metrics: Use the Prometheus query language (PromQL) to query metrics from various services within your Kubernetes cluster.
- Create Alerts: Set up alerting rules in Prometheus to notify you when certain thresholds are met.
- Troubleshoot: Use the web interface to troubleshoot issues by inspecting metrics related to application performance, Kubernetes components, and more.
Step 5: Stopping Port Forwarding
When you’re done using Prometheus, you can stop the port forwarding process. If you used kubectl, simply press Ctrl+C to stop the port forwarding session.
If you’re using OpenLens, navigate to the Port Forwarding section for the Prometheus pod and click the Stop button.
Some Common Issues
Some common issues that might occur in the processs:
- Error: “Unable to forward port”: Ensure that the pod you are trying to forward is running and that the port you are trying to forward (9090) is available and open on the Prometheus pod.
- Prometheus Web Interface Not Accessible: Double-check that the port forwarding is active and that you’re using the correct
localhost:9090URL.
Conclusion
Port forwarding is a powerful feature that allows you to access internal Kubernetes services, such as Prometheus, from your local machine. In this guide, we walked through the steps of setting up port forwarding using kubectl and OpenLens, making it easy to connect to Prometheus running inside your Kubernetes cluster.
With port forwarding set up, you can access Prometheus securely, query metrics, and gain valuable insights into your Kubernetes-based applications.
Let me know if you have any questions or need further clarification on any of the steps!







