# Connect Kubernetes metrics to Existing Prometheus

**URL:** <https://discuss.prometheus.io/t/connect-kubernetes-metrics-to-existing-prometheus/1512>\
**Category:** Prometheus server\
**Created:** [March 8, 2023, 3:57pm UTC](https://discuss.prometheus.io/t/connect-kubernetes-metrics-to-existing-prometheus/1512 "2023-03-08T15:57:31Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![eliassal](https://avatars.discourse-cdn.com/v4/letter/e/43a26b/32.png) [@eliassal](https://discuss.prometheus.io/u/eliassal)\
**Post date:** [March 8, 2023, 3:57pm UTC](https://discuss.prometheus.io/t/connect-kubernetes-metrics-to-existing-prometheus/1512/1 "2023-03-08T15:57:32Z")

</div>

Hi, I have Prometheus server up and running and gather metrics from different linux and windows server. I recently created a 3 ubuntu nodes K8S cluster and would like to know if there is a way/exporter rom K8S to my prometheus/Grafana infra (something like linux or windows or databases exporters) .  
I googles always I came across different articles and blogs and K8s do where they all talk about deploying prokeheus, grafana…inside K8S. is this the only possibility o get metrics from K8S cluster to prometheus/Greafana?Thanks in advance

---

<div class="post-metadata">

**Author:** ![stuart](https://dub1.discourse-cdn.com/flex017/user_avatar/discuss.prometheus.io/stuart/32/18_2.png) [@stuart](https://discuss.prometheus.io/u/stuart)\
**Post date:** [March 8, 2023, 7:09pm UTC](https://discuss.prometheus.io/t/connect-kubernetes-metrics-to-existing-prometheus/1512/2 "2023-03-08T19:09:34Z")

</div>

So are you talking about scraping metrics from Kubernetes pods from an existing Prometheus server, that’s hosted outside of Kubernetes?

---

<div class="post-metadata">

**Author:** ![eliassal](https://avatars.discourse-cdn.com/v4/letter/e/43a26b/32.png) [@eliassal](https://discuss.prometheus.io/u/eliassal)\
**Post date:** [March 8, 2023, 9:00pm UTC](https://discuss.prometheus.io/t/connect-kubernetes-metrics-to-existing-prometheus/1512/3 "2023-03-08T21:00:32Z")

</div>

Yes, exact as we have our prometheus/grafana up and running since long time and work fine . K8S was setup to do a POC to validate migration of a monolithic app to microservices architecture using K8S to show to business people

---

<div class="post-metadata">

**Author:** ![eliassal](https://avatars.discourse-cdn.com/v4/letter/e/43a26b/32.png) [@eliassal](https://discuss.prometheus.io/u/eliassal)\
**Post date:** [March 10, 2023, 10:28am UTC](https://discuss.prometheus.io/t/connect-kubernetes-metrics-to-existing-prometheus/1512/4 "2023-03-10T10:28:35Z")

</div>

Hi Stuart, any feedback on this, thansk?

---

<div class="post-metadata">

**Author:** ![stuart](https://dub1.discourse-cdn.com/flex017/user_avatar/discuss.prometheus.io/stuart/32/18_2.png) [@stuart](https://discuss.prometheus.io/u/stuart)\
**Post date:** [March 10, 2023, 12:11pm UTC](https://discuss.prometheus.io/t/connect-kubernetes-metrics-to-existing-prometheus/1512/5 "2023-03-10T12:11:34Z")

</div>

So it is totally possible to have a Prometheus server outside of the cluster. You would however need a few things:

1. Access to the internal cluster networking, so pods can be accessed for scraping (which could be by joining a service mesh overlay you are using for example)
2. Use the kubernetes\_sd option in Prometheus, which needs access to the Kubernetes API endpoint from your Prometheus server, with appropriate security token.

Alternatively it is often easier to run something within the Kubernetes cluster that does the scraping, and then use remote write to send that data to the external Prometheus. You can run full-fat Prometheus within the cluster, or just use “agent mode”. Another useful option it to additionally use the Prometheus Operator, which allows you to more easily setup scrape configurations via ServiceMonitor & PodMonitor custom resources.

---

<div class="post-metadata">

**Author:** ![eliassal](https://avatars.discourse-cdn.com/v4/letter/e/43a26b/32.png) [@eliassal](https://discuss.prometheus.io/u/eliassal)\
**Post date:** [March 10, 2023, 1:55pm UTC](https://discuss.prometheus.io/t/connect-kubernetes-metrics-to-existing-prometheus/1512/6 "2023-03-10T13:55:10Z")

</div>

Thanks for all those details, we would prefer to go with the 2nd option as it is the easiest and quickest way for our POC. Any url or instructions on how to use “kubernetes\_sd option in Prometheus” as this is the 1st time we hear of this term, thanks again

---

<div class="post-metadata">

**Author:** ![stuart](https://dub1.discourse-cdn.com/flex017/user_avatar/discuss.prometheus.io/stuart/32/18_2.png) [@stuart](https://discuss.prometheus.io/u/stuart)\
**Post date:** [March 10, 2023, 3:11pm UTC](https://discuss.prometheus.io/t/connect-kubernetes-metrics-to-existing-prometheus/1512/7 "2023-03-10T15:11:55Z")

</div>

The two options are:

1. Use kubernetes\_sd, which also means being able to access pods from the Prometheus server (e.g. via a service mesh or overlay network)
2. Run Prometheus/Prometheus agent mode/Prometheus Operator within the cluster and remote write to the external Prometheus.

These are the docs for kubernetes\_sd: [Configuration | Prometheus](https://prometheus.io/docs/prometheus/latest/configuration/configuration/#kubernetes_sd_config)

---

<div class="post-metadata">

**Author:** ![eliassal](https://avatars.discourse-cdn.com/v4/letter/e/43a26b/32.png) [@eliassal](https://discuss.prometheus.io/u/eliassal)\
**Post date:** [March 10, 2023, 4:06pm UTC](https://discuss.prometheus.io/t/connect-kubernetes-metrics-to-existing-prometheus/1512/8 "2023-03-10T16:06:18Z")

</div>

I visited the github at  
[prometheus/prometheus-kubernetes.yml at release-2.42 · prometheus/prometheus (github.com)](https://github.com/prometheus/prometheus/blob/release-2.42/documentation/examples/prometheus-kubernetes.yml)

but it is not indicated where this file should be located. Should it be located on the K8S master machine somewhere on on the prometheus server which a remote server? Also, you indicated  
with appropriate security token…  
but the example only shows https connection without using access keys usage

---

<div class="post-metadata">

**Author:** ![stuart](https://dub1.discourse-cdn.com/flex017/user_avatar/discuss.prometheus.io/stuart/32/18_2.png) [@stuart](https://discuss.prometheus.io/u/stuart)\
**Post date:** [March 10, 2023, 6:09pm UTC](https://discuss.prometheus.io/t/connect-kubernetes-metrics-to-existing-prometheus/1512/9 "2023-03-10T18:09:32Z")

</div>

The example you gave is the scrape configuration containing a few example jobs - these live in the prometheus.yaml file on your Prometheus server. However this example is designed to be run on a Prometheus that is itself hosted within Kubernetes.

You can use that as a basic example, but you’d need to look at adding the `api_server`, `basic_auth`, `authorization` or `oauth2` options to configure the connection to the Kubernetes API (if you host Prometheus directly on Kubernetes it can figure those out automatically). You likely would need to set the `tls_config` settings too.

While it is totally possible to get it all working well (don’t forget you need the correct network access too to be able to access pods, etc.) I think most people find it easier to just run something within the cluster (e.g. Prometheus in agent mode) to handle the scraping instead of trying to do it from outside - and a fair number of people find using the Prometheus Operator useful for setting up scraping configurations.

---

<div class="post-metadata">

**Author:** ![eliassal](https://avatars.discourse-cdn.com/v4/letter/e/43a26b/32.png) [@eliassal](https://discuss.prometheus.io/u/eliassal)\
**Post date:** [March 10, 2023, 7:04pm UTC](https://discuss.prometheus.io/t/connect-kubernetes-metrics-to-existing-prometheus/1512/10 "2023-03-10T19:04:48Z")

</div>

stuart, to be frank, I am a little bit confused. Ok, I will drop the idea of using our prom and adopt/follow your recommendation

> just run something within the cluster (e.g. **Prometheus in agent mode** ) to handle the scraping…of people find using the **Prometheus Operator** useful for setting up scraping configurations

So should I install both, Prometheus in agent mode and Prometheus Operator inside the cluster? They run as pod or containers? Any idea of a tutorial or step-by-etep guide? Thanks for your help
