How to Set Up Elasticsearch and Kibana Using Docker on Linux VPS

Elasticsearch, a distributed search and analytics engine, powers scenarios like log management, full-text search, and data analysis. It excels with fast indexing, distributed architecture, and multi-tenancy support. Paired with Kibana, its data visualization tool, Elasticsearch allows seamless exploration and visualization of data through dashboards and query interfaces. This guide outlines setting up an Elasticsearch cluster using Docker on a Linux VPS or physical machine.
Prerequisites:

System: Linux (Ubuntu 22/24 recommended)
Tools: Docker installed (apt update && apt install docker.io -y)

Step 1: Create a Docker Network

Run the command:
docker network create elastic

Step 2: Setup Data Persistence

Create and configure a local directory for data:
mkdir -p ~/elasticsearch/data
chmod 776 ~/elasticsearch/data -R

Step 3: Install Elasticsearch

Pull the latest Elasticsearch image:
docker pull docker.elastic.co/elasticsearch/elasticsearch:8.15.4
Run Elasticsearch with data persistence:
docker run -d --name es01 --net elastic -p 9200:9200 -m 2GB -v ~/elasticsearch/data:/usr/share/elasticsearch/data docker.elastic.co/elasticsearch/elasticsearch:8.15.4

Step 4: Configure Elasticsearch

Wait ~5 minutes for the container to stabilize, then reset the password:
docker exec -it es01 /usr/share/elasticsearch/bin/elasticsearch-reset-password -u elastic
Obtain the enrollment token for Kibana:
docker exec -it es01 /usr/share/elasticsearch/bin/elasticsearch-create-enrollment-token -s kibana
Enrollment Token: eyJ2ZXIiOiI4LjE0LjAiLCJhZHIiOlsiMTcyLjE5LjAuMjo5MjAwIl0sImZnciI6IjhiYzJhZmQ4ODhmNjg3NjE1YTA5NDQ1NjgzMzcwY2RiYzkxZGU0ZTEwMzQ4YmQyZWVlMTFlOTQ1ODAwOGZkZDQiLCJrZXkiOiJocGlaVXBNQm94LUZNa3RsaFplTTpJdW5MRTBySFFRR1BUZWwyQlprNUFnIn0=
Copy the SSL certificate locally:
docker cp es01:/usr/share/elasticsearch/config/certs/http_ca.crt .

Step 5: Verify Installation

Make a REST API call to Elasticsearch to ensure the Elasticsearch container is running. Check Elasticsearch status with:
curl --cacert http_ca.crt -u elastic:YOUR_PASSWORD https://localhost:9200
elastic password

Step 6: Install Kibana

Pull and run Kibana:
docker pull docker.elastic.co/kibana/kibana:8.15.4
docker run -d --name kib01 --net elastic -p 5601:5601 docker.elastic.co/kibana/kibana:8.15.4
Access Kibana via http://:5601 and complete the setup using the enrollment token and generated verification code.
elastic
Enter the Enrollment token generated previously:
enrollment token
Get verification code:
# docker exec -it kib01 /usr/share/kibana/bin/kibana-verification-code
Enter the verification code:
verification code
During configuration:
During configuration
Enter your username and password:
username and password
Log in to Kibana successfully:
Log in to Kibana successfully

Key Notes:

1. Ensure that Elasticsearch data persists even after Docker container restarts.
2. Access and test the setup from a web browser using your VPS or machine's public IP.
3. This configuration supports application-level logging by connecting developers' applications to Elasticsearch.
For more advanced setups, explore Elasticsearch security and scaling options. Happy deploying!