Join our Discord Server
Pushkar Joshi Site Reliability Engineer Engineer with 5+ years of hands-on experience supporting, automation, and optimizing critical tasks in administration on Linux, AWS, and GCP, Kubernetes leveraging configuration management, CI/CD, and DevOps processes.

How to Setup Prometheus & Grafana on Kubernetes

8 min read

 

Requirements

Installation and configuration

Helm Setup

$ brew install kubernetes-helm
$ helm init
$ helm search
$ helm fetch stable/<chart_name> --untar
$ helm install ./<chart_name>

Prometheus

Prometheus with Helm:

kubectl create ns monitor
$ helm fetch stable/prometheus --untar
rbac:
create: false
size: 2Gi
$ helm install ./prometheus
NAME:   test
LAST DEPLOYED: Tue Apr 17 00:41:16 2022
NAMESPACE: default
STATUS: DEPLOYED
.
.RESOURCES:
.
.NOTES:
.
.
$ helm ls
kubectl get pods -n monitor
NAME                                                   READY   STATUS    RESTARTS   AGE 
alertmanager-prometheus-operator-alertmanager-0 2/2 Running 0 49s
prometheus-operator-grafana-5bd6cbc556-w9lds 2/2 Running 0 59s
prometheus-operator-kube-state-metrics-746dc6ccc-gk2p8 1/1 Running 0 59s
prometheus-operator-operator-7d69d686f6-wpjtd 2/2 Running 0 59s
prometheus-operator-prometheus-node-exporter-4nwbf 1/1 Running 0 59s
prometheus-operator-prometheus-node-exporter-jrw69 1/1 Running 0 59s
prometheus-operator-prometheus-node-exporter-rnqfc 1/1 Running 0 60s
prometheus-prometheus-operator-prometheus-0 3/3 Running 1 39s
$ export POD_NAME=$(kubectl get pods --namespace default -l "app=prometheus,component=server" -o jsonpath="{.items[0].metadata.name}")$ kubectl --namespace default port-forward $POD_NAME 9090

Grafana:

Grafana with Helm

$ helm fetch stable/grafana --untar
adminUser: admin
adminPassword: strongpasswordpersistence:
enabled: true
size: 4Gi
datasources:
datasources.yaml:
apiVersion: 1
datasources:
- name: Prometheus
type: prometheus
url: <PROMETHEUS_URL_HERE>
access: proxy
isDefault: true
echo http://$(kubectl get service --namespace default -l "app=prometheus,component=server" -o jsonpath="{.items[0].metadata.name}").default.svc.cluster.local
$ helm install -f grafana/values.yaml stable/grafana
NAME:   test-user
LAST DEPLOYED: Tue Apr 17 18:35:07 2022
NAMESPACE: default
STATUS: DEPLOYEDRESOURCES:
.
.NOTES:.
.

There seems to be an error with the port forwarding command as described in the NOTES, if the port forwarding command fails, run the one below but replace the <RELEASE_NAME> with your release name.

$ export POD_NAME=$(kubectl get pods --namespace default -l "app=grafana,release=<RELEASE_NAME>" -o jsonpath="{.items[0].metadata.name}")
$ kubectl --namespace default port-forward $POD_NAME 3000

Django Application Level Monitoring with Prometheus and Grafana:

INSTALLED_APPS = (
...
'django_prometheus',
...
)
MIDDLEWARE_CLASSES = (
'django_prometheus.middleware.PrometheusBeforeMiddleware',
# All your other middlewares go here, including the default
# middlewares like SessionMiddleware, CommonMiddleware,
# CsrfViewmiddleware, SecurityMiddleware, etc.
'django_prometheus.middleware.PrometheusAfterMiddleware',
)
urlpatterns = [
...
url('', include('django_prometheus.urls')),
]
$ image: <IMAGE_NAME>:<TAG>
$ kubectl apply -f django/
$ minikube service django-service

Conclusion

Have Queries? Join https://launchpass.com/collabnix

Pushkar Joshi Site Reliability Engineer Engineer with 5+ years of hands-on experience supporting, automation, and optimizing critical tasks in administration on Linux, AWS, and GCP, Kubernetes leveraging configuration management, CI/CD, and DevOps processes.

How to Build and Host Your Own MCP Servers…

Introduction The Model Context Protocol (MCP) is revolutionizing how LLMs interact with external data sources and tools. Think of MCP as the “USB-C for...
Adesoji Alu
1 min read
Join our Discord Server
Index