首页 > 技术文章 > 大数据学习初级入门教程(九) —— Elasticsearch 7.6.2 伪分布式集群的安装、配置、启动和测试

tzhuwb 2020-04-04 12:56 原文

在前一篇文章《大数据学习初级入门教程(八) —— Elasticsearch 7.6.2 单节点的安装、启动和测试》中,简要说明了在一台测试服务器上如何安装单节点 Elasticsearch 并做了简单的测试,这篇主要说下在一台机器上如何配置多个节点,需要在前一篇文章操作后的基础上,搭建 Elasticsearch 伪分布式集群,这里配置 3 个代理节点。

特别注意:由于需要在一台机器上同时启动 3 个及以上 es 实例节点,所以建议机器内存>=4g。

第一步:复制节点

以上篇文章中搭建的节点为第一节点,再完全复制两个节点出来,并重命名。详细如下:

$ cd /opt/es/

$ cp -R elasticsearch-7.6.2 elasticsearch-7.6.2-2

$ cp -R elasticsearch-7.6.2 elasticsearch-7.6.2-3

第二步:修改配置

分别修改 elasticsearch-7.6.2-2 和 elasticsearch-7.6.2-2 实例下的配置文件,修改后三个节点的配置分别如下:

elasticsearch-7.6.2/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
#
# 表示该节点会不会作为主节点,true表示会;false表示不会
node.master: true
# 当前节点是否用于存储数据,是:true、否:false
node.data: true
#
# ----------------------------------- Paths ------------------------------------
#
# Path to directory where to store the data (separate multiple locations by comma):
#
# 索引数据存放的位置
#path.data: /path/to/data
#
# Path to log files:
#
# 日志文件存放的位置
#path.logs: /path/to/logs
#
# ----------------------------------- Memory -----------------------------------
#
# Lock the memory on startup:
#
# 需要锁住物理内存,是:true、否:false
#bootstrap.memory_lock: true
# 系统调用过滤器检查,是: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.
#
# ---------------------------------- Network -----------------------------------
#
# Set the bind address to a specific IP (IPv4 or IPv6):
#
# 监听地址,用于访问该es
network.host: 192.168.220.242
#
# Set a custom port for HTTP:
#
# es对外提供的http端口,默认 9200
http.port: 9200
#
# For more information, consult the network module documentation.
#
# TCP的默认监听端口,默认 9300
transport.tcp.port: 9300
#
# 是否支持跨域,是:true,在使用head插件时需要此配置
http.cors.enabled: true
# “*” 表示支持所有域名
http.cors.allow-origin: "*"
#
# --------------------------------- Discovery ----------------------------------
#
# 设置这个参数来保证集群中的节点可以知道其它N个有master资格的节点。默认为1,对于大的集群来说,可以设置大一点的值(2-4)
discovery.zen.minimum_master_nodes: 2
#
# 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]"]
#
# es7.x 之后新增的配置,写入候选主节点的设备地址,在开启服务后可以被选为主节点
discovery.seed_hosts: ["192.168.220.242:9300", "192.168.220.242:9301", "192.168.220.242:9302"]
# 判断结点是否脱离时间配置
discovery.zen.fd.ping_timeout: 60s
# 判断结点是否脱离次数配置
discovery.zen.fd.ping_retries: 5
#
# Bootstrap the cluster using an initial set of master-eligible nodes:
#
# es7.x 之后新增的配置,初始化一个新的集群时需要此配置来选举master
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

elasticsearch-7.6.2-2/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-2
#
# Add custom attributes to the node:
#
#node.attr.rack: r1
#
# 表示该节点会不会作为主节点,true表示会;false表示不会
node.master: true
# 当前节点是否用于存储数据,是:true、否:false
node.data: true
#
# ----------------------------------- Paths ------------------------------------
#
# Path to directory where to store the data (separate multiple locations by comma):
#
# 索引数据存放的位置
#path.data: /path/to/data
#
# Path to log files:
#
# 日志文件存放的位置
#path.logs: /path/to/logs
#
# ----------------------------------- Memory -----------------------------------
#
# Lock the memory on startup:
#
# 需要锁住物理内存,是:true、否:false
#bootstrap.memory_lock: true
# 系统调用过滤器检查,是: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.
#
# ---------------------------------- Network -----------------------------------
#
# Set the bind address to a specific IP (IPv4 or IPv6):
#
# 监听地址,用于访问该es
network.host: 192.168.220.242
#
# Set a custom port for HTTP:
#
# es对外提供的http端口,默认 9200
http.port: 9201
#
# For more information, consult the network module documentation.
#
# TCP的默认监听端口,默认 9300
transport.tcp.port: 9301
#
# 是否支持跨域,是:true,在使用head插件时需要此配置
http.cors.enabled: true
# “*” 表示支持所有域名
http.cors.allow-origin: "*"
#
# --------------------------------- Discovery ----------------------------------
#
# 设置这个参数来保证集群中的节点可以知道其它N个有master资格的节点。默认为1,对于大的集群来说,可以设置大一点的值(2-4)
discovery.zen.minimum_master_nodes: 2
#
# 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]"]
#
# es7.x 之后新增的配置,写入候选主节点的设备地址,在开启服务后可以被选为主节点
discovery.seed_hosts: ["192.168.220.242:9300", "192.168.220.242:9301", "192.168.220.242:9302"]
# 判断结点是否脱离时间配置
discovery.zen.fd.ping_timeout: 60s
# 判断结点是否脱离次数配置
discovery.zen.fd.ping_retries: 5
#
# Bootstrap the cluster using an initial set of master-eligible nodes:
#
# es7.x 之后新增的配置,初始化一个新的集群时需要此配置来选举master
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

