Monitoring Spring Boot Application with Prometheus and Grafana on Kubernetes

unsplash-logoCarlos Muza

Welcome to the second post on Prometheus & Grafana. In last post Monitoring Spring Boot Application with Prometheus and Grafana we Integrated Prometheus , Spring Boot and Grafana using docker.

In this post we will discuss, How to setup Prometheus and Grafana on Kubernetes using Helm Charts

The code for this post is available on Github here

If you’re new to Kubernetes & Prometheus I recommend reading the following hands-on guide on Kubernetes.

  1. Deploy React, Spring Boot & MongoDB Fullstack application on Kubernetes
  2. Monitoring Spring Boot Application with Prometheus and Grafana

Prerequisites

You need to have Kubectl, Helm, Minikube installed on your machine. To Follow along this post, Basic knowledge of Kubernetes is needed.

Step 1 : Deploy a Spring Boot application on Kubernetes and expose actuator endpoints

  1. How to deploy Spring boot application on Kubernetes is explained in detail here
  2. How to expose actuator endpoints for Prometheus is explained here.

In Kubernetes environment , we can configure annotations which will be used by prometheus to scrap data.Below is the complete deployment.yaml file

spring-boot-prometheus-deployment.yaml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
apiVersion: apps/v1
kind: Deployment
metadata:
name: spring-boot-prometheus
spec:
selector:
matchLabels:
app: spring-boot-prometheus
replicas: 1
template:
metadata:
labels:
app: spring-boot-prometheus
annotations:
prometheus.io/scrape: "true"
prometheus.io/port: "8080"
prometheus.io/path: "/actuator/prometheus"
spec:
containers:
- name: spring-boot-prometheus
image: nirajsonawane/spring-boot-prometheus:0.0.1-SNAPSHOT
imagePullPolicy: Always
ports:
- containerPort: 8080
resources:
limits:
memory: 294Mi

Step 2 : Create separate namespace for Monitoring

it’s always good idea to keep related things together, We will create separate namespace in Kubernetes for monitoring and will deploy all monitoring related application under that namespace.

namespace.yml
1
2
3
4
kind: Namespace
apiVersion: v1
metadata:
name: monitoring

Helm Chart

Helm uses a packaging format called charts. A chart is a collection of files that describe a related set of Kubernetes resources. A single chart might be used to deploy something simple, like a memcached pod, or something complex, like a full web app stack with HTTP servers, databases, caches, and so on.
Charts are created as files laid out in a particular directory tree, then they can be packaged into versioned archives to be deployed.

Step 3: Deploy Prometheus using Helm Chart

With the help of Helm, We can deploy prometheus using single command.

1
helm install prometheus stable/prometheus --namespace monitoring

This will deploy Prometheus into your cluster in the monitoring namespace and mark the release with the name prometheus.

let’s check if prometheus is running or not

1
kubectl get pods -n monitoring 

Step 4: Deploy Grafana using Helm Chart

In Previous post we have manually created the data sources. Here we can create the config map for Prometheus data source and grafana deployment can use these config maps.

After Grafana Helm chart deployment, it looks for any config maps that contain a grafana_datasource label.

config.yml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
apiVersion: v1
kind: ConfigMap
metadata:
name: prometheus-grafana-datasource
namespace: monitoring
labels:
grafana_datasource: '1'
data:
datasource.yaml: |-
apiVersion: 1
datasources:
- name: Prometheus
type: prometheus
access: proxy
orgId: 1
url: http://prometheus-server.monitoring.svc.cluster.local
values.yml
1
2
3
4
5
6
sidecar:
image: xuxinkun/k8s-sidecar:0.0.7
imagePullPolicy: IfNotPresent
datasources:
enabled: true
label: grafana_datasource
Config map & Grafana deployment
1
2
kubectl apply -f helm/monitoring/grafana/config.yml 
helm install grafana stable/grafana -f helm/monitoring/grafana/values.yml --namespace monitoring

Password protected Grafana instance will be be deployed, To know the password run the below command.

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

Now let’s do port-forward for accessing grafana
port-forward deployment
1
kubectl --namespace monitoring port-forward grafana-5c6bbf7f4c-n5pqb 3000

Now if you goto http://localhost:3000 grafana interface will be available.

Now lets add JVM chart which wil be using our Prometheus datasource.

The code for this post is available on Github here

Share Comments