ES集群(世间美好与你环环相扣)

那么本章学习我主要归为三大类;

  • 集群规划
  • 集群搭建
  • 集群管理

ES诞生
在这里插入图片描述

一、集群规划

搭建一个集群我们需要考虑如下几个问题:

  1. 我们需要多大规模的集群?

  2. 集群中的节点角色如何分配?

  3. 如何避免脑裂问题?

  4. 索引应该设置多少个分片?

  5. 分片应该设置几个副本?

下面我们就来分析和回答这几个问题

1、我们需要多大规模的集群?

需要从以下两个方面考虑:

1.1 当前的数据量有多大?数据增长情况如何?
1.2 你的机器配置如何?cpu、多大内存、多大硬盘容量?

推算的依据:
ES JVM heap 最大可以设置32G 。
30G heap 大概能处理的数据量 10 T。如果内存很大如128G,可在一台机器上运行多个ES节点实例。

备注:集群规划满足当前数据规模+适量增长规模即可,后续可按需扩展。

两类应用场景:

A. 用于构建业务搜索功能模块,且多是垂直领域的搜索。数据量级几千万到数十亿级别。一般2-4台机器的规模。
B. 用于大规模数据的实时OLAP(联机处理分析),经典的如ELK Stack,数据规模可能达到千亿或更多。几十到上百节点的规模。

2、集群中的节点角色如何分配?

2.1 节点角色:

Master
node.master: true 节点可以作为主节点
DataNode
node.data: true 默认是数据节点。
Coordinate node 协调节点
如果仅担任协调节点,将上两个配置设为false。

说明:

一个节点可以充当一个或多个角色,默认三个角色都有

协调节点:一个节点只作为接收请求、转发请求到其他节点、汇总各个节点返回数据等功能的节点。就叫协调节点

2.2 如何分配:

A. 小规模集群,不需严格区分。
B. 中大规模集群(十个以上节点),应考虑单独的角色充当。特别并发查询量大,查询的合并量大,可以增加独立的协调节点。角色分开的好处是分工分开,不互影响。如不会因协调角色负载过高而影响数据节点的能力。

3、如何避免脑裂问题?

3.1 脑裂问题:

一个集群中只有一个A主节点,A主节点因为需要处理的东西太多或者网络过于繁忙,从而导致其他从节点ping不通A主节点,这样其他从节点就会认为A主节点不可用了,就会重新选出一个新的主节点B。过了一会A主节点恢复正常了,这样就出现了两个主节点,导致一部分数据来源于A主节点,另外一部分数据来源于B主节点,出现数据不一致问题,这就是脑裂

3.2 尽量避免脑裂,需要添加最小数量的主节点配置:

discovery.zen.minimum_master_nodes:(有master资格节点数/2) + 1

这个参数控制的是,选举主节点时需要看到最少多少个具有master资格的活节点,才能进行选举。官方的推荐值是(N/2)+1,其中N是具有master资格的节点的数量。

3.3 常用做法(中大规模集群):

  1. MasterdataNode 角色分开,配置奇数个master,如3
  2. 单播发现机制,配置master资格节点:

discovery.zen.ping.multicast.enabled: false —— 关闭多播发现机制,默认是关闭的

discovery.zen.ping.unicast.hosts: [“master1”, “master2”, “master3”] —— 配置单播发现的主节点ip地址,其他从节点要加入进来,就得去询问单播发现机制里面配置的主节点我要加入到集群里面了,主节点同意以后才能加入,然后主节点再通知集群中的其他节点有新节点加入

3. 配置选举发现数,及延长ping master的等待时长

discovery.zen.ping_timeout: 30(默认值是3秒)——其他节点ping主节点多久时间没有响应就认为主节点不可用了
discovery.zen.minimum_master_nodes: 2 —— 选举主节点时需要看到最少多少个具有master资格的活节点,才能进行选举

4、索引应该设置多少个分片?

说明:分片数指定后不可变,除非重索引。

思考:

  • 分片对应的存储实体是什么?

答:存储的实体是索引

  • 分片是不是越多越好?

答:不是

  • 分片多有什么影响?

答:分片多浪费存储空间、占用资源、影响性能

4.1 分片过多的影响:

每个分片本质上就是一个Lucene索引, 因此会消耗相应的文件句柄, 内存和CPU资源。
每个搜索请求会调度到索引的每个分片中. 如果分片分散在不同的节点倒是问题不太. 但当分片开始竞争相同的硬件资源时, 性能便会逐步下降。
ES使用词频统计来计算相关性. 当然这些统计也会分配到各个分片上. 如果在大量分片上只维护了很少的数据, 则将导致最终的文档相关性较差。

4.2 分片设置的可参考原则:

