Introduction

Modern microservices-based architectures rely heavily on observability to ensure applications perform reliably at scale.
For Java applications running on Kubernetes, Prometheus and Grafana are the standard tools for monitoring and visualization. Together, they offer a flexible system for collecting, storing, and analyzing metrics — allowing developers to detect issues before they affect end users.

This guide explains how to monitor Java applications on Kubernetes using Prometheus for metrics collection and Grafana for visualization, with hands-on steps, code examples, and best practices.


Why Monitoring Matters in Kubernetes Environments

Kubernetes introduces dynamic scaling, service discovery, and container orchestration — which complicate monitoring. Traditional APM tools struggle with ephemeral pods and rolling updates.

Effective monitoring helps to:

  • Detect performance bottlenecks.

  • Identify memory leaks and GC pauses in JVMs.

  • Track latency, throughput, and error rates.

  • Visualize cluster-level resource consumption.

Prometheus and Grafana simplify this by providing reliable time-series monitoring and dashboards that scale with your cluster.


Core Components

1. Prometheus

Prometheus is a time-series database built for cloud-native monitoring. It scrapes metrics from endpoints and stores them efficiently.

Key Features:

  • Pull-based data collection.

  • Flexible PromQL query language.

  • Built-in alerting with Alertmanager.

  • Multi-dimensional data model.

2. Grafana

Grafana is an open-source analytics and visualization platform that integrates with Prometheus to present data in meaningful dashboards.

Key Features:

  • Rich, interactive dashboards.

  • Query editor with PromQL support.

  • Alerts, annotations, and drill-downs.

  • Multi-source and team access management.

3. Kubernetes

Kubernetes exposes performance data through its metrics APIs (via kube-state-metrics and cAdvisor). It enables Prometheus to dynamically discover and scrape metrics from Pods, Services, and Nodes.


Step-by-Step Setup

Step 1: Deploy Prometheus on Kubernetes

Install Prometheus via Helm for simplicity:

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

Access Prometheus locally:

kubectl port-forward deploy/prometheus-server 9090:9090

Visit: http://localhost:9090


Step 2: Expose Java Application Metrics

Expose metrics in your Java app using Micrometer (built into Spring Boot Actuator).

Add dependency:

<dependency> <groupId>io.micrometer</groupId> <artifactId>micrometer-registry-prometheus</artifactId> </dependency>

Enable Prometheus endpoint:

management: endpoints: web: exposure: include: prometheus

Your metrics will now be available at:

/actuator/prometheus

Step 3: Configure Prometheus Scraping

Edit the Prometheus configuration to scrape your Java service:

scrape_configs: - job_name: 'java-app' metrics_path: '/actuator/prometheus' static_configs: - targets: ['java-app-service.default.svc.cluster.local:8080']

Reload the Prometheus configuration, and it will start scraping your application’s metrics automatically.


Step 4: Deploy Grafana and Connect to Prometheus

Install Grafana using Helm:

helm install grafana grafana/grafana

Expose the service:

kubectl port-forward svc/grafana 3000:3000

Default credentials:

Username: admin Password: admin

In Grafana → Settings → Data Sources, add Prometheus with URL:

http://prometheus-server:9090

Step 5: Build Grafana Dashboards

Import pre-made dashboards from the Grafana Dashboard Library or create custom ones.

Common JVM metrics:

Metric Description
jvm_memory_used_bytes JVM heap/non-heap memory usage
jvm_gc_pause_seconds Time spent in GC
http_server_requests_seconds Request latency
container_cpu_usage_seconds_total Pod-level CPU consumption

Example PromQL query:

rate(http_server_requests_seconds_count{status!~"2.."}[5m])

Displays non-2xx HTTP requests over a 5-minute window.


Best Practices

  1. Use Labels and Annotations — Tag services with labels (app, version, env) for easy filtering.

  2. Set Alerting Rules — Use Prometheus Alertmanager to notify when thresholds exceed limits.

  3. Secure Endpoints — Protect /actuator/prometheus with authentication or Kubernetes network policies.

  4. Service Discovery — Let Prometheus automatically detect new Pods via Kubernetes annotations.

  5. Dashboard Organization — Separate cluster-wide metrics, JVM metrics, and business KPIs into distinct dashboards.


Troubleshooting

Issue Possible Fix
Prometheus not scraping data Check kubectl logs for target errors
Metrics missing in Grafana Verify time range and PromQL syntax
No Java metrics Confirm /actuator/prometheus endpoint is reachable
Authentication errors Update ServiceAccount or RBAC permissions

Conclusion

By combining Prometheus and Grafana, Java developers can achieve end-to-end visibility into their microservices running on Kubernetes.
This setup ensures you can monitor JVM health, request latencies, and resource utilization in real time — enabling proactive optimization and reliability.

When implemented properly, this monitoring stack forms the backbone of an effective DevOps observability strategy.

References


<> “Happy developing, one line at a time!” </>


0 Comments

Leave a Reply

Avatar placeholder

Your email address will not be published. Required fields are marked *