Elasticsearch集群搭建(基于Elasticsearch7.5.1)

Elasticsearch集群搭建

环境介绍:CentOS7、jdk11.0.5、Elasticsearch7.5.1

准备三台Linux系统,本教程使用的是CentOS7如下IP地址:

192.168.28.129
192.168.28.130
192.168.29.131

注意:
以上三台Linux系统都要安装jdk

下载Elasticsearch

Linux: elasticsearch-7.5.1-linux-x86_64.tar.gz

curl -L -O https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-7.5.1-linux-x86_64.tar.gz

系统设置

Elasticsearch不能在 root 用户下启动,我们需要在三台机器上分别创建一个普通用户:

# 创建elastic用户
useradd elastic
# 设置用户密码
passwd elastic
# 切换到elastic用户
su elastic

分别在三台机器上的 /home/elastic/ 目录下创建elasticsearch文件夹,然后在elasticsearch文件夹下分别创建data、logs文件夹:

cd /home/elastic/
mkdir -p elasticsearch/data
mkdir -p elasticsearch/logs

在生产环境下我们要把Elasticsearch生成的索引文件数据存放到自定义的目录下
data:存储Elasticsearch索引文件数据
logs:存储日志文件

配置Elasticsearch

我们只需要简单的配置一下Elasticsearch就可以使用了

首先我们将下载好的 elasticsearch-7.5.1-linux-x86_64.tar.gz 压缩包上传到 192.168.28.129 这台机器上的 /home/elastic/elasticsearch/ 目录下,随便那一台机器都可以没有顺序区分。

解压 elasticsearch-7.5.1-linux-x86_64.tar.gz

tar -xvf elasticsearch-7.5.1-linux-x86_64.tar.gz

解压完成后在 /home/elastic/elasticsearch/ 目录下输入 ll 命令查看当前目录下的所有文件和文件夹如下:
image.png

修改elasticsearch.yml

输入如下命令修改 elasticsearch.yml 配置文件:

vi elasticsearch-7.5.1/config/elasticsearch.yml

修改后的配置文件如下,可以直接复制:

# ======================== Elasticsearch Configuration =========================
#
# NOTE: Elasticsearch comes with reasonable defaults for most settings.
#       Before you set out to tweak and tune the configuration, make sure you
#       understand what are you trying to accomplish and the consequences.
#
# The primary way of configuring a node is via this file. This template lists
# the most important settings you may want to configure for a production cluster.
#
# Please consult the documentation for further information on configuration options:
# https://www.elastic.co/guide/en/elasticsearch/reference/index.html
#
# ---------------------------------- Cluster -----------------------------------
#
# Use a descriptive name for your cluster:
#
cluster.name: my-application
#
# ------------------------------------ Node ------------------------------------
#
# Use a descriptive name for the node:
#
node.name: node-1
#
# Add custom attributes to the node:
#
#node.attr.rack: r1
#
# ----------------------------------- Paths ------------------------------------
#
# Path to directory where to store the data (separate multiple locations by comma):
#
path.data: /home/elastic/elasticsearch/data
#
# Path to log files:
#
path.logs: /home/elastic/elasticsearch/logs
#
# ----------------------------------- Memory -----------------------------------
#
# Lock the memory on startup:
#
#bootstrap.memory_lock: true
#
# Make sure that the heap size is set to about half the memory available
# on the system and that the owner of the process is allowed to use this
# limit.
#
# Elasticsearch performs poorly when the system is swapping the memory.
#
# ---------------------------------- Network -----------------------------------
#
# Set the bind address to a specific IP (IPv4 or IPv6):
#
network.host: 192.168.28.129
#
# Set a custom port for HTTP:
#
http.port: 9200
#
# For more information, consult the network module documentation.
#
# --------------------------------- Discovery ----------------------------------
#
# Pass an initial list of hosts to perform discovery when this node is started:
# The default list of hosts is ["127.0.0.1", "[::1]"]
#
discovery.seed_hosts: ["192.168.28.129", "192.168.28.130", "192.168.28.131"]
#
# Bootstrap the cluster using an initial set of master-eligible nodes:
#
cluster.initial_master_nodes: ["node-1", "node-2", "node-3"]
#
# For more information, consult the discovery and cluster formation module documentation.
#
# ---------------------------------- Gateway -----------------------------------
#
# Block initial recovery after a full cluster restart until N nodes are started:
#
#gateway.recover_after_nodes: 3
#
# For more information, consult the gateway module documentation.
#
# ---------------------------------- Various -----------------------------------
#
# Require explicit names when deleting indices:
#
#action.destructive_requires_name: true
#
# ---------------------------------- xpack -------------------------------------
#
xpack.monitoring.collection.enabled: true

主要修改如下几处配置:

  1. cluster.name:集群的名称,集群中所有节点的 cluster.name 的值必须要相同。
  2. node.name:集群中每个Elasticsearch的节点名称,不可以重复。
  3. path.data:设置存放Elasticsearch索引文件数据的路径。
  4. path.logs:设置存放日志文件的路径。
  5. network.host:Elasticsearch绑定的IP,外界可以通过这个IP访问到当前Elasticsearch节点,一般配配置当前系统的IP,或者 0.0.0.0 (任何地址都能访问到)。
  6. http.port:当前启动Elasticsearch的端口号,一般默认 9200 即可,当然你也可以修改
  7. discovery.seed_hosts:配置所有Elasticsearch节点绑定的IP地址。
  8. cluster.initial_master_nodes:配置那些节点可以有资格被选为主节点。
  9. xpack.monitoring.collection.enabled:收集监控数据默认为false不收集监控数据。

