Elasticsearch Basics (2): Installation and deployment of Elasticsearch on windows and liunx

Preface

This article is based on the official document: Installing Elasticsearch
is based on several official installation methods for different environments. This article will choose

  • Install Elasticsearch with .zip on Windows
    Install Elasticsearch on Windows using .zip file

  • Install Elasticsearch from archive on Linux or MacOS
    Install Elasticsearch from archive on Linux or macOS

  • Install Elasticsearch with Docker (This method is to be determined)
    Use Docker to install Elasticsearch

Insert image description here

1. Deploy Elasticsearch in Windows environment

1.1 Download and unzip the Elasticsearch compressed package

Insert image description here

Unzip it with your favourite unzip tool. This will create a folder called elasticsearch-7.9.3, which we will refer to as %ES_HOME%. In a terminal window, cd to the %ES_HOME% directory, for instance:

Set the decompression directory as the home directory of es and 在 elasticsearch-7.9.3文件夹地址栏输入cmdenter the command line
Insert image description here

1.2 Start elasticsearch from the command line

  • Start elasticsearch


    Running Elasticsearch from the command line.Elasticsearch can be started from the command line as follows :

    .\bin\elasticsearch.bat
    
  • Start page
    Insert image description here

  • Launch success page
    Insert image description here

1.3 Verify whether elasticsearch is successfully started

Checking that Elasticsearch is running
You can test that your Elasticsearch node is running by sending an HTTP request to port 9200 on localhost: Checking that Elasticsearch is running You can test that your Elasticsearch node is running by sending an HTTP request to port 9200 on localhost: :

win+r输入cmdAfter pressing Enter, enter the following request URL

curl -X GET "localhost:9200/?pretty"

Indicates successful startup
Insert image description here
or enter in the browser address bar:localhost:9200
Insert image description here

1.4 Close Elasticsearch

  • Ctrl+C stops Elasticsearch


    By default, Elasticsearch runs in the foreground, prints its logs to STDOUT, and can Ctrl-Cbe stopped by pressing Ctrl-C. it.

    Insert image description here

  • Verify whether it has been closed (directly closing the command line window above can also stop es)
    Insert image description here

1.5 Install Elasticsearch as a service on Windows

Elasticsearch can be installed as a service to run in the background or start automatically at boot time without any user interaction. This can be achieved through the elasticsearch-service.bat script in the bin\ folder which allows one to install, remove, manage or configure the service and potentially start and stop the service, all from the command-line.
Elasticsearch can be installed as a service 后台运行或在启动时自动启动, without any user interaction. This is achieved through the elasticsearch-service.bat script in the bin\ folder, which allows you to install, remove, manage or configure the service from the command line, and also potentially start and stop the service.

e:\elasticsearch-7.9.3\bin>elasticsearch-service.bat

Usage: elasticsearch-service.bat install|remove|start|stop|manager [SERVICE_ID]

Insert image description here


The script requires one parameter (the command to execute) followed by an optional one indicating the service id (useful when installing multiple Elasticsearch services) 可选参数. Useful when installing multiple Elasticsearch services)

The following are the commands available in the elasticsearch-service.bat script and their descriptions:

Order describe
install Install Elasticsearch as a service
remove Remove the installed Elasticsearch service (stop the service if it is started)
start Start the Elasticsearch service (if installed)
stop Stop the Elasticsearch service (if it is started)
manager Open the GUI interface for managing installed services
  • Specify the service name to install.
    Install as a service. Service ID ( 务必唯一): elasticsearch-9200-test
    elasticsearch-service.bat install elasticsearch-9200-test
    
  • Start or stop services from the command line
    • Start service
      elasticsearch-service.bat start elasticsearch-9200-test
      
      Insert image description here
    • Out of service
      elasticsearch-service.bat stop elasticsearch-9200-test
      
      Insert image description here
  • Start or stop the service on the service page.
    Win+r and press Enter and enter services.msc to enter the service list. Right-click and select Start or Stop.
    Insert image description here
  • Set it to start automatically at boot.
    Right-click the service name, click Properties, and select the startup type as Automatic.
    Insert image description here

2. Deploy Elasticsearch in Liunx environment

Install Elasticsearch 7.17.11 and configure

This article describes how to download and install Elasticsearch 7.17.11 on the Linux operating system, and perform the necessary configurations. The configuration and startup process in centos or unbuntu is basically the same, but the scripts for open ports are different.

1. Download the es database and upload it to the server

First, visit the Elasticsearch download page and download Elasticsearch 7.17.11.
Insert image description here
Click "Download" and upload the downloaded file to the specified directory on the server.
Insert image description here

