Have you ever tried to find a bug by digging through a pile of text log files? Imagine there are a couple dozen of those files, all scattered across different servers. To make situations like that easier, the ELK stack was invented. ELK is a trio of tools: Elasticsearch, Logstash and Kibana, which help collect, store, analyze, and visualize logs.
In a microservices architecture each service generates its own logs. If an error happens, you'll want to quickly and easily find where it occurred, inspect the chain of events, and understand why things went wrong. Centralized logging lets you:
- Collect logs from all microservices in one place.
- Analyze logs conveniently using queries (instead of "manual eyeballing").
- Quickly find interactions between services and figure out the root cause of issues.
- Visualize data for better understanding.
What is the ELK stack?
Let's break down each of the three letters in ELK:
- Elasticsearch — a data store and search engine. It finds the logs you need across massive amounts of data in fractions of a second. It also supports queries based on JSON.
- Logstash — a data processor. It collects logs from various sources, filters or transforms them, and sends them to Elasticsearch. You can think of it as the "courier" for logs.
- Kibana — a visualization tool. It pulls data from Elasticsearch and draws charts, graphs, and tables so you can understand what's going on without pain.
When these three tools work together, they turn boring text logs into a powerful analysis toolkit.
Installation and configuration of ELK
Elasticsearch
Let's start by installing Elasticsearch, which will act as our log storage.
- Download and install Elasticsearch.
- After installation open the config file
elasticsearch.yml, which you can find in theconfigfolder. - Make sure Elasticsearch is reachable locally:
network.host: 127.0.0.1 http.port: 9200 - Start Elasticsearch:
After startup the service will be available at http://localhost:9200.
./bin/elasticsearch
Logstash
Now let's set up the second component — Logstash. It's the bridge that will accept logs from our Spring Boot application and send them to Elasticsearch.
- Download and install Logstash.
- Create a configuration file
logstash.conf:input { beats { port => 5044 } } output { elasticsearch { hosts => ["localhost:9200"] } }- In the input section we specify that we want to accept logs via port
5044. - In the output we configure Logstash to send data to Elasticsearch.
- In the input section we specify that we want to accept logs via port
- Start Logstash:
./bin/logstash -f logstash.conf
Kibana
The final step is installing and running Kibana so we can visualize the data.
- Download and install Kibana.
- Configure Kibana by editing the file
kibana.yml:server.port: 5601 server.host: "localhost" elasticsearch.hosts: ["http://localhost:9200"] - Start Kibana:
Now the Kibana interface is available at http://localhost:5601.
./bin/kibana
So, the main components are installed. Now we can integrate our Spring Boot application with ELK.
Integrating microservices with ELK
Configuring logging in Spring Boot
It's time to make your app send logs directly to Logstash.
- Open
pom.xmland add the following dependencies:<dependency> <groupId>net.logstash.logback</groupId> <artifactId>logstash-logback-encoder</artifactId> <version>7.4</version> </dependency> - Configure
logback-spring.xmlto send logs to Logstash:<configuration> <appender name="LOGSTASH" class="net.logstash.logback.appender.LogstashTcpSocketAppender"> <destination>127.0.0.1:5044</destination> <encoder class="net.logstash.logback.encoder.LogstashEncoder" /> </appender> <root level="INFO"> <appender-ref ref="LOGSTASH" /> </root> </configuration> Restart the application. Now logs will be automatically sent to Logstash, and from there — to Elasticsearch.
Checking logs
- Open the Kibana interface (http://localhost:5601).
- Go to the Discover section.
- Set up the index (for example,
logstash-*) and you'll see logs from your application.
Practical ELK setup for microservices
Let's imagine we have two microservices:
- User Service — responsible for managing user data.
- Order Service — responsible for processing orders.
Steps:
- Add a Logstash configuration to each service, like shown earlier.
- Configure both services to send logs to Logstash.
- Use Kibana to analyze logs:
- Find requests that go through both services.
- If requests are correlated (for example, a Trace ID from Sleuth), follow their path.
What to keep in mind?
Logging is a powerful tool, but it comes with some challenges:
- Resource usage. Generating and processing a large volume of logs can slow the system down.
- Security. Make sure sensitive data (like passwords) doesn't end up in the logs.
- Storage. Elasticsearch has limits on data volume, so old logs should be archived or deleted.
Now that your microservices are equipped with centralized logging via ELK, you can sleep a lot easier. After all, diagnosing problems will become a matter of seconds, and visualizing logs in Kibana will definitely impress your team! (Or at least your tech lead.)
GO TO FULL VERSION