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:
Access Prometheus locally:
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:
Enable Prometheus endpoint:
Your metrics will now be available at:
Step 3: Configure Prometheus Scraping
Edit the Prometheus configuration to scrape your Java service:
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:
Expose the service:
Default credentials:
In Grafana → Settings → Data Sources, add Prometheus with URL:
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:
Displays non-2xx HTTP requests over a 5-minute window.
Best Practices
-
Use Labels and Annotations — Tag services with labels (
app,version,env) for easy filtering. -
Set Alerting Rules — Use Prometheus Alertmanager to notify when thresholds exceed limits.
-
Secure Endpoints — Protect
/actuator/prometheuswith authentication or Kubernetes network policies. -
Service Discovery — Let Prometheus automatically detect new Pods via Kubernetes annotations.
-
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.
0 Comments