Monitoring Spring Boot Apps on Kubernetes Using Prometheus and Grafana
Monitoring is a critical component of maintaining the reliability, performance, and scalability of applications deployed in Kubernetes environments. By combining the power of Spring Boot Actuator, Prometheus, and Grafana, you can establish a robust monitoring pipeline. This guide covers the essential steps for integrating these tools, from enabling metrics collection to creating insightful dashboards and alerting systems.
Enabling Spring Boot Actuator /metrics
Spring Boot Actuator makes it simple to expose application metrics, health information, and other operational data. By enabling the /metrics
endpoint, Spring Boot applications provide rich data that can be collected by Prometheus for further analysis.
Step 1. Add the Actuator Dependency
Add the following dependency to your pom.xml
file to enable Actuator:
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency>
If you’re using Gradle, include this:
implementation 'org.springframework.boot:spring-boot-starter-actuator'
Step 2. Enable the /metrics
Endpoint
By default, Actuator exposes a variety of system and application metrics. To expose the /metrics
endpoint, configure it in application.properties
:
management.endpoints.web.exposure.include=metrics
Or, using YAML:
management: endpoints: web: exposure: include: metrics
Step 3. Verify the /metrics
Endpoint
Start your application and check the metrics endpoint:
curl http://localhost:8080/actuator/metrics
You should see a list of available metrics, e.g.:
{ "names": [ "jvm.memory.used", "http.server.requests", "application.ready", ... ] }
Adding micrometer-registry-prometheus
to the Project
Micrometer acts as a facade for various metric collection systems, enabling seamless integration with Prometheus.
Step 1. Add the Prometheus Registry Dependency
Include the micrometer-registry-prometheus
dependency in your pom.xml
:
<dependency> <groupId>io.micrometer</groupId> <artifactId>micrometer-registry-prometheus</artifactId> </dependency>
For Gradle projects:
implementation 'io.micrometer:micrometer-registry-prometheus'
Step 2. Configure Prometheus Metrics Export
Expose the Prometheus scrape endpoint by specifying the configuration in application.properties
:
management.metrics.export.prometheus.enabled=true management.endpoints.web.exposure.include=prometheus
The Prometheus metrics endpoint will be available at /actuator/prometheus
.
Exposing the Metrics Endpoint on Port 8080 in Kubernetes
Kubernetes configurations often map application ports to external services. Here’s how you can expose the Prometheus metrics endpoint in your Spring Boot deployment:
Example YAML Configuration
apiVersion: apps/v1 kind: Deployment metadata: name: springboot-app spec: replicas: 2 template: spec: containers: - name: springboot-app-container image: springboot-app ports: - containerPort: 8080 # Exposing port where the Spring Boot app runs
Ensure the Service exposes port 8080 to make it accessible for Prometheus scraping:
apiVersion: v1 kind: Service metadata: name: springboot-service spec: type: ClusterIP ports: - protocol: TCP port: 8080 targetPort: 8080 selector: app: springboot-app
Deploying Prometheus in Kubernetes
Prometheus scrapes metrics from /actuator/prometheus
and stores them for analysis.
Step 1. Install Prometheus
Use Helm to deploy Prometheus:
helm repo add prometheus-community https://prometheus-community.github.io/helm-charts helm repo update helm install prometheus prometheus-community/prometheus
Step 2. Configure Prometheus to Scrape Metrics
Update the Prometheus configuration to include the Spring Boot application:
scrape_configs: - job_name: "springboot-app" metrics_path: "/actuator/prometheus" static_configs: - targets: - springboot-service.default.svc.cluster.local:8080
Reload the Prometheus configuration after updating.
Creating Dashboards with Grafana
Grafana provides powerful visualization capabilities for Prometheus metrics.
Step 1. Install Grafana
Deploy Grafana using Helm:
helm install grafana grafana/grafana
Step 2. Connect Grafana to Prometheus
- Log in to Grafana and add Prometheus as a data source.
- URL for Prometheus should be
http://prometheus-server.default.svc.cluster.local
.
Step 3. Import a Dashboard
Search for pre-built Spring Boot dashboards on Grafana’s official website or create a custom one.
Adding Custom Metrics with @Timed
and @Gauge
Micrometer allows defining custom application metrics.
Example with @Timed
@RestController public class MetricsController { @Timed(value = "custom.endpoint.timing", description = "Time taken to return the response") @GetMapping("/custom-metrics") public String customMetrics() { return "Hello, Metrics!"; } }
Example with @Gauge
@Component public class CustomGauge { private final AtomicInteger gaugeValue = new AtomicInteger(0); @Gauge(name = "custom.gauge.value", description = "A custom gauge metric") public int getGaugeValue() { return gaugeValue.get(); } }
Setting Up Alerts in Grafana
Grafana alerts notify you when a metric crosses a specified threshold.
- Define a PromQL query (e.g.,
http_server_requests_seconds_count
). - Set thresholds for warning and critical stages.
- Configure notification channels like Slack or email.
Exporting Container-Level Metrics
Use cAdvisor to monitor container metrics alongside application metrics:
kubectl apply -f https://github.com/google/cadvisor/blob/master/deploy/kubernetes/standalone/cadvisor.yaml
Connect cAdvisor to Prometheus for unified monitoring.
Final Dashboard Link and Repository
Here is a sample GitHub repository containing all configuration YAML files and the Grafana dashboard JSON:
- Repository: GitHub – Monitoring-SpringBoot
- Grafana Dashboard JSON: GrafanaDashboard.json
FAQ
What is the purpose of /metrics
and /prometheus
endpoints?
The /metrics
endpoint provides aggregated application metrics, while /prometheus
renders them in Prometheus-friendly format for scraping.
What’s the simplest way to deploy Prometheus?
Using Helm significantly simplifies deploying Prometheus in Kubernetes. Alternatively, you can manually define its deployment YAML.
Can I monitor container resource usage in Grafana?
Yes, container metrics can be scraped using cAdvisor and included in Grafana dashboards.
How do I ensure secure Prometheus scraping?
Secure the Prometheus service with RBAC policies, and consider TLS for additional security.
Can Grafana send alerts automatically?
Yes, Grafana allows you to configure alerting rules and integrate them with notification channels like email or Slack.
By following these steps, you can build a robust observability pipeline for monitoring Spring Boot applications in Kubernetes, providing actionable insights and better operational control.