Monitoring Java Spring Microservices with Prometheus and Grafana using Helm in a Kubernetes Environment

Youssef EL GAMRANI
5 min readJul 7, 2023

--

Monitoring is crucial to any microservices architecture to ensure system health, performance, and reliability. This article will explore how to monitor Java Spring microservices using Prometheus and Grafana in a Kubernetes environment. We will leverage Helm, a popular package manager for Kubernetes, to simplify the deployment and management of Prometheus and Grafana.

Prerequisites: Before getting started, make sure you have the following components set up:

  1. A Kubernetes cluster is up and running.
  2. Helm installed on your local machine.
  3. A Spring Boot application as a microservice example deployed in Kubernetes pod, exposed and running in port 8081.

This article will be divided into 4 parts :

  • Part 1: Deploying and Installing Prometheus using Helm
  • Part 2: Deploying and Installing Grafana using Helm
  • Part 3: Instrumenting Java Spring-based Microservices
  • Part 4: Visualizing Metrics in Grafana

Before diving deep into each part and explaining it in detail, let’s design the architecture overview.

Part 1: Deploying and Installing Prometheus using Helm

Prometheus is a widely adopted monitoring solution that collects metrics from various targets. Helm allows us to install Prometheus in a Kubernetes environment easily by following these steps:

1- Add the Prometheus Helm repository:

helm repo add prometheus-community https://prometheus-community.github.io/helm-charts
helm repo update

2- Create a prometheus-values.yaml file and configure Prometheus. Here's a sample configuration:

The prometheus-values.yaml file is used to provide custom configuration options for Prometheus when deploying it using Helm. Helm allows us to override default configuration values for Helm charts by providing a custom values file.

In the case of Prometheus, the prometheus-values.yaml file allows us to customize various aspects of its deployment. For example, we can configure scrape intervals, alerting rules, and target configurations. By using a separate values file, we can easily manage and version control our Prometheus configuration separately from the Helm chart itself.

server:
global:
scrape_interval: 15s # Specifies the time interval at which Prometheus will scrape and collect metrics from the configured targets
alerting:
alertmanagers:
- scheme: http
static_configs:
- targets:
- alertmanager:9093
scrape_configs:
- job_name: 'prometheus'
metrics_path: /actuator/prometheus
static_configs:
- targets: ['localhost:8081'] # Prometheus should scrape metrics from the instance running and listening on port `9090`

3- Install Prometheus using Helm:

helm install prometheus prometheus-community/prometheus -f prometheus-values.yaml
Installation result
Prometheus server pods created

4- Run Prometheus:

kubectl port-forward service/prometheus-server 9090:80

Part 2: Deploying and Installing Grafana using Helm

Grafana is a popular open-source platform for visualizing metrics. Helm simplifies the deployment of Grafana as well. Let’s proceed:

1- Add the Grafana Helm repository:

helm repo add grafana https://grafana.github.io/helm-charts
helm repo update

2- Install Grafana using Helm:

helm install grafana grafana/grafana
Grafana pod created

3- Obtain the admin password for Grafana:

kubectl get secret --namespace default grafana -o jsonpath="{.data.admin-password}" | base64 --decode ; echo

4- Access Grafana by forwarding the port to your local machine:

kubectl port-forward service/grafana 3000:80

4- Open your web browser and navigate to http://localhost:3000. Log in using the default username admin and the password obtained in the previous step.

Part 3: Instrumenting Java Spring-based Microservices

1- add Micrometer to our project and enable Prometheus metrics endpoint:

Micrometer is a powerful metrics collection library that provides a vendor-neutral API for gathering application metrics. It offers a consistent programming model across various monitoring systems, including Prometheus. Micrometer acts as a bridge between your application and the monitoring system, allowing you to instrument your code and collect metrics easily.

The /actuator/metrics Endpoint in Spring Boot provides a comprehensive set of metrics exposed by Micrometer. This endpoint returns a JSON representation of all available metrics in your application. While this endpoint is useful for general metric inspection and monitoring purposes, it doesn't adhere to the specific data format expected by Prometheus.

On the other hand, the /actuator/prometheus endpoint is specifically designed to expose metrics in the Prometheus text-based format. It provides the metrics data in a format that Prometheus can directly scrape and ingest. By using the /actuator/prometheus endpoint, you enable direct integration with Prometheus, allowing it to collect and process metrics without requiring any additional transformation or adaptation steps.

<dependency>
<groupId>io.micrometer</groupId>
<artifactId>micrometer-registry-prometheus</artifactId>
</dependency>
management:
endpoints:
web:
exposure:
include: prometheus # Expose the Prometheus endpoint
metrics:
export:
prometheus:
enabled: true # Enable the Prometheus registry
/actuator/prometheus example result

Part 4: Visualizing Metrics in Grafana

Now that we have Prometheus collecting metrics from our Java Spring microservices, let’s configure Grafana to visualize the data.

  1. Log in to the Grafana web interface.
  2. Add Prometheus as a data source:
  • Click on the “Connections” gear icon in the left sidebar.
  • Select “Data Sources” and click on “Add data source.”
  • Choose “Prometheus” as the data source type.
  • Set the URL to http://localhost:9090 (assuming you deployed Prometheus using Helm).

3. Create a dashboard and add panels to visualize the desired metrics:

  • Click on the “+” icon in the left sidebar and select “Dashboard.”
  • Choose “Add new panel” and configure the panel with the desired metric queries and visualization options.

This article explored how to monitor Java Spring microservices using Prometheus and Grafana in a Kubernetes environment. We utilized Helm to simplify the deployment and management of Prometheus and Grafana. By instrumenting our Spring Boot applications and configuring Grafana, we can effectively monitor the health and performance of our microservices. Monitoring is an essential practice to ensure the reliability and scalability of modern distributed systems.

--

--

Youssef EL GAMRANI

Curious Software Craftsman. Passionate about code, team work, software architecture and coffee !!