ElasticSearch推荐的最大JVM堆空间是30~32G, 所以把你的分片最大容量限制为30GB, 然后再对分片数量做合理估算. 例如, 你认为你的数据能达到200GB, 推荐你最多分配7到8个分片。
在开始阶段, 一个好的方案是根据你的节点数量按照1.5~3倍的原则来创建分片. 例如,如果你有3个节点, 则推荐你创建的分片数最多不超过9(3x3)个。当性能下降时,增加节点,ES会平衡分片的放置。
对于基于日期的索引需求, 并且对索引数据的搜索场景非常少. 也许这些索引量将达到成百上千, 但每个索引的数据量只有1GB甚至更小. 对于这种类似场景, 建议只需要为索引分配1个分片。如日志管理就是一个日期的索引需求,日期索引会很多,但每个索引存放的日志数据量就很少。

5、分片应该设置几个副本?

说明:副本数是可以随时调整的!

思考:

  • 副本的用途是什么?

答:备份数据保证高可用数据不丢失,高并发的时候参与数据查询

  • 针对它的用途,我们该如何设置它的副本数?

答:一般一个分片有1-2个副本即可保证高可用

  • 集群规模没变的情况下副本过多会有什么影响?

答:副本多浪费存储空间、占用资源、影响性能

5.1 副本设置基本原则:

为保证高可用,副本数设置为2即可。要求集群至少要有3个节点,来分开存放主分片、副本。
如发现并发量大时,查询性能会下降,可增加副本数,来提升并发查询能力。

注意:新增副本时主节点会自动协调,然后拷贝数据到新增的副本节点

二、集群搭建

1. 准备3台虚拟机:
这三台虚拟机我都事先配置好了环境变量,所以直接可以用hdp-1代替ip;
hdp-1:192.168.183.131 hdp-2:192.168.183.132 hdp-3:192.168.183.133

2. 在3台虚拟机里面都安装好elasticsearch

安装教程参考我之前写的文章的ES的安装和配置部分:

3. 修改3台虚拟机下ES的配置,使得它们组成一个集群

进入elasticsearch的config目录,修改elasticsearch.yml的配置

3.1. IP访问限制、默认端口修改9200

这里有两个需要提醒下,第一个就是IP访问限制,第二个就是es实例的默认端口号9200。IP访问限制可以限定具体的IP访问服务器,这有一定的安全过滤作用。

# Set the bind address to a specific IP (IPv4 or IPv6): 
# 
network.host: hdp-1

说明:这里我配置了环境(配置环境网上也有很多),所以用hdp-1就可以,如果你还没有配置的话,直接换成第一台虚拟机的端口号。

如果设置成0.0.0.0则是不限制任何IP访问。一般在生产的服务器可能会限定几台IP,通常用于管理使用。

默认的端口9200在一般情况下也有点风险,可以将默认的端口修改成另外一个,这还有一个原因就是怕开发人员误操作,连接上集群。当然,如果你的公司网络隔离做的很好也无所谓。

# 
# Set a custom port for HTTP: 
# 
http.port: 9200 
transport.tcp.port: 9300

这里的9300是集群内部通讯使用的端口,这个也可以修改掉。因为连接集群的方式有两种,通过扮演集群node也是可以进入集群的,所以还是安全起见,修改掉默认的端口。

说明:记得修改安装了ES的3台虚拟机(三个节点)的相同配置,要不然节点之间无法建立连接工作,也会报错。

3.2 集群发现IP列表、node、cluster名称

紧接着修改集群节点IP地址,这样可以让集群在规定的几个节点之间工作。elasticsearch,默认是使用自动发现IP机制。就是在当前网段内,只要能被自动感知到的IP就能自动加入到集群中。这有好处也有坏处。好处就是自动化了,当你的es集群需要云化的时候就会非常方便。但是也会带来一些不稳定的情况,如,master的选举问题、数据复制问题。

导致master选举的因素之一就是集群有节点进入。当数据复制发生的时候也会影响集群,因为要做数据平衡复制和冗余。这里面可以独立master集群,剔除master集群的数据节点能力。

固定列表的IP发现有两种配置方式,一种是互相依赖发现,一种是全量发现。各有优势吧,我是使用的依赖发现来做的。这有个很重要的参考标准,就是你的集群扩展速度有多快。因为这有个问题就是,当全量发现的时候,如果是初始化集群会有很大的问题,就是master全局会很长,然后节点之间的启动速度各不一样。所以我采用了靠谱点的依赖发现。

你需要在192.168.183.131的elasticsearch中配置成:

# --------------------------------- 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]"] 
# 
discovery.zen.ping.unicast.hosts: [ "hdp-2:9300","hdp-3:9300" ]

让他去发现hdp-2,hdp-3的机器,以此内推,完成剩下的hdp-2和hdp-3机器的配置。

然后你需要配置下集群名称,就是你当前节点所在集群的名称,这有助于你规划你的集群。集群中的所有节点的集群名称必须一样,只有集群名称一样才能组成一个逻辑集群。

# ---------------------------------- Cluster ----------------------------------- 
# 
# Use a descriptive name for your cluster: 
# 
cluster.name: escluster 

配置你当前节点的名称

