As we saw in our previous post, Docker is a great option for us to quickly spin up an instance of Elasticsearch. Let’s see in this post how to run another container with Kibana and connect it to our Elasticsearch container.
Pull the Kibana image
First, we will pull the image from the Elastic repository. As we mentioned in our previous post, pay attention to the version you’re downloading, make sure you use the same version for all the components in your ELK stack. In this tutorial we’ll be using version 8.15.3, the latest available at the time of writing this post and the version we use for Elasticsearch:docker pull docker.elastic.co/kibana/kibana:8.15.3
Run the container
Next, we will run the containerdocker run -d --name kibana -p 5601:5601 \
--net elastic \
-e "SERVER_PORT=5601" \
-e "SERVER_HOST=0.0.0.0" \
-e "ELASTICSEARCH_HOSTS=http://elasticsearch:9200" \
docker.elastic.co/kibana/kibana:8.15.3
-d
- to run the container in the background--name kibana
- Provides a name for our container-p 5601:5601
- maps the port 5601 in the host to the port 5601 in the container. This the default port where Kibana is listening for requests--net elastic
- Adds the container to the elastic network we created in Part I. Remember that we need a docker network so that both containers (elasticsearch and kibana) can communicate using the container names as dns names.- Environment variables for custom configuration:
-e SERVER_PORT=5601
- Specifies the HTTP port-e ELASTICSEARCH_HOSTS=http://localhost:9200
- Specifies the URL of the Elasticsearch node that Kibana should connect to. In our case, our elasticsearch instance is running on another container of the same docker network, so we’ll put here the name of the elasticsearch container as hostname. In our example, we set up elasticsearch on http (change it to https if yours is different). Also, make sure about the port your elasticsearch container is listening to.-e SERVER_HOST="0.0.0.0"
- Binds Kibana to a specific network interface. With 0.0.0.0 we’ll bind it to all interfaces in the container
docker ps
docker logs [YOUR_KIBANA_CONTAINER]
Let’s see what we need to do to set it up and connect it to our local Elasticsearch container
Connecting Kibana to Elasticsearch
There are two ways for the initial connection between Kibana and Elasticsearch. Using the UI or setting up the kibana config fileOption 1: Using the UI
NOTE: This option will only work if your Elasticsearch instance has been set up with the default configuration:- Port 9200
- Security pack enabled (authentication required)
- HTTPS enabled
Head over to your web browser and go to http://localhost:5601.
The enrollment token is provided by the elasticsearch instance.
For that we’ll have to run the following command from the elasticsearch container:
For that we’ll have to run the following command from the elasticsearch container:
docker exec -it elasticsearch /usr/share/elasticsearch/bin/elasticsearch-create-enrollment-token -s kibana
Copy the enrollment token and paste it in the Kibana page. You will then be asked for a Verification code
The verification code should be in the Kibana logs. Type the following:
docker logs [YOUR_KIBANA_CONTAINER]
It that’s correct, your setup will start
Once it’s done, provide username and password to access Kibana
Now you should see the Kibana UI
Option 2: Modifying the kibana config file
The other option to connect Kibana to Elasticsearch is modifying the kibana.yml file located at /usr/share/kibana/config/kibana.yml.As we saw on How to install Elasticsearch on Docker we can’t edit the file from within the container. There are two options to modify that file:
- Using Docker Desktop
- From the terminal
The settings we need to add to the kibana config file are the connection parameters for the Elasticsearch instance. We will need to add values for these two parameters:
elasticsearch.hosts: [ "" ]
elasticsearch.serviceAccountToken:
localhost
because Kibana is running on a different container. That’s why we created the elastic docker network. Connecting both containers (Elasticsearch and Kibana) to this network allows the containers to communicate to each other using the container’s name as dns name. This means, that we can use the elasticsearch container name as the hostname.Set the value of the elasticsearch host in the kibana.yml file:
elasticsearch.hosts: [ "http://[YOUR_ELASTICSEARCH_CONTAINER]:9200" ]
For the Service Account Token, we need to get it using the Elasticsearch REST API. We’ll use the /_security/enroll/kibana endpoint.
With CURL:
With Postman:
The response will be a JSON with the name and the value of the token we’ll use to authenticate Kibana.
Copy the token and set the value in the kibana.yml file:
Copy the token and set the value in the kibana.yml file:
elasticsearch.serviceAccountToken: "[YOUR_SERVICE_ACCOUNT_TOKEN]"
Lastly, save the file and restart the Kibana container. Once Kibana has started, open your web browser and go to http://localhost:5601. You should now see the login page of Kibana
Next Steps
With all of the above you can get your Kibana + Elasticsearch containers for testing in minutes. From here, depending on what you need your testing for, the next steps could be:- Install Logstash on Docker
- Set up ELK for our Mule Apps