Elasticsearch集群部署详解

Elasticsearch集群部署详解

环境:centos6.9、jdk1.8.0_151、elasticsearch-5.6.5
注意:es5.x要求jdk1.8,否则会报错
本文以讲解Elasticsearch三个节点的分布式部署、核心配置的含义以及分布式部署遇到的坑。

部署节点原理

  • 多机集群中的节点可以分为master nodes和data nodes,在配置文件中使用Zen发现(Zen discovery)机制来管理不同节点。Zen发现是ES自带的默认发现机制,使用多播发现其它节点。只要启动一个新的ES节点并设置和集群相同的名称这个节点就会被加入到集群中。(所以,同集群的集群名称一致,才能便于自动发现)
  • Elasticsearch集群中有的节点一般有三种角色:master node、data node和client node。

    1)master node——master节点点主要用于元数据(metadata)的处理,比如索引的新增、删除、分片分配等。
    2)client node——client 节点起到路由请求的作用,实际上可以看做负载均衡器。
    3)data node——data节点上保存了数据分片。它负责数据相关操作,比如分片的 CRUD,以及搜索和整合操作。这些操作都比较消耗 CPU、内存和 I/O 资源;

    节点详解,会在下一篇博客中说明

三节点 Elasticsearch 分布式部署。

节点规划

master node:10.0.15.57
client node:10.0.15.12
data node:10.0.15.2

1、下载
下载地址 https://www.elastic.co/downloads/past-releases/elasticsearch-5-6-5

wget https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-5.6.5.tar.gz

2、解压

tar zxvf elasticsearch-5.6.5.tar.gz
3、创建ES数据文件和日志文件目录
cd elasticsearch

数据目录

mkdir data

日志目录

mkdir logs

4、修改ES配置文件
进入到config文件夹,编辑 elasticsearch.yml

vim elasticsearch-5.6.5/config/elasticsearch.yml

这里以主节点为例
步骤1:配置好主节点Master信息。

# ======================== 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: es
#
# ------------------------------------ Node ------------------------------------
#
# Use a descriptive name for the node:
#节点名称,各个节点不同
#node.name: node-1
#
# Add custom attributes to the node:
#该节点所属机架
#node.attr.rack: r1
##该节点的名字
node.name: "test01-master"
#该节点有机会成为master节点
node.master: true
#该节点可以存储数据
node.data: true
# ----------------------------------- Paths ------------------------------------
#
# Path to directory where to store the data (separate multiple locations by comma):
#数据存放目录
path.data: /opt/elasticsearch/data
#
# Path to log files:
#日志存放目录
path.logs: /opt/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):
#修改一下ES的监听地址,这样别的机器才可以访问
network.host: 10.0.15.57
#
# Set a custom port for HTTP:
#端口
http.port: 9200
#
# For more information, consult the network module documentation.
#
# 增加新的参数,这样head插件可以访问es
http.cors.enabled: true
http.cors.allow-origin: "*"
# --------------------------------- Discovery ----------------------------------
#
# Pass an initial list of hosts to perform discovery when new node is started:
# The default list of hosts is ["127.0.0.1", "[::1]"]
#定义发现的节点:注意,分布式系统整个集群节点个数N要为奇数个
discovery.zen.ping.unicast.hosts: ["10.0.15.57:9300", "10.0.15.12:9300","10.0.15.2:9300"]
#
# Prevent the "split brain" by configuring the majority of nodes (total number of master-eligible nodes / 2 + 1):
#master选举最少的节点数,这个一定要设置为N/2+1,其中N是:具有master资格的节点的数量,而不是整个集群节点个数。
discovery.zen.minimum_master_nodes: 2
#
# For more information, consult the zen discovery module documentation.
# ---------------------------------- Gateway -----------------------------------
#
# Block initial recovery after a full cluster restart until N nodes are started:
#
gateway.recover_after_nodes: 3
gateway.recover_after_time: 5m
gateway.expected_nodes: 1
# For more information, consult the gateway module documentation.
#
# ---------------------------------- Various -----------------------------------
#
# Require explicit names when deleting indices:
#
#action.destructive_requires_name: true

