Configuring Access to Prometheus and Grafana via Sub-paths
Published: 2024-06-18 00:41:30

## 1. Introduction
Grafana and Prometheus stand as integral tools for monitoring infrastructures across various industry sectors. This document aims to illustrate the process of redirecting Grafana and Prometheus to operate under the same domain while utilizing distinct paths.

## 2. How does the story begin?
As a seasoned DevOps engineer, my commitment lies in sharing knowledge with a broader audience. My pursuit involves identifying tools conducive to enhancing the monitoring capabilities of both general users and fellow DevOps Engineers.

## 3. Prerequisites
Functioning Kubernetes Cluster
Proficiency in Kubernetes Management
Helm Installation
Optional: K9s for Kubernetes Cluster Observation ([GitHub link](https://github.com/derailed/k9s))

## 4. Goals & Objectives
After researching, I found a lack of available information on this topic despite numerous related questions online. Hence, I’m writing a guide to fill this gap.

## 5. Step-by-Step Guide
### Step 1: Installing [Kube-prometheus-stack](https://github.com/prometheus-community/helm-charts/tree/main/charts/kube-prometheus-stack)

Create a values.yml file to configure the Helm chart values for both Prometheus and Grafana.

```yaml
grafana:
  env:
    GF_SERVER_SERVE_FROM_SUB_PATH: true
grafana.ini:
  server:
    domain: "<domain>"
    root_url: "<protocol>:<domain>/<path>/"
prometheus:
  prometheusSpec:
    externalUrl: "<protocol>:<domain>/<path>/"
    routePrefix: /path`
```

For instance

```yaml
grafana:
  env:
    GF_SERVER_SERVE_FROM_SUB_PATH: true
  grafana.ini:
    server:
      domain: "example.com"
      root_url: "https://example.com/grafana/"
prometheus:
  prometheusSpec:
    externalUrl: "https://example.com/prometheus/"
    routePrefix: /prometheus
```

```bash
helm repo add prometheus-community https://prometheus-community.github.io/helm-charts
helm repo update
helm install <relase-name> prometheus-community/kube-prometheus-stack -n <namespace> -f values.yml
```

### Step 2. Create Ingress for mapping domain

There are two methods to accomplish this task:

Define in the values.yml file for automatic creation.
Another approach involves manually creating a YAML file for the ingress. I opted for this method because it allows centralized management, facilitating easy problem identification if any issues arise.
Create ingress.yml file

```yaml
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: prometheus
  namespace: monitoring
  annotations:
    kubernetes.io/ingressClassName: "traefik"
spec:
  rules:
    - host: ""
      http:
        paths:
          - path: /prometheus
            pathType: Prefix
            backend:
              service:
                name: <helm-release-name>-kube-prometheus-prometheus
                port:
                  number: 9090
          - path: /grafana
            pathType: Prefix
            backend:
              service:
                name: <helm-release-name>-grafana
                port:
                  number: 80
```

Execute the following command to apply the changes:
```bash
kubectl apply -f ingress.yml
```
The result is shown below


![Prometheus](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/jalm1a254zco31gd7p53.png)


![Grafana](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/pg03vto5t355vxabom8t.png)




## 6. Conclusion
As I venture into writing for Medium, I acknowledge potential errors in this documentation. I invite feedback and further inquiries from readers to improve the accuracy and comprehensiveness of this guide. Thank you for your attention and engagement.