Article directory
1 Introduction
Elasticsearch is an open source distributed search and analysis engine that uses the Lucene library to implement full-text search capabilities. This article will introduce how to use Docker to deploy and install Elasticsearch on a single machine.
2 Docker installation and configuration
2.1 Install Docker
First, you need to install Docker engine. You can follow the instructions in the official documentation to complete the installation process. Make sure your operating system meets Docker's minimum hardware requirements.
2.2 Configure Docker image accelerator
To speed up the download of Docker images, you can configure the Docker image accelerator. Here we take Alibaba Cloud's image accelerator as an example.
Execute the following commands in the terminal to configure:
$ sudo vi /etc/docker/daemon.json
Add the following content to the opened file:
{
"registry-mirrors": ["https://your-registry-mirror-url"]
}
Replace your-registry-mirror-url
with the address of the image accelerator you are using.
After saving and closing the file, restart the Docker service:
$ sudo systemctl restart docker
2.3 Adjust Docker resource limits
Depending on your needs, you may need to adjust Docker's resource limits, such as memory and CPU. You can do this by modifying the Docker startup configuration file.
Edit the Docker configuration file:
$ sudo vi /etc/docker/daemon.json
Add the following to set resource limits:
{
"default-cgroup-parent": "your-cgroup-parent-path"
}
Replace your-cgroup-parent-path
it with your custom cgroup path.
After saving and closing the file, restart the Docker service:
$ sudo systemctl restart docker
3 Prepare the Elasticsearch Docker image
3.1 Download the Elasticsearch image
Next, we need to download the official image of Elasticsearch from Docker Hub. Execute the following command to pull the latest version:
$ docker pull elasticsearch:8.5.0
3.2 Custom image configuration
You can also customize the configuration of the Elasticsearch image as needed. Here we take Docker Compose as an example, create a docker-compose.yml
file and add the following configuration:
version: '3'
services:
elasticsearch:
image: elasticsearch:8.5.0
privileged: true
environment:
- discovery.type=single-node
- "ES_JAVA_OPTS=-Xms512m -Xmx512m"
ports:
- 9200:9200
- 9300:9300
By setting privileged to true, the container will obtain the privileged permissions of the host and can perform privileged operations.
This configuration will create an Elasticsearch container using single-node mode and map ports 9200 and 9300 to the corresponding ports on the host.
3.3 Execute Docker Compose
docker-compose up -d #这是后台执行的命令
4. Run the Elasticsearch container
4.1 Create an Elasticsearch container
Run the Elasticsearch container using Docker Compose:
$ docker-compose up -d
4.2 Modify configuration file
Because the default network mode of the configuration file in the image and a security authentication configuration need to be removed.
cluster.name: "docker-cluster"
http.host: 0.0.0.0
#----------------------- BEGIN SECURITY AUTO CONFIGURATION -----------------------
#
# The following settings, TLS certificates, and keys have been automatically
# generated to configure Elasticsearch security features on 05-09-2023 07:49:11
#
# --------------------------------------------------------------------------------
# Enable security features
xpack.security.enabled: false
xpack.security.enrollment.enabled: true
# Enable encryption for HTTP API client connections, such as Kibana, Logstash, and Agents
xpack.security.http.ssl:
enabled: true
keystore.path: certs/http.p12
# Enable encryption and mutual authentication between cluster nodes
xpack.security.transport.ssl:
enabled: true
verification_mode: certificate
keystore.path: certs/transport.p12
truststore.path: certs/transport.p12
#----------------------- END SECURITY AUTO CONFIGURATION -------------------------
4.3 Verify Elasticsearch running status
After waiting for a period of time, you can verify the running status of Elasticsearch through the curl command or browser access. Here is an example:
$ curl http://localhost:9200
5 FAQ
Here are some common problems and their solutions:
-
Issue 1: Unable to connect to Elasticsearch container.
Workaround: Make sure the Elasticsearch container is running and the Docker network is configured correctly. -
Issue 2: Insufficient memory when starting the Elasticsearch container.
Solution: Adjust Docker resource limits and increase available memory. -
Problem 3: Unable to pull the Elasticsearch image.
Solution: Check whether the Docker image accelerator configuration is correct and ensure that the network connection is smooth.
6 Summary
This article describes how to use Docker to deploy and install Elasticsearch on a single machine. By using Docker, we can quickly build development and testing environments and effectively manage Elasticsearch dependencies and configurations.