[Kubernetes (K8S) Ingress Nginx] Use Prometheus and Grafana for scraping the metrics of the Nginx Ingress controller

Use Prometheus and Grafana for scraping the metrics of the Nginx Ingress controller

This tutorial will show you how to use Prometheus and Grafana for scraping the metrics of the Nginx Ingress controller.

Prerequisites

Configuration

The controller should be configured for exporting metrics. This requires 3 configurations to the controller. These configurations are :

  • controller.metrics.enabled=true

  • controller.podAnnotations."prometheus.io/scrape"="true"

  • controller.podAnnotations."prometheus.io/port"="10254"

Helm

The easiest way to configure the controller for metrics is via helm upgrade. Assuming you have installed the ingress-nginx controller as a helm release named ingress-controller, then you can simply type the command show below :

1
2
3
4
5
$ helm upgrade ingress-controller ingress-nginx/ingress-nginx \
--namespace ingress-nginx \
--set controller.metrics.enabled=true \
--set-string controller.podAnnotations."prometheus\.io/scrape"="true" \
--set-string controller.podAnnotations."prometheus\.io/port"="10254"

Or set values to values.yaml shown below;

1
2
3
4
5
6
7
8
9
# values.yaml

controller:
metrics:
enabled: true
service:
annotations:
prometheus.io/port: "10254"
prometheus.io/scrape: "true"
1
2
3
$ helm upgrade ingress-controller ingress-nginx/ingress-nginx \
--namespace ingress-nginx \
--values values.yaml

Manifest

If you are not using helm, you will have to edit your manifests like this:

Service manifest:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
# manifests/Service.yaml

apiVersion: v1
kind: Service
metadata:
annotations:
prometheus.io/scrape: "true"
prometheus.io/port: "10254"

spec:
ports:
- name: prometheus
port: 10254
targetPort: prometheus

Or DaemonSet manifest:

1
2
3
4
5
# manifests/DaemonSet.yaml

ports:
- name: prometheus
containerPort: 10254

Run kubectl to apply manifests file

1
$ kubectl apply -f manifests/*.yaml

Grafana Dashboard

After the login you can import the Grafana dashboard from official dashboards, by following steps given below :

  • Left menu (hover over +) -> Dashboard

  • Click Import

  • Enter the copy pasted json from https://raw.githubusercontent.com/kubernetes/ingress-nginx/main/deploy/grafana/dashboards/nginx.json

  • Click Import JSON

  • Select the Prometheus data source

  • Click “Import”

ingress-nginx grafana

Then, follow the steps above to enter the copy pasted json from https://github.com/kubernetes/ingress-nginx/blob/main/deploy/grafana/dashboards/request-handling-performance.json

See ingress-nginx/deploy/grafana/dashboards at main · kubernetes/ingress-nginx - https://github.com/kubernetes/ingress-nginx/tree/main/deploy/grafana/dashboards to learn more.

References

[1] Prometheus and Grafana installation - Nginx Ingress Controller - https://kubernetes.github.io/ingress-nginx/user-guide/monitoring/

[2] ingress-nginx/deploy/grafana/dashboards at main · kubernetes/ingress-nginx - https://github.com/kubernetes/ingress-nginx/tree/main/deploy/grafana/dashboards

[3]] kubernetes/ingress-nginx: Nginx Ingress Controller for Kubernetes - https://github.com/kubernetes/ingress-nginx

[4] Dashboards | Grafana Labs - https://grafana.com/grafana/dashboards/

[5] Prometheus | Nginx Ingress Controller - https://docs.nginx.com/nginx-ingress-controller/logging-and-monitoring/prometheus/

[6] Prometheus | Grafana Labs - https://grafana.com/oss/prometheus/

[7] Installation | Grafana Labs - https://grafana.com/docs/grafana/latest/installation/?pg=docs

[8] Installation | Grafana Labs - https://grafana.com/docs/loki/latest/installation/

[9] Grafana Loki | Grafana Labs - https://grafana.com/oss/loki/

[10] Welcome - NGINX Ingress Controller - https://kubernetes.github.io/ingress-nginx/