- Installation Methods
Elasticsearch can be installed using several methods. This guide covers tarball, RPM, and Docker approaches.
1.1 Tarball Installation
Create a setup script to automate the installation process.
#!/bin/bash
# setup_es.sh
ES_INSTALL_DIR="/opt/elasticsearch"
ES_VERSION="7.9.2"
# Create installation directory
mkdir -p ${ES_INSTALL_DIR}
# Download the Elasticsearch tarball
wget https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-${ES_VERSION}-linux-x86_64.tar.gz -P ${ES_INSTALL_DIR}
# Extract the archive
cd ${ES_INSTALL_DIR}
tar -xzf elasticsearch-${ES_VERSION}-linux-x86_64.tar.gz
# Create a symbolic link for easier access
ln -s elasticsearch-${ES_VERSION} elasticsearch
1.2 RPM Installation
For RPM-based systems, use the following command to install Elasticsearch directly.
sudo yum install https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-8.4.3-x86_64.rpm
- System Optimization
Optimize the system for Elasticsearch by adjusting file descriptors and memory settings.
# Increase file descriptors
echo "* soft nofile 65535
* hard nofile 65535
* soft nproc 65535
* hard nproc 65535" >> /etc/security/limits.conf
# Increase virtual memory map counts
echo "vm.max_map_count=655360" >> /etc/sysctl.conf
sysctl -p
# Set environment variables
echo "export JAVA_HOME=/opt/elasticsearch/elasticsearch/jdk
export ES_HOME=/opt/elasticsearch/elasticsearch
export PATH=\$ES_HOME/bin:\$ES_HOME/jdk/bin:\$PATH" >> /etc/profile
source /etc/profile
- Starting Elasticsearch
Start the Elasticsearch service as a dedicated user.
# Create a dedicated user
sudo useradd -r elasticsearch_user
# Set ownership
sudo chown -R elasticsearch_user /opt/elasticsearch/elasticsearch
# Switch to the user and start Elasticsearch
sudo su - elasticsearch_user
/opt/elasticsearch/elasticsearch/bin/elasticsearch -d
- Security Configuration
Enable security features and set up passwords for Elasticsearch.
4.1 Configure elasticsearch.yml
Edit the configuration file to enable security and set basic network settings.
node.name: node-1
network.host: 0.0.0.0
cluster.initial_master_nodes: ["node-1"]
http.port: 9200
transport.tcp.port: 9300
http.cors.enabled: true
http.cors.allow-origin: "*"
http.cors.allow-headers: Authorization
xpack.security.enabled: true
xpack.security.transport.ssl.enabled: true
4.2 Set Initial Passwords
Run the interactive password setup script.
sudo /opt/elasticsearch/elasticsearch/bin/elasticsearch-setup-passwords interactive
4.3 Change Password
Update the password for a specific user using the REST API.
curl -H "Content-Type:application/json" -XPOST -u elastic 'http://127.0.0.1:9200/_security/user/elastic/_password' -d '{ "password" : "new_password123" }'
4.4 Verify
Test the connection with the new credentials.
curl -XGET --user elastic:new_password123 'http://127.0.0.1:9200'
- Configuration File Details
Understand the key settings in the elasticsearch.yml file.
cluster.name: my_cluster # Unique cluster identifier
node.name: my_node_01 # Unique node identifier
node.master: true # Eligible to be master node
node.data: true # Store data on this node
path.data: /data/es/data # Data storage path
path.logs: /logs/es/log # Log file path
bootstrap.memory_lock: false # Disable memory swapping
bootstrap.system_call_filter: false # Disable system call filtering
network.host: 192.168.1.100 # Bind to specific IP
http.port: 9200 # HTTP API port
transport.port: 9300 # Cluster communication port
# Discovery and cluster formation
discovery.seed_hosts: ["192.168.1.100:9300", "192.168.1.101:9300"]
cluster.initial_master_nodes: ["192.168.1.100", "192.168.1.101"]
# Security settings
xpack.security.enabled: true
xpack.license.self_generated.type: basic
xpack.security.transport.ssl.enabled: true
xpack.security.transport.ssl.verification_mode: certificate
xpack.security.transport.ssl.keystore.path: elastic-certificates.p12
xpack.security.transport.ssl.truststore.path: elastic-certificates.p12
# CORS settings
http.cors.enabled: true
http.cors.allow-origin: "*"
http.cors.allow-headers: Authorization,X-Requested-With,Content-Length,Content-Type
- Dependency Management
Ensure system dependencies are properly configured, particularly glibc.
# Upgrade glibc to a compatible version
wget http://ftp.gnu.org/gnu/glibc/glibc-2.17.tar.gz
tar -xvf glibc-2.17.tar.gz
cd glibc-2.17
mkdir build; cd build
../configure --prefix=/usr --disable-profile --enable-add-ons --with-headers=/usr/include --with-binutils=/usr/bin
make -j 8
make install
- Docker Installation
Deploy Elasticsearch using Docker for containerized environments.
7.1 Create Dockerfile
Define the Docker image with custom configurations.
FROM docker.io/elasticsearch:7.16.1
LABEL description="Elasticsearch 7.16.1 with custom config"
# Copy custom configuration
COPY elasticsearch.yml /usr/share/elasticsearch/config/elasticsearch.yml
# Set timezone
RUN ln -snf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime && echo "Asia/Shanghai" > /etc/timezone
7.2 Build and Run Container
Build the Docker image and start the container with port mappings.
# Build the Docker image
docker build -t my-elasticsearch:7.16.1 -f /path/to/Dockerfile .
# Run the container
docker run -d --name elasticsearch-container -p 9200:9200 -p 9300:9300 my-elasticsearch:7.16.1
7.3 Set Passwords in Docker
Configure passswords for the Docker container.
# Access the container
docker exec -it elasticsearch-container bash
# Run interactive password setup
elasticsearch-setup-passwords interactive
# Test connection
curl -XGET --user elastic:your_password 'http://127.0.0.1:9200'
- Plugin Installation
Install and manage Elasticsearch plugins, such as analysis plugins.
8.1 Online Installation
Install a plugin directly from the internet.
elasticsearch-plugin install https://github.com/medcl/elasticsearch-analysis-ik/releases/download/v7.12.1/elasticsearch-analysis-ik-7.12.1.zip
8.2 Offline Installation
Install a plugin from a local file when internet access is not available.
# Create plugin directory
mkdir -p /opt/elasticsearch/elasticsearch/plugins/ik
# Download plugin package
wget https://github.com/medcl/elasticsearch-analysis-ik/releases/download/v7.12.1/elasticsearch-analysis-ik-7.12.1.zip -P /opt/elasticsearch/elasticsearch/plugins/ik
# Extract the plugin
cd /opt/elasticsearch/elasticsearch/plugins/ik
unzip elasticsearch-analysis-ik-7.12.1.zip
8.3 Plugin Management
List and remove installed plugins.
# List installed plugins
elasticsearch-plugin list
# Remove a plugin
elasticsearch-plugin remove analysis-ik
Note: Elasticsearch service restart is required after plugin installation or removal.