Monitor Spring Boot Apps using Prometheus on Kubernetes

This posts show how to monitor Spring Boot apps using Prometheus when running on Kubernetes. You should already have a running Kubernetes cluster.

Install Helm

I’m going to use Prometheus Operator to create and manage Prometheus on Kubernetes. Prometheus Operator is installed using Helm, so Helm needs to be installed first. On macOS, simply use Homebrew.

1
$ brew install kubernetes-helm

Then you need to initialize Helm.

1
$ helm init

Install Prometheus Operator

Before installing Prometheus Operator, you need to make sure the current account has the permission to manage CustomResourceDefinitions. A simple way is to assign the role cluster-admin to current user, see here. If you are using the default system service account, you can use the following command to create the role binding. Replace system:serviceaccount:kube-system:default with the name of your own account.

1
$ kubectl create clusterrolebinding cluster-admin-binding --clusterrole=cluster-admin --user=system:serviceaccount:kube-system:default

Now you can install Prometheus Operator using Helm.

1
$ helm install stable/prometheus-operator --name prometheus-operator

After installation, you can use the following command to access the Prometheus web UI on http://localhost:9090.

1
$ kubectl port-forward  prometheus-prometheus-operator-prometheus-0 9090

Create ServiceMonitor

By default, Prometheus Operator monitors various targets of the Kubernetes cluster. To add your own targets, you need to create ServiceMonitors.

You need to have a Service for your Spring Boot application first.

The yaml file below creates a new ServiceMonitor that matches the label app with name my-service. The endpoint is for Spring Boot Actuator with the path /actuator/prometheus. Use kubectl create to create this ServiceMonitor.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
apiVersion: monitoring.coreos.com/v1
kind: ServiceMonitor
metadata:
name: my-service
labels:
release: prometheus-operator
spec:
selector:
matchLabels:
app: my-service
endpoints:
- port: api
path: '/actuator/prometheus'
interval: 10s
honorLabels: true

Please note, the label release: prometheus-operator is important for the ServiceMonitor to work. If you run kubectl get prometheus -o yaml, you can see the following output. This means Prometheus Operator only selects ServiceMonitors with the label release: prometheus-operator.

1
2
3
serviceMonitorSelector:
matchLabels:
release: prometheus-operator

Now you should see the target for the Spring Boot app in the list of http://localhost:9090/targets.

Comments