1. Introduction
Elasticsearch is the distributed search and analytics engine at the heart of the Elastic Stack. If you want to try Elasticsearch without installing it, there is a Cloud offer. In this tutorial, we assume that you want to install a self-managed instance of Elasticsearch locally. Elastic offers different binary types depending on your Operating System. We won’t be using any of that in this post, as we will run our Elasticsearch instance in a Docker Container.
2. Prerequisites
You are expected to have basic knowledge of Docker. This tutorial uses Docker Desktop for Windows, but you can use any other Docker installation depending on your Operating System. Moving forward, we will assume that you have a working installation of Docker in your workstation.
3. Single-node Cluster
We will start with a single-node Elasticsearch cluster. Later, we will add more nodes to the cluster.
3.1. Create the Network
This step is not mandatory if you’re working in a single instance container. However, since we are going to add more nodes to this cluster, plus Elasticsearch will be communicating with the other services of the ELK stack(Logstash, Kibana), let’s start by creating a network interface.
From a command prompt(or terminal), enter the following command:
>docker network create elk
With this command, we create a network named “elk”.
Your output will be similar to this:
759d536b180c54486af7414d1f9f7017f9df405ef41c991db5e3f011c20cd5ab
3.2. Pull the Elasticsearch Docker image
You can find the list of Docker images for Elasticsearch in the Docker Elastic Registry. For this tutorial, we will be installing the version 8.14.3.
Open a command prompt (or a terminal) and run the following command:
>docker pull docker.elastic.co/elasticsearch/elasticsearch:8.14.3
After the download is complete, you’ll have an output similar to this:
3.3. Create a Configuration file: elasticsearch.yml
Elasticsearch requires a configuration file in YAML format. Let’s create the file and save it under “C:\apps\elasticsearch\config”, with the following content:
cluster.name: "docker-cluster"
network.host: 0.0.0.0
3.4. Start the Container
Once the image is pulled, and the configuration file is created, use the following command to start the elastic container:
>docker run --name es01 -h elasticsearch --net elk -p 9200:9200 -it -m 1GB -v C:/apps/elasticsearch/config/elasticsearch.yml:/usr/share/elasticsearch/config/elasticsearch.yml docker.elastic.co/elasticsearch/elasticsearch:8.14.3
- es01 is the name we are giving to the container
-h elasticsearch
is the hostname of the docker container--net elk
: to specify the network to which the container is attached-m 1GB
: to limit the memory size-p 9200:9200
: to specify the port mapping. Elasticsearch will be available on port 9200 within the container and outside the container.-v ...
: to bind a directory in the host machine to a directory in the container. We use it to pass the configuration file from our host machine to the container.
When you execute this command, Elasticsearch will print on the screen the password for your Elasticsearch backend and an enrollment token for Kibana. Copy and save them as they only appear the first time Elasticsearch starts.
3.5. Verify the installation
To test your setup, you can make a REST API Call to Elasticsearch. You can use a command line tool like Curl, or a tool like Postman. We will be using Curl here.
Open a command prompt (or a terminal) and enter the following command:
C:\apps\elasticsearch>curl -k -u elastic:%ELASTIC_PASSWORD% https://localhost:9200
- For security reasons, we have saved the Elasticsearch password as an environment variable ELASTIC_PASSWORD
- We also added the
-k
(for insecure) option to avoid the CERT_TRUST_REVOCATION_STATUS_UNKNOWN error on Windows. You don’t need it if you’re running Curl from a different Operating System.
If the command is successful, you’ll get the following output (or similar):
{
"name" : "elasticsearch",
"cluster_name" : "docker-cluster",
"cluster_uuid" : "UIdL-JY_QR6kHpg4IpTUbg",
"version" : {
"number" : "8.14.3",
"build_flavor" : "default",
"build_type" : "docker",
"build_hash" : "d55f984299e0e88dee72ebd8255f7ff130859ad0",
"build_date" : "2024-07-07T22:04:49.882652950Z",
"build_snapshot" : false,
"lucene_version" : "9.10.0",
"minimum_wire_compatibility_version" : "7.17.0",
"minimum_index_compatibility_version" : "7.0.0"
},
"tagline" : "You Know, for Search"
}
4. Multi-node Cluster
The objective of this section is to add a new node to the previously created cluster.
4.1. Generate an enrollment token
Before adding new nodes to the cluster, you must generate an enrollment token.
>docker exec -it es01 /usr/share/elasticsearch/bin/elasticsearch-create-enrollment-token -s node
The token will be printed to the console and is valid for 30 minutes.
4.2. Create an environment variable for the enrollment token
For security reasons, we will save the previously generated token in an environment variable named ELASTIC_ENROLLMENT_TOKEN.
4.3. Start a new node
C:\apps\elasticsearch>docker run -e ENROLLMENT_TOKEN=%ELASTIC_ENROLLMENT_TOKEN% --name es02 --net elk -it -m 1GB docker.elastic.co/elasticsearch/elasticsearch:8.14.3
The new node is named “es02” and is attached to the same network (elk) as the first node “es01”.
After some time, a new container will be added to the cluster. You’ll have a similar output to this:
4.4. Verify the installation
To test your installation, you can call the Cat nodes API to list the nodes of your cluster.
Using a command prompt, issue the following curl command:
C:\apps\elasticsearch>curl -k -u elastic:%ELASTIC_PASSWORD% https://localhost:9200/_cat/nodes
The command will display all the nodes within the cluster.
172.18.0.2 67 92 0 0.05 0.12 0.08 cdfhilmrstw * elasticsearch
172.18.0.3 51 94 2 0.05 0.12 0.08 cdfhilmrstw - 4065eefef26c
As you can see, there are 2 nodes available in the cluster.
5. Reset Elasticsearch Password
When you start Elasticsearch for the first time, a password is generated and printed on the screen. You can connect to Elasticsearch with the user “elastic” and the given password. If you have forgotten your Elasticsearch password or if for some reason you need to regenerate it, you can use the following command:
>docker exec -it es01 /usr/share/elasticsearch/bin/elasticsearch-reset-password -u elastic
Assuming that Elasticsearch is installed in the container name “es01”, this will reset the password of the user “elastic”.
6. Conclusion
In this tutorial, you learned how to install Elasticsearch using Docker. You should be aware that the setup we did in this tutorial is only for a testing environment and is not suitable for production. Kindly have a look at these recommendations if you plan to run Elasticsearch in production with Docker.
Pingback: How to Install Filebeat Using Docker
Pingback: How to Install Metricbeat Using Docker
Pingback: How to Install Kibana Using Docker
Pingback: How to Install OpenTelemetry Collector Using Docker