On the server, unpack Elasticsearch using the following command:

tar -zxvf elasticsearch-7.17.11-linux-x86_64.tar.gz

Insert image description here

2. Create Elasticsearch users and groups

Create a user named "es" and a group named "es" and add the user to the group:

# 新建群组es
 groupadd es
# 新建用户es并指定群组为es
 useradd -g es es
# 设置用户密码 
 passwd es  
# usermod 将用户添加到某个组group
 usermod -aG root es

3. System configuration

3.1 Modify the number of file handles and threads

In order to prevent errors caused by low permissions on createable file descriptors owned by the Elasticsearch user, you need to modify the number of file handles and threads. Edit /etc/security/limits.confthe file and add the following content:

# 文件句柄
es  soft nofile 65536
es  hard nofile 65536
# 线程
es  soft nproc 4096
es  hard nproc 4096

保存退出后,需要重新启动系统

The above configuration is to solve:

Error report: max file descriptors [4096] for elasticsearch process is too low, increase to at least [65535]
Problem description: The elasticsearch user has too low permissions to create file descriptions, at least 65536 is required;

3.2 Modify virtual memory

Edit /etc/sysctl.confthe file and add the following content:

vm.max_map_count=262144

After saving and exiting, refresh the configuration file:

sysctl -p

Verify whether the modification is successful:

sysctl vm.max_map_count

The above configuration is to solve
the error problem: max virtual memory areas vm.max_map_count [65530] is too low, increase to at least [262144]

3.3 Close the swap space (Swap)

Official recommendation: Give half of the memory to Lucene+ and do not exceed 32G + turn off swap.
ES recommends turning off the swap memory swap space and disabling swapping. Because when the memory is swapped to the disk, a 100 microsecond operation may become 10 milliseconds, and then the 100 microsecond operation delay adds up. It can be seen that swapping has a fatal impact on performance.

vim /etc/fstab

The comment contains the swap line.
Insert image description here
Before the comment:
Insert image description here

保存退出后需要系统重启!

After comment:
Insert image description here

4. Modify Elasticsearch configuration

Edit the Elasticsearch configuration file conf/elasticsearch.ymland configure it according to your needs. Here are some example configuration items:

# 禁用了 es 的机器学习功能(Machine Learning)减少资源消耗
xpack.ml.enabled: false
# 设置 Elasticsearch 集群的名称
cluster.name: es-single
# 设置当前 Elasticsearch 节点的名称
node.name: node
# 数据目录
path.data: /home/es/path/node/data
# 日志目录
path.logs: /home/es/path/node/logs
# 当前主机的 IP
network.host: 192.168.0.10
# 暴露的 HTTP 端口
http.port: 11700
# 暴露的 Transport 端口
transport.port: 11710
# 设置节点发现的种子主机列表
discovery.seed_hosts: ["192.168.0.10:11710"]
# 设置初始的主节点列表,新节点将联系这些主节点以加入集群
cluster.initial_master_nodes: ["node"]

5. Set directory permissions

Set the user and group to which the Elasticsearch data directory belongs:

chown -R es:es /mnt/data/elasticsearch-7.17.11

6. Start the Elasticsearch service

注意当前目录是在 ../elasticsearch-7.17.11

Create a script to start and stop the Elasticsearch service:

startes-single.sh

#!/bin/bash
cd "$(dirname "$0")"
# -d:后台(daemon)方式运行 Elasticsearch
./bin/elasticsearch -d -p pid

stopes-single.sh

#!/bin/bash
cd "$(dirname "$0")"
if [ -f "pid" ]; then
  pkill -F pid
fi

Give execution permissions to these two scripts:

chmod 755 startes-single.sh stopes-single.sh
chown es:es startes-single.sh stopes-single.sh

Then, start the Elasticsearch service as the Elasticsearch user:

su - es
cd /mnt/data/elasticsearch-7.17.11
./startes-single.sh

7. Open firewall ports

CentOS

# 查看防火墙状态
systemctl status firewalld
# 查看开放的端口
firewall-cmd --query-port=9200/tcp
# 添加端口
firewall-cmd --zone=public --add-port=9200/tcp --permanent
# 重载防火墙
firewall-cmd --reload
# 再次查看端口是否已经开放
firewall-cmd --query-port=9200/tcp

Ubuntu

# 查看防火墙状态
sudo ufw status
# 开放端口 9200
sudo ufw allow 9200/tcp
# 查看已添加的规则
sudo ufw status numbered
# 查看防火墙状态
sudo ufw status

Guess you like

Origin blog.csdn.net/qq_29864051/article/details/131446118