# 
# ------------------------------------ Node ------------------------------------ 
# 
# Use a descriptive name for the node: 
# 
node.name: es1

以此类推,完成另外两个节点的配置。cluster.name的名称必须保持一样。然后分别设置node.name。

说明:

这里搭建的是一个简单的集群,没有做集群节点角色的区分,所以3个节点默认的角色有主节点、数据节点、协调节点

选举ES主节点的逻辑:

选举的大概逻辑,它会根据分片的数据的前后新鲜程度来作为选举的一个重要逻辑。(日志、数据、时间都会作为集群master全局的重要指标)

因为考虑到数据一致性问题,当然是用最新的数据节点作为master,然后进行新数据的复制和刷新其他node。

这里我将三台虚拟机的配置文件贴出来方便参考:

# ======================== 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: escluster
#
# ------------------------------------ Node ------------------------------------
#
# Use a descriptive name for the node:
#
  node.name: es1
  node.master: true
#
# 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/zpark/esdata/data
#
# Path to log files:
#
  path.logs: /home/zpark/esdata/logs
#
# ----------------------------------- Memory -----------------------------------
#
# Lock the memory on startup:
#
  bootstrap.memory_lock: 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):
#
  network.host: hdp-1
#
# 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 new node is started:
# The default list of hosts is ["127.0.0.1", "[::1]"]
#
  transport.tcp.port: 9300
  discovery.zen.ping.unicast.hosts: ["hdp-2:9300","hdp-3:9300"]
#
# Prevent the "split brain" by configuring the majority of nodes (total number of master-eligible nodes / 2 + 1):
#
  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
#
# For more information, consult the gateway module documentation.
#
# ---------------------------------- Various -----------------------------------
#
# Require explicit names when deleting indices:
#
#action.destructive_requires_name: true
# ======================== 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: escluster
#
# ------------------------------------ Node ------------------------------------
#
# Use a descriptive name for the node:
#
  node.name: es2
  node.master: true
#
# 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/zpark/esdata/data
#
# Path to log files:
#
  path.logs: /home/zpark/esdata/logs
#
# ----------------------------------- Memory -----------------------------------
#
# Lock the memory on startup:
#
  bootstrap.memory_lock: 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):
#
  network.host: hdp-2
#
# 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 new node is started:
# The default list of hosts is ["127.0.0.1", "[::1]"]
#
  transport.tcp.port: 9300
  discovery.zen.ping.unicast.hosts: ["hdp-1:9301","hdp-3:9303"]
#
# Prevent the "split brain" by configuring the majority of nodes (total number of master-eligible nodes / 2 + 1):
#
  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
#
# For more information, consult the gateway module documentation.
#
# ---------------------------------- Various -----------------------------------
#
# Require explicit names when deleting indices:
#
#action.destructive_requires_name: true
# ======================== 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: escluster
#
# ------------------------------------ Node ------------------------------------
#
# Use a descriptive name for the node:
#
  node.name: es3
  node.master: true
#
# 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/zpark/esdata/data
#
# Path to log files:
#
  path.logs: /home/zpark/esdata/logs
#
# ----------------------------------- Memory -----------------------------------
#
# Lock the memory on startup:
#
  bootstrap.memory_lock: 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):
#
  network.host: hdp-3
#
# 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 new node is started:
# The default list of hosts is ["192.168.183.133", "[::1]"]
#
  transport.tcp.port: 9300
  discovery.zen.ping.unicast.hosts: ["hdp-1:9301", "hdp-2:9302"]
#
# Prevent the "split brain" by configuring the majority of nodes (total number of master-eligible nodes / 2 + 1):
#
  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
#
# For more information, consult the gateway module documentation.
#
# ---------------------------------- Various -----------------------------------
#
# Require explicit names when deleting indices:
#
#action.destructive_requires_name: true

三、集群管理

以下推荐三种验证es集群的方法:

1. 查看ES集群健康状态

http://hdp-1:9200/_cluster/health?pretty

在这里插入图片描述说明:

`{
“cluster_name” : “escluster”,
“status” : “green”, //集群的状态红绿灯,绿:健康,黄:亚健康,红:病态
“timed_out” : false,
“number_of_nodes” : 2, //节点数
“number_of_data_nodes” : 2, //数据节点数
“active_primary_shards” : 6, //分片数,6个Index库
“active_shards” : 12,
“relocating_shards” : 0,
“initializing_shards” : 0,
“unassigned_shards” : 0 //未指定节点,配置了复本,仅使用一台机器部署会出现这种情况
}

2.监控API

a.随便一台虚拟机输入一下命令,即可查看:
在这里插入图片描述
b.打开一个网页也是可以检测的:

http://hdp-1:9200/_cat
在这里插入图片描述参考文章:
集群搭建:https://www.cnblogs.com/jstarseven/p/6803054.html 这一篇文章写得比较详细,同时还总结了搭建过程中遇到的问题,搭建ES集群的话强烈推荐

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值