> ## Documentation Index
> Fetch the complete documentation index at: https://notes.kodekloud.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Demo Connecting a Datasource in Grafana

> Guide to configure Grafana to use an in-cluster Prometheus data source in Kubernetes, including DNS service names, datasource setup, testing and troubleshooting.

Grafana visualizes time-series data but does not store metrics itself. To display metrics, Grafana needs a data source that points to a metrics backend such as Prometheus. This guide shows how to connect Grafana to an in-cluster Prometheus instance (kube-prometheus-stack) running in the `monitoring` namespace.

Environment: kube-prometheus-stack is installed in the `monitoring` namespace. Verify the Prometheus and Grafana pods and services with kubectl.

```bash theme={null}
# pods in the monitoring namespace
controlplane ~ ➜  kubectl get pods -n monitoring
NAME                                                   READY   STATUS    RESTARTS   AGE
kube-prometheus-stack-grafana-84ct9bcd57-9vm7h         2/2     Running   0          12m
kube-prometheus-stack-kube-state-metrics-567d49447b-lf2pz 1/1  Running   0          12m
kube-prometheus-stack-operator-5d99bc6db8-8bqhx         1/1     Running   0          12m
kube-prometheus-stack-prometheus-node-exporter-5r5x7   1/1     Running   0          12m
prometheus-kube-prometheus-stack-prometheus-0          2/2     Running   0          12m
```

```bash theme={null}
# services in the monitoring namespace
controlplane ~ ➜  kubectl get svc -n monitoring
NAME                                            TYPE        CLUSTER-IP       EXTERNAL-IP   PORT(S)                                       AGE
kube-prometheus-stack-grafana                   NodePort    172.20.151.255   <none>        80:30080/TCP                                  13m
kube-prometheus-stack-kube-state-metrics        ClusterIP   172.20.6.143     <none>        8080/TCP                                      13m
kube-prometheus-stack-operator                  ClusterIP   172.20.149.171   <none>        443/TCP                                       13m
kube-prometheus-stack-prometheus                NodePort    172.20.50.36     <none>        9090:30090/TCP,8080:30392/TCP                 13m
kube-prometheus-stack-prometheus-node-exporter  ClusterIP   172.20.93.180    <none>        9100/TCP                                      13m
prometheus-operated                              ClusterIP   None             <none>        9090/TCP                                      13m
```

From the service listing you can quickly identify where to point Grafana:

| Service (Kubernetes) | Purpose | Key ports |
| - | - | - |
| `prometheus` / `prometheus-operated` | Prometheus HTTP API (query endpoint) | `9090` (cluster port) — NodePort mapped to `30090` |
| `kube-prometheus-stack-grafana` | Grafana UI | container `80` — NodePort mapped to `30080` |

For background on in-cluster DNS resolution and service names, see the Kubernetes DNS docs: [https://kubernetes.io/docs/concepts/services-networking/dns-pod-service/](https://kubernetes.io/docs/concepts/services-networking/dns-pod-service/)

Next, open Grafana and add Prometheus as a data source.

Click "Add your first data source" (or Add data source) and choose Prometheus from the list:

![A screenshot of Grafana's "Add data source" interface in dark mode. It shows a list of time-series and logging/database options including Prometheus, Graphite, InfluxDB, OpenTSDB, Loki, and Elasticsearch.](https://mintcdn.com/kodekloud-c4ac6d9a/seqLTE3JlSrVAtw9/images/Prep-Course-Certified-Cloud-Native-Platform-Engineering-CNPE/Observability-and-Operations/Demo-Connecting-a-Datasource-in-Grafana/grafana-add-data-source-dark.jpg?fit=max\&auto=format\&n=seqLTE3JlSrVAtw9\&q=85\&s=6448cac8a72f3463434f140c78d0c647)

On the Prometheus data source configuration page:

* Leave Name as the default (`Prometheus`) unless you need multiple Prometheus endpoints.
* Set the URL to point to your Prometheus service inside the cluster. When Grafana runs inside the same cluster, use the in-cluster DNS name in this form:

```text theme={null}
http://prometheus-operated.monitoring.svc.cluster.local:9090
```

This matches the Kubernetes service DNS pattern: `service-name.namespace.svc.cluster.local:port`.

If Grafana runs inside the same Kubernetes cluster, the in-cluster DNS name (`prometheus-operated.monitoring.svc.cluster.local:9090`) will resolve automatically. If Grafana runs outside the cluster, point the URL to an external address such as the NodePort (e.g., `http://<NODE_IP>:30090`) or an Ingress endpoint instead.

Optionally enable the "Default" toggle to make this Prometheus instance the default for new dashboards. Leave other settings at their defaults unless you have specific proxy, authentication, or TLS needs. Click Save & test.

If the connection succeeds, Grafana will confirm it can query the Prometheus API with a success banner:

![A screenshot of Grafana’s Connections > Data sources > Prometheus settings page showing performance and query options (e.g., cache level, HTTP method set to POST, series limit 40000). A green banner at the bottom indicates "Successfully queried the Prometheus API," with Delete and Save & test buttons visible.](https://mintcdn.com/kodekloud-c4ac6d9a/seqLTE3JlSrVAtw9/images/Prep-Course-Certified-Cloud-Native-Platform-Engineering-CNPE/Observability-and-Operations/Demo-Connecting-a-Datasource-in-Grafana/grafana-prometheus-datasource-settings-post-40000.jpg?fit=max\&auto=format\&n=seqLTE3JlSrVAtw9\&q=85\&s=97ec38ae750481c937403583f0069ca8)

Troubleshooting checklist (if Save & test fails):

* Ensure the URL includes the scheme (`http://`) and correct service name, namespace and port.
* Confirm Prometheus is running: `kubectl get pods -n monitoring`.
* Verify Grafana's network access:
  * If Grafana is in-cluster, ensure it can resolve the in-cluster DNS name (test with a busybox or curl pod).
  * If Grafana is external, test connectivity to the NodePort or Ingress (`curl http://<NODE_IP>:30090/api/v1/status/flags`).
* Check Prometheus logs for errors: `kubectl logs -n monitoring prometheus-kube-prometheus-stack-prometheus-0`.

Avoid exposing Prometheus or Grafana publicly without authentication or network restrictions. If you use NodePorts or Ingress, add proper access controls (Ingress auth, network policies, or firewall rules).

What’s next

* With Prometheus connected, you can build dashboards and panels in Grafana using PromQL queries.
* For Prometheus query examples and the HTTP API, see: [https://prometheus.io/docs/prometheus/latest/querying/api/](https://prometheus.io/docs/prometheus/latest/querying/api/)
* For more on configuring Grafana datasources and advanced settings, see Grafana docs: [https://grafana.com/docs/grafana/latest/datasources/add-a-data-source/](https://grafana.com/docs/grafana/latest/datasources/add-a-data-source/)

> **[Watch Video](https://learn.kodekloud.com/user/courses/prep-course-certified-cloud-native-platform-engineer-cnpe/module/9bd090c8-8d99-4742-b50c-ae63e516e6b9/lesson/b380cd38-72e3-4794-8486-ab7f52739839)**


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.