5、拷贝到其他机器上
6、修改配置文件
cient节点;修改节点名称信息。
只列举不一样的配置:

node.name: "test02-cient"
node.master: true
node.data: false
network.host: 10.0.15.12

步骤3: data节点;修改节点名称
只列举不一样的配置:

node.name: “test03-data”
node.master: false
node.data: true
network.host: 10.0.15.2

7、创建用户
+++注意:三台机器都要创建+++
root用户无法启动es,必须新建一个其他用户,并对其赋予es目录的操作权限 ,这里我新建了es用户和组
创建组

grupadd es

创建用户

useradd es -g es

修改用户和组

chown -R es:es es的安装目录

查看
Elasticsearch集群部署详解_第1张图片
注意缺少这一步会报错:can not run elasticsearch as root

[2018-04-25T16:37:08,160][WARN ][o.e.b.ElasticsearchUncaughtExceptionHandler] [node01] uncaught exception in thread [main]
org.elasticsearch.bootstrap.StartupException: java.lang.RuntimeException: can not run elasticsearch as root
    at org.elasticsearch.bootstrap.Elasticsearch.init(Elasticsearch.java:136) ~[elasticsearch-5.6.5.jar:5.6.5]

8、启动
分别运行Master,client,data节点(顺序无关)
控制台启动命令

./elasticsearch

后台启动命令

./elasticsearch -d

成功效果
Elasticsearch集群部署详解_第2张图片

浏览器查看(分别输入三台机器ip:9200)
Elasticsearch集群部署详解_第3张图片

用head插件可以看到各个节点情况
Elasticsearch集群部署详解_第4张图片

9、分布式部署遇到的坑
启动可能出现的错误
错误1:max file descriptors [65535] for elasticsearch process is too low, increase to at least [65536]
这个问题是无法创建本地文件,用户最大可创建文件数太小
解决:只需要修改创建文件的最大数目为65536就行了
切换到root用户修改

vim /etc/security/limits.conf

    root soft nofile 65536
    root hard nofile 65536
    * soft nofile 65536
    * hard nofile 65536

保存、退出、重新登录才可生效

参数解释:
- soft nproc:可打开的文件描述符的最大数(软限制)
- hard nproc:可打开的文件描述符的最大数(硬限制)
- soft nofile:单个用户可用的最大进程数量(软限制)
- hard nofile:单个用户可用的最大进程数量(硬限制)

错误2:max virtual memory areas vm.max_map_count [65530] is too low, increase to at least [262144]
虚拟内存太小
切换到root用户修改

vim /etc/sysctl.conf

vm.max_map_count=262144

执行命令:

sudo sysctl -p

错误3:Java HotSpot(TM) 64-Bit Server VM warning: INFO: os::commit_memory(0x0000000085330000, 2060255232, 0) failed; error=’Cannot allocate memory’ (errno=12)

jvm需要分配的内存太大

vim config/jvm.options

-Xms2g
-Xmx2g
该为:
-Xms100m
-Xmx100m

错误4:max number of threads [1024] for user [es] likely too low, increase to at least [2048]
原因:无法创建本地线程问题,用户最大可创建线程数太小
解决方案:切换到root用户,进入limits.d目录下,修改90-nproc.conf 配置文件
成功启动:

vi /etc/security/limits.d/90-nproc.conf

“`
找到如下内容:

* soft nproc 1024

修改为

* soft nproc 2048
“`
错误5:n ERROR No log4j2 configuration file found. Using default configuration: logging only errors to the console. Set system property ‘log4j2.debug‘ to show Log4j2 internal initialization logging.
es用

yum install -y log4j*

错误6:unknown setting [discovery.zen.ping.multicast.enabled] please check that any required plugins are installed, or check the breaking changes documentation for removed settings
在elasticsearch.yml文件中 添加bootstrap.system_call_filter: false

错误7:三节点不能联通的:
原因:
1)各节点的hostname没有正确设置,和节点名称设置为一致。
2)关闭防火墙,service iptables stop;否则,打开防火墙会导致无法正常通信,head插件不能看到节点数据信息。

你可能感兴趣的:(es)