<p>I have a prometheus on my kuberetes with prometheus-operated service</p>
<pre><code>NAME                       TYPE        CLUSTER-IP       EXTERNAL-IP   PORT(S)       AGE
prometheus-operated        ClusterIP   None             &lt;none&gt;        9090/TCP      13d
</code></pre>
<p>and a flask app deployment</p>
<pre><code>NAME                         READY   STATUS    RESTARTS   AGE
flask-app-7d59898b78-2j59f   1/1     Running   0          23m
</code></pre>
<p>I want to connect to prometheus-operated from my app by sending get request</p>
<pre><code>URL = &quot;http://localhost:9090/api/v1/query_range&quot;
PROMQL = ...
r1 = requests.get(url=URL, params=PROMQL)
</code></pre>
<p>but I'm getting Connection Refused errors</p>
<pre><code>ConnectionError: HTTPConnectionPool(host='localhost', port=9090): Max retries exceeded with url: /api/v1/query_range?query= &lt;&lt;&lt;my long query here&gt;&gt;&gt;  (Caused by NewConnectionError('&lt;urllib3.connection.HTTPConnection object at 0x7fbbc01b6d60&gt;: Failed to establish a new connection: [Errno 111] Connection refused'))
</code></pre>
<p>When i run my flask app outside of kubernetes the connection works fine. What is the correct URL to connect to prometheus-operated?</p>

Nimander
<p>If both Prometheus and your flask app are deployed in the same namespace, you can use the service name <code>prometheus-operated </code> to connect to it.</p>
<pre><code>URL = &quot;http://prometheus-operated:9090/api/v1/query_range&quot;
</code></pre>
<p>or in case of a different namespace, assuming you don't have network policies in place and your cluster is deployed with Kube DNS:
The Kube-DNS naming convention is <code>service.namespace.svc.cluster-domain.tld</code> and the default cluster domain is <code>cluster.local</code>.</p>
<p>For example, if you want to contact a service called <code>mysql</code> in the <code>db</code> namespace from any namespace, you can simply speak to <code>mysql.db.svc.cluster.local</code>.</p>
<p>So:</p>
<pre><code>URL = &quot;http://prometheus-operated.$PROM_NAMESPACE.svc.cluster.local:9090/api/v1/query_range&quot;
</code></pre>
