In an earlier post we saw how to deploy your Spring application to a Kubernetes cluster. Here we will see how to setup monitoring in your cluster where Kubernetes and Prometheus work together. Follow along to setup your own Prometheus instance!
For more in depth information on Prometheus read the documentation.
Prometheus
Prometheus will be a separate pod in your cluster that will scrape given endpoints for information. In this first section we will get this pod running. All code for this section is found on github. Let’s take a look at the Kubernetes resource definition of Prometheus:
apiVersion: monitoring.coreos.com/v1
kind: Prometheus
metadata:
name: prometheus
labels:
prometheus: sybrenbolandit
spec:
serviceMonitorSelector:
matchLabels:
prometheus: sybrenbolandit
resources:
requests:
memory: 400Mi
Note that the apiVersion and the kind are not part of the standard Kubernetes api. These Custom Resource Definitions (CRDs) can be added by installing the Prometheus operator. Here is a diagram with the architecture.

Here we use helm to install the Prometheus Operator (to get started with helm start here).
helm install coreos/prometheus-operator --name prometheus-operator --namespace sybrenbolandit
Now we can deploy the Prometheus pod with its required config. Get the code from github and run the following command:
cd deployment | kustomize build overlays/test | kubectl apply --record -f -
Check the pods in the sybrenbolandit namespace and you see these:

To get to the Prometheus UI you can port-forward the pod like this:
kubectl -n sybrenbolandit port-forward prometheus-prometheus-0 9090:9090
Then browse to http://localhost:9090 and you will see an empty screen. At the end of this post there will be data.

Actuator endpoints
Prometheus can scrape a Spring Boot Actuator endpoint to collect data from a Spring Boot application. In this section we will build from an application used in an earlier post and add the required configuration.
The full code can be found on this branch. Here are the steps I took.
First of all we need to expose the Spring Boot Actuator endpoints. We do this on a different port than the service endpoints. So our service will look something like the following where we have multiple ports.
apiVersion: v1
kind: Service
metadata:
name: java-spring-api
spec:
ports:
- name: web
port: 8080
- name: actuator
port: 8081
In our application we set the following property so for the actuator port.
management.server.port=8081
To get this in our pod we use the configMapGenerator of Kustomize. So we add an application.properties per environment with this property and a kustomization.yaml with this:
namespace: sybrenbolandit
commonLabels:
env: test
bases:
- ../../base
patchesStrategicMerge:
- autoscaling.yaml
- ingress.yaml
configMapGenerator:
- files:
- application.properties
name: application-config
If we push this and run the Jenkins pipeline that we made earlier our pod has the actuator endpoint exposed on port 8081.
Prometheus endpoint
We can now add an Actuator endpoint that Prometheus can scrape. First add the following dependencies.
<dependency>
<groupId>io.micrometer</groupId>
<artifactId>micrometer-core</artifactId>
<version>${micrometer.version}</version>
</dependency>
<dependency>
<groupId>io.micrometer</groupId>
<artifactId>micrometer-registry-prometheus</artifactId>
<version>${micrometer.version}</version>
</dependency>
We also extend our application.properties to include the following.
management.server.port=8081
management.server.address=0.0.0.0
management.endpoints.web.exposure.include=shutdown,health,prometheus
management.endpoint.shutdown.enabled=true
management.metrics.enable.jvm=true
management.endpoint.metrics.enabled=true
management.endpoint.prometheus.enabled=true
management.metrics.export.prometheus.enabled=true
The last thing we need is a ServiceMonitor, a CRD that specifies how groups of services should be monitored (Prometheus scrape config will be automatically generated based on this definition). Please look at the diagram above to see how it fits in the architecture. Here is our definition.
apiVersion: monitoring.coreos.com/v1
kind: ServiceMonitor
metadata:
name: java-spring-api
labels:
prometheus: sybrenbolandit
spec:
selector:
matchLabels:
app: java-spring-api
endpoints:
- port: actuator
path: /actuator/prometheus
targetLabels:
- env
- app
After deploying this application with the Jenkins pipeline we made in an earlier post, the prometheus endpoint is available and Prometheus scrapes this to get information about this pod. When we look at the Prometheus UI we can now query this information and show it in a graph.

Hopefully you can now get your own Prometheus up and running to monitory your Kubernetes pods. Happy monitoring!
You should take part in a contest for one of the best sites
on the web. I’m going to highly recommend this website!
Hey! Do you use Twitter? I’d like to follow you if that would be okay.
I’m definitely enjoying your blog and look forward to new
posts.
you’re in point of fact a excellent webmaster. The website loading pace is amazing.
It seems that you are doing any distinctive trick.
Moreover, The contents are masterpiece. you have done a fantastic task in this matter!
Nice post. I was checking constantly this blog and I’m impressed!
Very helpful information particularly the last part 🙂 I care for such info much.
I was seeking this certain info for a long time.
Thank you and good luck.
Hello everyone, it’s my first pay a quick visit at this web site, and piece of writing is
genuinely fruitful in support of me, keep up posting these types of content.
Very soon this website will be famous amid all blogging
viewers, due to it’s fastidious articles
Like!! Great article post.Really thank you! Really Cool.
I’m no longer certain where you are getting your
info, but great topic. I needs to spend a while learning more or figuring out more.
Thank you for wonderful information I used to be in search of this information for
my mission.
There’s definately a great deal to learn about this issue.
I really like all of the points you have made.
Excellent way of explaining, and good piece of writing to get data about
my presentation subject matter, which i am going
to deliver in college.
Unquestionably believe that which you stated.
Your favorite justification appeared to be on the web the easiest thing to be aware
of. I say to you, I definitely get annoyed while people think about worries
that they just don’t know about. You managed to hit the nail
upon the top and defined out the whole thing without having side-effects ,
people can take a signal. Will probably be back to get more.
Thanks
Greetings! Very useful advice in this particular article!
It is the little changes which will make the most
important changes. Thanks for sharing!
Hi, just wanted to mention, I loved this blog post.
It was inspiring. Keep on posting!
Why visitors still use to read news papers when in this technological globe all is presented on web?