我们已经配置好一台Elasticsearch节点了接下来我们只需要把这台配置好的Elasticsearch复制到另外两台机器中在做一些简单的修改就就可以了。

我们使用 scp 命令复制当前配置好的Elasticsearch到另外两台机器中:

scp -r /home/elastic/elasticsearch/elasticsearch-7.5.1 elastic@192.168.28.130:/home/elastic/elasticsearch/elasticsearch-7.5.1
scp -r /home/elastic/elasticsearch/elasticsearch-7.5.1 elastic@192.168.28.131:/home/elastic/elasticsearch/elasticsearch-7.5.1

分别在两台机器中修改几处配置:
192.168.28.130 这台机器修改elasticsearch.yml配置文件如下:

node.name: node-2
network.host: 192.168.28.130

192.168.28.131 这台机器修改elasticsearch.yml配置文件如下:

node.name: node-3
network.host: 192.168.28.131

启动Elasticsearch

从命令行运行Elasticsearch

Elasticsearch可以从命令行启动,如下所示:

./bin/elasticsearch

作为后台启动

要将Elasticsearch作为后台程序运行,请在命令中指定-d,然后使用-p将进程ID记录在文件中:

./bin/elasticsearch -d -p pid

分别在三台机器上启动Elasticsearch,启动过程中建议单个机器启动成功后在启动另一台。

注意:
在启动过程中如果出现错误请看下面章节

设置系统配置

下面的所有修改在生产环境下都可能不会出现,如果出现了就做相应的修改即可
修改系统配置文件需要切换到 root 用户下:

su root

会提示你输入 root 用户密码,输入正确的密码即可

设置虚拟内存

将虚拟内存设置大一些,否则在启动elasticsearch时会出错导致启动失败:

ERROR: [1] bootstrap checks failed
[1]: max virtual memory areas vm.max_map_count [65530] is too low, increase to at least [262144]

输入 vi /etc/sysctl.conf 命令在 sysctl.conf 中配置如下内容:

vm.max_map_count=655360

接着输入如下命令让配置生效:

sysctl -p

设置最大文件描述符

在启动Elasticsearch有可能会出现如下错误:

[1]: max file descriptors [4096] for elasticsearch process is too low, increase to at
least [65536]

错误说明:elasticsearch过程的最大文件描述符 [4096] 太低,增加到
最少 [65536]

接下来我们修改最大文件描述符,输入 vi /etc/security/limits.conf 命令在 limits.conf 中配置如下内容:

* soft nofile 65536
* hard nofile 131072
* soft nproc 2048
* hard nproc 4096

设置最大线程数

在启动Elasticsearch有可能会出现如下错误:

[2]: max number of threads [1024] for user [elsearch] is too low, increase to at least[4096]

错误说明:线程[1024]用户[elsearch]的最大数量太低,增加至少[4096]

接下来我们修改线程数,输入 vi /etc/security/limits.d/20-nproc.conf 命令在 20-nproc.conf 中配置如下内容:

* soft nproc 4096

SecComp

在启动Elasticsearch有可能会出现如下错误:

[3]: system call filters failed to install; check the logs and fix your configuration or disable system call filters at your own risk

错误说明:系统调用过滤器安装失败;检查日志和修复配置或禁用系统调用过滤器需要您自担风险
这是在因为 Centos7 不支持 SecComp ,而ES5.2.0之后默认 bootstrap.system_call_filtertrue进行检测,所以导致检测失败,失败后直接导致ES不能启动。

接下来我们修改配置文件,输入 vim config/elasticsearch.yml 命令,在 elasticsearch.yml 中配置如下内容:

# ----------------------------------- Memory -----------------------------------
#
# Lock the memory on startup:
#
#bootstrap.memory_lock: true
# 设置为false不进行检测
bootstrap.system_call_filter: false
#
# Make sure that the heap size is set to about half the memory available
# on the system and that the owner of the process is allowed to use this
# limit.
#
# Elasticsearch performs poorly when the system is swapping the memory.
#

检查集群

上面我们已经搭建好了三个节点的集群,并且已经启动了。

接下来我们来检查一下集群是否已经形成,给三台服务器中的任意一台发送http请求:

http://192.168.28.129:9200/_cat/health?v

应该会反馈如下内容:

epoch      timestamp cluster        status node.total node.data shards pri relo init unassign pending_tasks max_task_wait_time active_shards_percent
1579067395 05:49:55  my-application green           3         3     22  11    0    0        0             0                  -                100.0%

cluster:显示的是当前集群的名称
status:显示的是 green 表示当前集群是健康的状态
node.total:显示 3 表示当前集群有三个节点

最后我们的Elasticsearch集群已经搭建好了,本教程主要参考Elastic官方文档

你可能感兴趣的