elasticsearch-7.6.2-3/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-3
#
# Add custom attributes to the node:
#
#node.attr.rack: r1
#
# 表示该节点会不会作为主节点,true表示会;false表示不会
node.master: true
# 当前节点是否用于存储数据,是:true、否:false
node.data: true
#
# ----------------------------------- Paths ------------------------------------
#
# Path to directory where to store the data (separate multiple locations by comma):
#
# 索引数据存放的位置
#path.data: /path/to/data
#
# Path to log files:
#
# 日志文件存放的位置
#path.logs: /path/to/logs
#
# ----------------------------------- Memory -----------------------------------
#
# Lock the memory on startup:
#
# 需要锁住物理内存,是:true、否:false
#bootstrap.memory_lock: true
# 系统调用过滤器检查,是: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.
#
# ---------------------------------- Network -----------------------------------
#
# Set the bind address to a specific IP (IPv4 or IPv6):
#
# 监听地址,用于访问该es
network.host: 192.168.220.242
#
# Set a custom port for HTTP:
#
# es对外提供的http端口,默认 9200
http.port: 9202
#
# For more information, consult the network module documentation.
#
# TCP的默认监听端口,默认 9300
transport.tcp.port: 9302
#
# 是否支持跨域,是:true,在使用head插件时需要此配置
http.cors.enabled: true
# “*” 表示支持所有域名
http.cors.allow-origin: "*"
#
# --------------------------------- Discovery ----------------------------------
#
# 设置这个参数来保证集群中的节点可以知道其它N个有master资格的节点。默认为1,对于大的集群来说,可以设置大一点的值(2-4)
discovery.zen.minimum_master_nodes: 2
#
# 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]"]
#
# es7.x 之后新增的配置,写入候选主节点的设备地址,在开启服务后可以被选为主节点
discovery.seed_hosts: ["192.168.220.242:9300", "192.168.220.242:9301", "192.168.220.242:9302"]
# 判断结点是否脱离时间配置
discovery.zen.fd.ping_timeout: 60s
# 判断结点是否脱离次数配置
discovery.zen.fd.ping_retries: 5
#
# Bootstrap the cluster using an initial set of master-eligible nodes:
#
# es7.x 之后新增的配置,初始化一个新的集群时需要此配置来选举master
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

第三步:启动集群

依次启动 3 个 es 节点实例:

./bin/elasticsearch

如果启动中发生如下错误,说明机器内存配置不足:

[elastic@test242 elasticsearch-7.6.2-2]$ ./bin/elasticsearch
Exception in thread "main" java.lang.RuntimeException: starting java failed with [1]
output:
#
# There is insufficient memory for the Java Runtime Environment to continue.
# Native memory allocation (mmap) failed to map 986513408 bytes for committing reserved memory.
# An error report file with more information is saved as:
# logs/hs_err_pid4427.log
error:
OpenJDK 64-Bit Server VM warning: Option UseConcMarkSweepGC was deprecated in version 9.0 and will likely be removed in a future release.
OpenJDK 64-Bit Server VM warning: INFO: os::commit_memory(0x00000000c5330000, 986513408, 0) failed; error='Not enough space' (errno=12)
        at org.elasticsearch.tools.launchers.JvmErgonomics.flagsFinal(JvmErgonomics.java:123)
        at org.elasticsearch.tools.launchers.JvmErgonomics.finalJvmOptions(JvmErgonomics.java:88)
        at org.elasticsearch.tools.launchers.JvmErgonomics.choose(JvmErgonomics.java:59)
        at org.elasticsearch.tools.launchers.JvmOptionsParser.main(JvmOptionsParser.java:95)

解决方法:

第一种,增加机器内存配置。

第二种,修改配置文件 elasticsearch-7.6.2/config/jvm.options 中的配置,调小内存需求。详细如下:

# Xms represents the initial size of total heap space
# Xmx represents the maximum size of total heap space

-Xms512m
-Xmx512m

如果启动中出现如下错误,信息如下:

org.elasticsearch.cluster.coordination.CoordinationStateRejectedException: not enough nodes discovered to form a quorum in the initial configuration

 

可以看到三个节点都报这个错误,这就是所谓的“脑裂”。脑裂是指在主备切换时,由于切换不彻底或其他原因,导致客户端和 Slave 误以为出现两个 active master,最终使得整个集群处于混乱状态。

discovery.zen.minimum_master_nodes 配置的数量如何才为最优?自动发现 master 节点的最小数,如果这个集群中配置进来的 master 节点少于这个数目,es 的日志会一直报 master 节点数目不足。(默认为1)为了避免脑裂,个数请遵从该公式 =》 total number of master - eligible nodes / 2 + 1,可以尝试修改该值为1。

再次启动集群,可以看到 3 个节点均已正常启动,无报错。通过浏览访问地址:http://192.168.220.242:9200/_cat/nodes,可以看到集群运行节点信息,如图:

备注:带 * 的为 master节点,即当前 node-2 节点为 master 节点。

推荐阅读