<p>I have a <code>k3s</code> cluster with a Prometheus and Alertmanager deployment inside, and set up an <code>Ingress</code> resource for each of them. My initial setup was <code>prometheus.domain.com</code> and <code>alermanager.domain.com</code> respectively, and these worked as expected.</p>
<p>However, I would like to switch it to <code>domain.com/prometheus</code> and <code>domain.com/alertmanager</code> respectively, and the options I have researched aren't working.</p>
<p><strong>GOAL</strong>
Have my Prometheus service work having <code>domain.com/prometheus</code> being the 'root' of my Prometheus pathing, hence redirecting automatically to <code>domain.com/prometheus/graph</code>, as per its default behavior, and all subpaths under Prometheus (alerts, config, admin API) follow the same behavior.</p>
<p><strong>Attempt #1</strong></p>
<pre><code>apiVersion: networking.k8s.io/v1beta1
kind: Ingress
metadata:
  app: Prometheus
  annotations:
    traefik.ingress.kubernetes.io/rewrite-target: /$2
spec:
  rules:
    - http:
        paths:
          - path: /prometheus($|/)(.*)
            backend:
              serviceName: prometheus
              servicePort: 9090
</code></pre>
<p>SSH into the Vagrant box hosting my k3s cluster:</p>
<pre><code>$ kubectl get ingress
NAME         CLASS             HOSTS         ADDRESS         PORTS
prometheus   &lt;none&gt;            *             192.168.0.200   80

$ curl 192.168.0.200/prometheus
404 page not found

$ curl 192.168.0.200/prometheus/graph
404 page not found
</code></pre>
<p><strong>Attempt #2</strong></p>
<pre><code>apiVersion: networking.k8s.io/v1beta1
kind: Ingress
metadata:
  app: Prometheus
  annotations:
    traefik.ingress.kubernetes.io/rule-type: PathPrefixStrip
spec:
  rules:
    - http:
        paths:
          - path: /prometheus
            backend:
              serviceName: prometheus
              servicePort: 9090
</code></pre>
<p>Same result as above.</p>

Xanagandr
<p>You need to start prometheus with:</p>
<pre><code>--web.route-prefix=&quot;http://example.com/prometheus&quot;
</code></pre>
<p>From <a href="https://github.com/prometheus/prometheus/blob/4e6a94a27d64f529932ef5f552d9d776d672ec22/cmd/prometheus/main.go#L160-L162" rel="nofollow noreferrer">source code</a>:</p>
<blockquote>
<p>web.external-url&quot;,
&quot;The URL under which Prometheus is externally reachable (for example, if Prometheus is served via a reverse proxy). Used for generating relative and absolute links back to Prometheus itself. If the URL has a path portion, it will be used to prefix all HTTP endpoints served by Prometheus. If omitted, relevant URL components will be derived automatically.</p>
</blockquote>
<hr />
<p>And for altermanager you need to set:</p>
<pre><code>--alertmanager.url=http://example.com/alertmanager
</code></pre>
<hr />
<p><strong>NOTE:</strong> Don't use rewrites in ingress in this case.</p>
