Setting up Monitoring with Prometheus
Introduction
In this tutorial, we will walk through the process of setting up monitoring for OCS.io using Prometheus in a Docker/Kubernetes environment. We will configure Prometheus to scrape metrics from OCS.io, which will allow us to monitor the application’s performance and health.
Let’s get started!
Prerequisites
-
Spring Boot application with Micrometer support
-
Docker and Kubernetes installed and running
-
Prometheus installed and running in a separate container
Step 1: Add Dependencies
Ensure you have the necessary dependencies in your build.gradle
file.
dependencies {
implementation "io.micrometer:micrometer-core:${micrometerVersion}"
implementation "io.micrometer:micrometer-registry-prometheus:${micrometerVersion}"
implementation "org.springframework.boot:spring-boot-starter-actuator"
}
Step 2: Enable Prometheus Endpoint
Configure your application.properties
to expose the Prometheus metrics endpoint.
management.endpoints.web.exposure.include=*
management.endpoint.prometheus.enabled=true
Step 3: Configuring Prometheus and Actuator
This section describes how to configure Prometheus and Actuator in a Spring Boot application using application properties and Kubernetes ConfigMaps.
Using Spring Boot Application Properties
To configure Prometheus and Actuator using Spring Boot application properties, add the following properties to your application.properties
or application.yml
file:
# Enable Prometheus endpoint management.endpoints.web.exposure.include=prometheus management.endpoint.prometheus.enabled=true # Enable Actuator endpoints management.endpoints.web.exposure.include=health,info,metrics management.endpoint.health.show-details=always management.endpoint.metrics.enabled=true # Configure Prometheus scrape interval management.metrics.export.prometheus.step=15s
Step 4: Create Kubernetes Deployment for OCS.io
Define a Kubernetes deployment for your OCS.io application.
apiVersion: apps/v1
kind: Deployment
metadata:
name: ocs-io
spec:
replicas: 1
selector:
matchLabels:
app: ocs-io
template:
metadata:
labels:
app: ocs-io
spec:
containers:
- name: ocs-io
image: your-docker-repo/ocs-io:latest
ports:
- containerPort: 8080
Step 5: Create Kubernetes Deployment for Prometheus
Define a Kubernetes deployment for Prometheus.
apiVersion: apps/v1
kind: Deployment
metadata:
name: prometheus
spec:
replicas: 1
selector:
matchLabels:
app: prometheus
template:
metadata:
labels:
app: prometheus
spec:
containers:
- name: prometheus
image: prom/prometheus:latest
ports:
- containerPort: 9090
volumeMounts:
- name: prometheus-config
mountPath: /etc/prometheus/
volumes:
- name: prometheus-config
configMap:
name: prometheus-config
Step 6: Create Prometheus ConfigMap
Define a ConfigMap for Prometheus configuration.
apiVersion: v1
kind: ConfigMap
metadata:
name: prometheus-config
data:
prometheus.yml: |
global:
scrape_interval: 15s
scrape_configs:
- job_name: 'ocs-io'
static_configs:
- targets: ['ocs-io:8080']
Step 7: Deploy to Kubernetes
Apply the configurations to your Kubernetes cluster.
kubectl apply -f ocs-io-deployment.yaml
kubectl apply -f prometheus-deployment.yaml
kubectl apply -f prometheus-configmap.yaml
Step 8: Test the Monitoring Configuration
-
Start your OCS.io application. The system exposes metrics right after the module is started.
-
Monitor the Prometheus console output for incoming metrics and potential configuration errors.
-
Verify that metrics are being scraped by accessing Prometheus’s web interface at
localhost:9090
.
Conclusion
Congratulations! You have successfully configured monitoring for your containerized OCS.io application with Prometheus support. By scraping metrics with Prometheus, you can now monitor the performance and health of your application. Feel free to explore further customization options and integrate additional tools to enhance your monitoring and alerting workflow.