Configuring Access to Prometheus and Grafana via Sub-paths

tinhtq97

Raymond

Posted on June 18, 2024

Configuring Access to Prometheus and Grafana via Sub-paths

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)

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

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

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`
Enter fullscreen mode Exit fullscreen mode

For instance

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
Enter fullscreen mode Exit fullscreen mode
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
Enter fullscreen mode Exit fullscreen mode

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

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
Enter fullscreen mode Exit fullscreen mode

Execute the following command to apply the changes:

kubectl apply -f ingress.yml
Enter fullscreen mode Exit fullscreen mode

The result is shown below

Prometheus

Grafana

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.

๐Ÿ’– ๐Ÿ’ช ๐Ÿ™… ๐Ÿšฉ
tinhtq97
Raymond

Posted on June 18, 2024

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related

ยฉ TheLazy.dev

About