Join us

Springboot + ELK + Docker

1_5t47-htXKPhcxeVAG7oPow.png

Example of implementing an API using Spring Boot, Elasticsearch, Logstash, Kibana, and Docker.

In this article, we will cover an example of an application that uses Springboot + ELK + Docker. It is a simple application, only intended to demonstrate the concepts. I won’t go into a detailed explanation of all the elements (if you’re not familiar with this stack, I suggest doing a little research before doing this tutorial).

Pre-requirements

  • You must have docker installed on your machine.
  • An IDE of your choice (Intellij, Eclipse, or Vscode)
  • Some knowledge of Springboot, Elasticsearch, Logstash, Kibana, and Docker

Hands-On

Springboot Application

I’ll start with the application; I created a REST API with 4 endpoints: This is how the application’s Dockerfile was:

Alternatively, you can use a Dockerfile like this:

As we are going to use several containers, we are going to use docker compose. At the root of your project, you will create the docker-compose.yml file. For now, docker-compose.yml will look like this.

For now, docker-compose.yml will look like this.

To test, run from the root of the project: docker-compose up

Test by calling the endpoints via Postman, browser, or others of your choice.

Elasticsearch

I won’t go into details about Elasticsearch (I suggest you look into it if you don’t know about it).

Here we are setting Elasticsearch settings like port, memory variables, base directory in docker, etc. We also created a network called elk and added our service to it. To test, run: docker-compose up, open this URL http://localhost:9200/, and you will have a similar result to this:

This shows us that Elasticsearch is working correctly.

Logstash

For Logstash, the process is a little different: first let’s create a folder called .logstash, to store some settings. Inside it, we will create the logstash.conf file, which will have the following information:

Here we will have the operating mode, which can be TCP or per file. In TCP mode, logstash will get real-time data from the port specified in the logback-spring.xml file, inside the project in the resources package.

logback-spring.xml file:

It also contains the output which, in our case, will be for Elasticsearch, as described below. In the output information, we define the destination, elastic user and password (in this case it is with the default values), and index. The index will serve to filter the information only from this application in kibana. In docker-compose.yml, the container will look like this:

Kibana

Kibana has its simple configuration, just add to docker-compose.yml:

After that, it is now possible to access kibana through the browser at URL: http://localhost:5601/. When using kibana, you will need to add the index we created earlier in logstash to get the information. To do this, access the kibana and enter: http://localhost:5601/app/management/kibana/indexPatterns Right menu/ Stack Management/ Index Patterns/ Create index pattern. As an example, the application we cited as an example was: library-logstash-*.

Code available at: https://github.com/jefsterjr/library/tree/main

Originally published at http://github.com.


Only registered users can post comments. Please, login or signup.

Start blogging about your favorite technologies, reach more readers and earn rewards!

Join other developers and claim your FAUN account now!

Avatar

Jefster Farlei Fernandes Caixeta Júnior

Software Engineer, Midway

@jefsterjr
Passionate about technology and knowledge of all areas, always keeping me informed of the world around me.
User Popularity
46

Influence

4k

Total Hits

1

Posts