(Elastic monitoring - 5/5) Collect traces with Elastic APM for monitoring Kubernetes
Elastic APM is an application performance monitoring system built on the Elastic Stack. It allows you to monitor software services and applications in real time — collect detailed performance information on response time for incoming requests, database queries, calls to caches, external HTTP requests, and more. This makes it easy to pinpoint and fix performance problems quickly.
Elastic APM is OpenTracing compliant which means you can take advantages of the large range of libraries already available to trace components within your application (e.g MongoDB instrumentation).
For example, you will be able to follow a request in a highly distributed environment (micro-service architecture) and find potential bottleneck easily and quickly.
Elastic APM is composed of a component called APM-Server used to collect and ship traces to ElasticSearch and individual agents running with the application or service.
Install APM-Server
We first need to install APM-Server on k8s to collect the traces for the agents and forward them to ElasticSeach.
It’s composed of a ConfigMap
to configure the settings:
1 | ## apm.configmap.yml |
APM-Server needs to expose the port 8200
to allow the agent to forward their traces. The following Service
exposes this port to the environment:
1 | ## apm.service.yml |
The last bit is the Deployment
describing the container to be deployed:
1 | ## apm.deployment.yml |
See the full file
We can now deploy this new component of our stack:
1 | kubectl apply -f apm.deployment.yml \ |
Check that everything is up and running:
1 | kubectl get all -n monitoring -l app=apm-server |
We now can install an agent on our Spring-Boot app.
Configure a Java agent on the application
In the last part of this article, we will configure a Elastic APM Java agent on the sample application spring-boot-simple
.
First we need to put the jar elastic-apm-agent-1.8jar in the container. Add the following line to download the agent JAR when docker builds the image.
1 | RUN wget -O /apm-agent.jar https://search.maven.org/remotecontent?filepath=co/elastic/apm/elastic-apm-agent/1.8.0/elastic-apm-agent-1.8.0.jar |
1 | FROM openjdk:8-jdk-alpine |
Secondly add the following dependencies to your application, so your will be able to integrate open-tracing libraries (read more) and/or manually instrument some components with the Elastic APM API (read more).
1 | <dependency> |
Then we will change the Deployment
to start the Spring-Boot application with the Java agent enabled and connected to the APM-server.
1 | ## spring-boot-simple.deployment.yml |
Now reapply the Deployment
and spring-boot-simple should restart:
1 | kubectl apply -f spring-boot-simple.yml |
Execute a few calls such as:
get messages
Command to retrieve all the messages posted.
1 | curl -X GET http://10.154.0.2:30049/message |
get messages (slow request)
Use the attribute sleep=<ms>
to slow down the request.
1 | curl -X GET http://10.154.0.2:30049/message?sleep=3000 |
get messages (error)
Use the attribute error=true
to raised an exception during the execution.
1 | curl -X GET http://10.154.0.2:30049/message?error=true |
Now go to Kibana in the section “APM” and you should see the application spring-boot-simple
, click on it.
Detect recurrent errors:
Visualize applications metrics (e.g. HeapSize, GC)
Summary
Hopefully, this series of articles helped you understand how to deploy a monitoring on your Kubernetes environment with a minimal impact and a lots of perspectives to observe, track, prevent, alert and speed up the resolution of production issues.