ELK集群搭建
基本注意事项和要求
- 首先环境一定要一样
Jdk版本一定要一样- 明确设置那个节点是主节点,那个节点是数据节点node.master属性(主节点属性设置为true,数据节点设置为false,当然主节点也可以存储数据)
- xpack.ml.enabled: false
如果不需要使用machine learning功能,则可以在elasticsearch.yml中设置禁用:
异常org.elasticsearch.transport.RemoteTransportException: Failed to deserialize exception response from stream
原因:ElasticSearch节点之间的jdk版本不一致
异常org.elasticsearch.cluster.coordination.CoordinationStateRejectedException: join validation on cluster state with a different cluster uuid DGeDa4wNQ7OnyZyxWJRS_w than local cluster uuid u5-SNBcDR3mQHj0Vm-whVQ, rejecting
原因两个独立的集群 没法加入一个集群 应该删除对应的data数据(就是yml里面对应的data和logs里面的数据,重启服务)
异常[node-2] master not discovered yet: have discovered [{node-1}
原因:要初始化master,在E的yml配置文件中,如下
cluster.initial_master_nodes: [“192.168.1.3”]
Unsupported major.minor version 52.0
原因:jdk版本太低
bin/elasticsearch-plugin install license ERROR: Unknown plugin license
原因:ElasticSearch5.0.0以后插件命令已经改变
解决方案:bin/elasticsearch-plugin install x-pack
启动异常:ERROR: bootstrap checks failed system call filters failed to install; check the logs and fix your configuration or disable system call filters at your own risk
原因:因为Centos6不支持SecComp,而ES5.2.1默认bootstrap.system_call_filter为true进行检测,所以导致检测失败,失败后直接导致ES不能启动。详见 :https://github.com/elastic/elasticsearch/issues/22899
解决方案:在elasticsearch.yml中配置bootstrap.system_call_filter为false,注意要在Memory下面
bootstrap.memory_lock: false
bootstrap.system_call_filter: false
具体Elasticsearch配置文件如下
# ======================== 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
#cluster.name: cui
#
# ------------------------------------ Node ------------------------------------
#
# Use a descriptive name for the node:
#
#node.name: node-1
#
node.name: node-2
node.master: false
node.data: 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: /path/to/data
#
# Path to log files:
#
#path.logs: /path/to/logs
#
path.data: /home/cui/elk/software/elasticsearch-7.1.1/data/to/data1,/home/cui/elk/software/elasticsearch-7.1.1/data/to/data2
path.logs: /home/cui/elk/software/elasticsearch-7.1.1/data/logs
# ----------------------------------- Memory -----------------------------------
#
# Lock the memory on startup:
#
#bootstrap.memory_lock: true
#
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.
#
#indices.cache.filter.size: 20%
#index.cache.field.max_size: 50000
#index.cache.field.expire: 10m
#index.cache.field.type: soft
#
xpack.ml.enabled: false
# ---------------------------------- Network -----------------------------------
#
# Set the bind address to a specific IP (IPv4 or IPv6):
#
#network.host: 192.168.0.1
#
# Set a custom port for HTTP:
#
#http.port: 9200
#
# For more information, consult the network module documentation.
#
network.host: 0.0.0.0
http.port: 9200
network.bind_host: 0.0.0.0 #只有本机可以访问http接口
network.publish_host: 192.168.1.4
transport.tcp.compress: true
transport.tcp.port: 9300
#使用head等插件监控集群信息,需要打开以下配置项 ###########
http.cors.enabled: true
http.cors.allow-origin: "*"
http.cors.allow-credentials: true
#
# --------------------------------- 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.1.3","192.168.1.4"]
#
# Bootstrap the cluster using an initial set of master-eligible nodes:
#
#cluster.initial_master_nodes: ["node-1", "node-2"]
#
cluster.initial_master_nodes: ["192.168.1.3"]
discovery.zen.ping.unicast.hosts: ["192.168.1.3","192.168.1.4"]
discovery.zen.minimum_master_nodes: 1
#
# 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
配置文件
- config/elasticsearch.yml 主配置文件
- config/jvm.options jvm参数配置文件
- cofnig/log4j2.properties 日志配置文件
基本概念
接近实时(NRT)
• Elasticsearch 是一个接近实时的搜索平台。这意味着,从索引一个文档直到这个文档能够被搜索到有一个很小的延迟(通常是 1 秒)。
集群(cluster)
代表一个集群,集群中有多个节点(node),其中有一个为主节点,这个主节点是可以通过选举产生的,主从节点是对于集群内部来说的。es的一个概念就是去中心化,字面上理解就是无中心节点,这是对于集群外部来说的,因为从外部来看es集群,在逻辑上是个整体,你与任何一个节点的通信和与整个es集群通信是等价的。
节点(node)
1.节点是属于集群一部分的单个服务器,存储数据并参与集群的索引和搜索功能。就像一个集群一样,一个节点由一个名称来标识,默认情况下该名称是在启动时分配给节点的随机通用唯一标识符(UUID)。如果您不需要默认值,您可以定义任何您想要的节点名称。此名称对于管理目的非常重要,您需要确定网络中的哪些服务器与Elasticsearch群集中的哪些节点相对应。
2.可以将节点配置为按集群名称加入特定集群。默认情况下,每个节点都设置为加入一个名为的群集elasticsearch,这意味着如果启动网络中的多个节点并假设它们可以互相发现,它们将自动形成并加入一个名为的群集elasticsearch。
3.在单个群集中,您可以拥有任意数量的节点。此外,如果网络上当前没有其他Elasticsearch节点正在运行,则默认情况下启动单个节点将形成名为的新单节点群集elasticsearch。
索引(index)
ElasticSearch将它的数据存储在一个或多个索引(index)中。用SQL领域的术语来类比,索引就像数据库,可以向索引写入文档或者从索引中读取文档,并通过ElasticSearch内部使用Lucene将数据写入索引或从索引中检索数据。
文档(document)
文档(document)是ElasticSearch中的主要实体。对所有使用ElasticSearch的案例来说,他们最终都可以归结为对文档的搜索。文档由字段构成。
数据路由(documnet routing)
当客户端发起创建document的时候,es需要确定这个document放在该index哪个shard上。这个过程就是数据路由。
路由过程:
路由算法:shard = hash(routing) % number_of_primary_shards
routing:每次增删改查一个document的时候,都会带过来一个routing number,他的默认值就是这个document的_id(可能是手动指定,也可能是自动生成)routing = _id,所以决定一个document在哪个shard上,最重要的一个值就是routing值。相同的routing值,从hash函数中,产出的hash值一定是相同的。
number_of_primary_shards:主分片。
例如:假设 _id=1会将这个routing值,传入一个hash函数中,产出一个routing值的hash值,hash(routing) = 21,然后将hash函数产出的值对这个index的primaryshard的数量求余数,21 % 3 = 0 就决定了,这个document就放在P0上。
手动指定routing:PUT /index/type/id?routing=user_id
手动指定routing value,可以保证某一类document一定被路由到一个shard上去,那么在后续进行应用级别的负载均衡以及提升批量读取的性能的时候,是很有帮助的。
映射(mapping)
所有文档写进索引之前都会先进行分析,如何将输入的文本分割为词条、哪些词条又会被过滤,这种行为叫做映射(mapping)。一般由用户自己定义规则。
类型(type)
每个文档都有与之对应的类型(type)定义。这允许用户在一个索引中存储多种文档类型,并为不同文档提供类型提供不同的映射。
分片(shards)
代表索引分片,es可以把一个完整的索引分成多个分片,这样的好处是可以把一个大的索引拆分成多个,分布到不同的节点上。构成分布式搜索。分片的数量只能在索引创建前指定,并且索引创建后不能更改。5.X默认不能通过配置文件定义分片
primary shard数量不可变
假如我们的集群在初始化的时候有5个primary shard,我们望里边加入一个document id=5,假如hash(5)=23,这时该document 将被加入 (shard=23%5=3)P3这个分片上。如果随后我们给es集群添加一个primary shard ,此时就有6个primary shard,当我们GET id=5 ,这条数据的时候,es会计算该请求的路由信息找到存储他的 primary shard(shard=23%6=5) ,根据计算结果定位到P5分片上。而我们的数据在P3上。所以es集群无法添加primary shard,但是可以扩展replicas shard。
副本(replicas)
代表索引副本,es可以设置多个索引的副本,副本的作用一是提高系统的容错性,当个某个节点某个分片损坏或丢失时可以从副本中恢复。二是提高es的查询效率,es会自动对搜索请求进行负载均衡。
数据恢复(recovery)
代表数据恢复或叫数据重新分布,es在有节点加入或退出时会根据机器的负载对索引分片进行重新分配,挂掉的节点重新启动时也会进行数据恢复。
GET /_cat/health?v #可以看到集群状态
数据源(River)
代表es的一个数据源,也是其它存储方式(如:数据库)同步数据到es的一个方法。它是以插件方式存在的一个es服务,通过读取river中的数据并把它索引到es中,官方的river有couchDB的,RabbitMQ的,Twitter的,Wikipedia的,river这个功能将会在后面的文件中重点说到。
网关(gateway)
代表es索引的持久化存储方式,es默认是先把索引存放到内存中,当内存满了时再持久化到硬盘。当这个es集群关闭再重新启动时就会从gateway中读取索引数据。es支持多种类型的gateway,有本地文件系统(默认),分布式文件系统,Hadoop的HDFS和amazon的s3云存储服务。
自动发现(discovery.zen)
代表es的自动发现节点机制,es是一个基于p2p的系统,它先通过广播寻找存在的节点,再通过多播协议来进行节点之间的通信,同时也支持点对点的交互。
5.X关闭广播,需要自定义
通信(Transport)
代表es内部节点或集群与客户端的交互方式,默认内部是使用tcp协议进行交互,同时它支持http协议(json格式)、thrift、servlet、memcached、zeroMQ等的传输协议(通过插件方式集成)。
节点间通信端口默认:9300-9400
分片和复制(shards and replicas)
一个索引可以存储超出单个结点硬件限制的大量数据。比如,一个具有10亿文档的索引占据1TB的磁盘空间,而任一节点可能没有这样大的磁盘空间来存储或者单个节点处理搜索请求,响应会太慢。
为了解决这个问题,Elasticsearch提供了将索引划分成多片的能力,这些片叫做分片。当你创建一个索引的时候,你可以指定你想要的分片的数量。每个分片本身也是一个功能完善并且独立的“索引”,这个“索引” 可以被放置到集群中的任何节点上。
分片之所以重要,主要有两方面的原因:
• 允许你水平分割/扩展你的内容容量
• 允许你在分片(位于多个节点上)之上进行分布式的、并行的操作,进而提高性能/吞吐量
至于一个分片怎样分布,它的文档怎样聚合回搜索请求,是完全由Elasticsearch管理的,对于作为用户的你来说,这些都是透明的。
在一个网络/云的环境里,失败随时都可能发生。在某个分片/节点因为某些原因处于离线状态或者消失的情况下,故障转移机制是非常有用且强烈推荐的。为此, Elasticsearch允许你创建分片的一份或多份拷贝,这些拷贝叫做复制分片,或者直接叫复制。
复制之所以重要,有两个主要原因:
• 在分片/节点失败的情况下,复制提供了高可用性。复制分片不与原/主要分片置于同一节点上是非常重要的。因为搜索可以在所有的复制上并行运行,复制可以扩展你的搜索量/吞吐量
• 总之,每个索引可以被分成多个分片。一个索引也可以被复制0次(即没有复制) 或多次。一旦复制了,每个索引就有了主分片(作为复制源的分片)和复制分片(主分片的拷贝)。
• 分片和复制的数量可以在索引创建的时候指定。在索引创建之后,你可以在任何时候动态地改变复制的数量,但是你不能再改变分片的数量。
• 5.X默认5:1 5个主分片,1个复制分片
默认情况下,Elasticsearch中的每个索引分配5个主分片和1个复制。这意味着,如果你的集群中至少有两个节点,你的索引将会有5个主分片和另外5个复制分片(1个完全拷贝),这样每个索引总共就有10个分片。
document增删改的处理流程
假如我们es集群有3个node,每个node上一个主分片一个复制分片。
1、客户端发起一个PUT请求,假如该请求被发送到第一个node节点,那么该节点将成为协调节点(coordinating node),如图P1所在的节点就是协调节点。他将根据该请求的路由信息计算,该document将被存储到哪个分片。
2、第二步 通过计算发现该document被存储到p0分片,那么就将请求转发到node2节点。
3、第三步 P0根据请求信息创建document,和相应的索引信息,创建完毕后将信息同步到自己的副本节点R0上。
4、第四步 P0和R0将通知我们的协调节点,任务完成情况。
5、第五部 协调节点响应客户端最终的处理结果。
document读请求流程
1、第一步客户端发送读器请求到协调节点(coordinate node)。
2、第二步协调节点(coordinate node)根据请求信息对document进行路由计算,将请求转发到对应的node,node2 或者node3。 此时会使用round-robin随机轮询算法,在primary shard以及其所有replica(副本)中随机选择一个,让读请求负载均衡。
3、第三步相应接收到请求的节点(node2或者node3)将处理结果返回给协调节点(coordinate node)。
4、协调节点将最终的结果反馈给客户端。
注意:document如果还在建立索引过程中,可能只有primary shard有,任何一个replica shard都没有,此时请求如果作用到replica shard上可能会导致无法读取到document信息。但是document完成索引建立之后,primary shard和replica shard就都有了。
elasticsearch . yml 配置文件说明
1. ##################### Elasticsearch Configuration Example #####################
2. #
3. # 只是挑些重要的配置选项进行注释,其实自带的已经有非常细致的英文注释了!
4. # https://www.elastic.co/guide/en/elasticsearch/reference/current/modules.html
5. #
6. ################################### Cluster ###################################
7. # 代表一个集群,集群中有多个节点,其中有一个为主节点,这个主节点是可以通过选举产生的,主从节点是对于集群内部来说的.
8. # es的一个概念就是去中心化,字面上理解就是无中心节点,这是对于集群外部来说的,因为从外部来看es集群,在逻辑上是个整体,你与任何一个节点的通信和与整个es集群通信是等价的。
9. # cluster.name可以确定你的集群名称,当你的elasticsearch集群在同一个网段中elasticsearch会自动的找到具有相同cluster.name的elasticsearch服务.
10. # 所以当同一个网段具有多个elasticsearch集群时cluster.name就成为同一个集群的标识.
11.
12. # cluster.name: elasticsearch
13.
14. #################################### Node #####################################
15. # https://www.elastic.co/guide/en/elasticsearch/reference/5.1/modules-node.html#master-node
16. # 节点名称同理,可自动生成也可手动配置.
17. # node.name: node-1
18.
19. # 允许一个节点是否可以成为一个master节点,es是默认集群中的第一台机器为master,如果这台机器停止就会重新选举master.
20. # node.master: true
21.
22. # 允许该节点存储数据(默认开启)
23. # node.data: true
24.
25. # 配置文件中给出了三种配置高性能集群拓扑结构的模式,如下:
26. # 1. 如果你想让节点从不选举为主节点,只用来存储数据,可作为负载器
27. # node.master: false
28. # node.data: true
29. # node.ingest: true #默认true
30.
31. # 2. 如果想让节点成为主节点,且不存储任何数据,并保有空闲资源,可作为协调器
32. # node.master: true
33. # node.data: false
34. # node.ingest: true
35.
36. # 3. 如果想让节点既不称为主节点,又不成为数据节点,那么可将他作为搜索器,从节点中获取数据,生成搜索结果等
37. # node.master: false
38. # node.data: false
39. # node.ingest: true
40. #
41.
42. # 4. 仅作为协调器
43. # node.master: false
44. # node.data: false
45. # node.ingest: false
46.
47. # 监控集群状态有一下插件和API可以使用:
48. # Use the Cluster Health API [http://localhost:9200/_cluster/health], the
49. # Node Info API [http://localhost:9200/_nodes] or GUI tools # such as <http://www.elasticsearch.org/overview/marvel/>,
50.
51.
52. # A node can have generic attributes associated with it, which can later be used
53. # for customized shard allocation filtering, or allocation awareness. An attribute
54. # is a simple key value pair, similar to node.key: value, here is an example:
55. # 每个节点都可以定义一些与之关联的通用属性,用于后期集群进行碎片分配时的过滤
56. # node.rack: rack314
57.
58. # 默认情况下,多个节点可以在同一个安装路径启动,如果你想让你的es只启动一个节点,可以进行如下设置
59. # node.max_local_storage_nodes: 1
60.
61. #################################### Index ####################################
62. # 设置索引的分片数,默认为5
63. #index.number_of_shards: 5
64.
65. # 设置索引的副本数,默认为1:
66. #index.number_of_replicas: 1
67.
68. # 配置文件中提到的最佳实践是,如果服务器够多,可以将分片提高,尽量将数据平均分布到大集群中去
69. # 同时,如果增加副本数量可以有效的提高搜索性能
70. # 需要注意的是,"number_of_shards" 是索引创建后一次生成的,后续不可更改设置
71. # "number_of_replicas" 是可以通过API去实时修改设置的
72.
73. #################################### Paths ####################################
74. # 配置文件存储位置
75. # path.conf: /path/to/conf
76.
77. # 数据存储位置(单个目录设置)
78. # path.data: /path/to/data
79. # 多个数据存储位置,有利于性能提升
80. # path.data: /path/to/data1,/path/to/data2
81.
82. # 临时文件的路径
83. # path.work: /path/to/work
84.
85. # 日志文件的路径
86. # path.logs: /path/to/logs
87.
88. # 插件安装路径
89. # path.plugins: /path/to/plugins
90.
91. #################################### Plugin ###################################
92. # 设置插件作为启动条件,如果一下插件没有安装,则该节点服务不会启动
93. # plugin.mandatory: mapper-attachments,lang-groovy
94.
95. ################################### Memory ####################################
96. # 当JVM开始写入交换空间时(swapping)ElasticSearch性能会低下,你应该保证它不会写入交换空间
97. # 设置这个属性为true来锁定内存,同时也要允许elasticsearch的进程可以锁住内存,linux下可以通过 `ulimit -l unlimited` 命令
98. # bootstrap.mlockall: true
99.
100. # 确保 ES_MIN_MEM 和 ES_MAX_MEM 环境变量设置为相同的值,以及机器有足够的内存分配给Elasticsearch
101. # 注意:内存也不是越大越好,一般64位机器,最大分配内存别才超过32G
102.
103. ############################## Network And HTTP ###############################
104. # 设置绑定的ip地址,可以是ipv4或ipv6的,默认为0.0.0.0
105. # network.bind_host: 192.168.0.1 #只有本机可以访问http接口
106.
107. # 设置其它节点和该节点交互的ip地址,如果不设置它会自动设置,值必须是个真实的ip地址
108. # network.publish_host: 192.168.0.1
109.
110. # 同时设置bind_host和publish_host上面两个参数
111. # network.host: 192.168.0.1 #绑定监听IP
112.
113. # 设置节点间交互的tcp端口,默认是9300
114. # transport.tcp.port: 9300
115.
116. # 设置是否压缩tcp传输时的数据,默认为false,不压缩
117. # transport.tcp.compress: true
118.
119. # 设置对外服务的http端口,默认为9200
120. # http.port: 9200
121.
122. # 设置请求内容的最大容量,默认100mb
123. # http.max_content_length: 100mb
124.
125. # 使用http协议对外提供服务,默认为true,开启
126. # http.enabled: false
127.
128. ###################### 使用head等插件监控集群信息,需要打开以下配置项 ###########
129. # http.cors.enabled: true
130. # http.cors.allow-origin: "*"
131. # http.cors.allow-credentials: true
132.
133. ################################### Gateway ###################################
134. # gateway的类型,默认为local即为本地文件系统,可以设置为本地文件系统
135. # gateway.type: local
136.
137. # 下面的配置控制怎样以及何时启动一整个集群重启的初始化恢复过程
138. # (当使用shard gateway时,是为了尽可能的重用local data(本地数据))
139.
140. # 一个集群中的N个节点启动后,才允许进行恢复处理
141. # gateway.recover_after_nodes: 1
142.
143. # 设置初始化恢复过程的超时时间,超时时间从上一个配置中配置的N个节点启动后算起
144. # gateway.recover_after_time: 5m
145.
146. # 设置这个集群中期望有多少个节点.一旦这N个节点启动(并且recover_after_nodes也符合),
147. # 立即开始恢复过程(不等待recover_after_time超时)
148. # gateway.expected_nodes: 2
149.
150. ############################# Recovery Throttling #############################
151. # 下面这些配置允许在初始化恢复,副本分配,再平衡,或者添加和删除节点时控制节点间的分片分配
152. # 设置一个节点的并行恢复数
153. # 1.初始化数据恢复时,并发恢复线程的个数,默认为4
154. # cluster.routing.allocation.node_initial_primaries_recoveries: 4
155.
156. # 2.添加删除节点或负载均衡时并发恢复线程的个数,默认为2
157. # cluster.routing.allocation.node_concurrent_recoveries: 2
158.
159. # 设置恢复时的吞吐量(例如:100mb,默认为0无限制.如果机器还有其他业务在跑的话还是限制一下的好)
160. # indices.recovery.max_bytes_per_sec: 20mb
161.
162. # 设置来限制从其它分片恢复数据时最大同时打开并发流的个数,默认为5
163. # indices.recovery.concurrent_streams: 5
164. # 注意: 合理的设置以上参数能有效的提高集群节点的数据恢复以及初始化速度
165.
166. ################################## Discovery ##################################
167. # 设置这个参数来保证集群中的节点可以知道其它N个有master资格的节点.默认为1,对于大的集群来说,可以设置大一点的值(2-4)
168. # discovery.zen.minimum_master_nodes: 1
169. # 探查的超时时间,默认3秒,提高一点以应对网络不好的时候,防止脑裂
170. # discovery.zen.ping.timeout: 3s
171.
172. # For more information, see
173. # <http://elasticsearch.org/guide/en/elasticsearch/reference/current/modules-discovery-zen.html>
174.
175. # 设置是否打开多播发现节点.默认是true.
176. # 当多播不可用或者集群跨网段的时候集群通信还是用单播吧
177. # discovery.zen.ping.multicast.enabled: false
178.
179. # 这是一个集群中的主节点的初始列表,当节点(主节点或者数据节点)启动时使用这个列表进行探测
180. # discovery.zen.ping.unicast.hosts: ["host1", "host2:port"]
181.
182. # Slow Log部分与GC log部分略,不过可以通过相关日志优化搜索查询速度
183.
184. ################ X-Pack ###########################################
185. # 官方插件 相关设置请查看此处
186. # https://www.elastic.co/guide/en/x-pack/current/xpack-settings.html
187. #
188. ############## Memory(重点需要调优的部分) ################
189. # Cache部分:
190. # es有很多种方式来缓存其内部与索引有关的数据.其中包括filter cache
191.
192. # filter cache部分:
193. # filter cache是用来缓存filters的结果的.默认的cache type是node type.node type的机制是所有的索引内部的分片共享filter cache.node type采用的方式是LRU方式.即:当缓存达到了某个临界值之后,es会将最近没有使用的数据清除出filter cache.使让新的数据进入es.
194.
195. # 这个临界值的设置方法如下:indices.cache.filter.size 值类型:eg.:512mb 20%。默认的值是10%。
196.
197. # out of memory错误避免过于频繁的查询时集群假死
198. # 1.设置es的缓存类型为Soft Reference,它的主要特点是据有较强的引用功能.只有当内存不够的时候,才进行回收这类内存,因此在内存足够的时候,它们通常不被回收.另外,这些引用对象还能保证在Java抛出OutOfMemory异常之前,被设置为null.它可以用于实现一些常用图片的缓存,实现Cache的功能,保证最大限度的使用内存而不引起OutOfMemory.在es的配置文件加上index.cache.field.type: soft即可.
199.
200. # 2.设置es最大缓存数据条数和缓存失效时间,通过设置index.cache.field.max_size: 50000来把缓存field的最大值设置为50000,设置index.cache.field.expire: 10m把过期时间设置成10分钟.
201. # index.cache.field.max_size: 50000
202. # index.cache.field.expire: 10m
203. # index.cache.field.type: soft
204.
205. # field data部分&&circuit breaker部分:
206. # 用于fielddata缓存的内存数量,主要用于当使用排序,faceting操作时,elasticsearch会将一些热点数据加载到内存中来提供给客户端访问,但是这种缓存是比较珍贵的,所以对它进行合理的设置.
207.
208. # 可以使用值:eg:50mb 或者 30%(节点 node heap内存量),默认是:unbounded #indices.fielddata.cache.size: unbounded
209. # field的超时时间.默认是-1,可以设置的值类型: 5m #indices.fielddata.cache.expire: -1
210.
211. # circuit breaker部分:
212. # 断路器是elasticsearch为了防止内存溢出的一种操作,每一种circuit breaker都可以指定一个内存界限触发此操作,这种circuit breaker的设定有一个最高级别的设定:indices.breaker.total.limit 默认值是JVM heap的70%.当内存达到这个数量的时候会触发内存回收
213.
214. # 另外还有两组子设置:
215. #indices.breaker.fielddata.limit:当系统发现fielddata的数量达到一定数量时会触发内存回收.默认值是JVM heap的70%
216. #indices.breaker.fielddata.overhead:在系统要加载fielddata时会进行预先估计,当系统发现要加载进内存的值超过limit * overhead时会进行进行内存回收.默认是1.03
217. #indices.breaker.request.limit:这种断路器是elasticsearch为了防止OOM(内存溢出),在每次请求数据时设定了一个固定的内存数量.默认值是40%
218. #indices.breaker.request.overhead:同上,也是elasticsearch在发送请求时设定的一个预估系数,用来防止内存溢出.默认值是1
219.
220. # Translog部分:
221. # 每一个分片(shard)都有一个transaction log或者是与它有关的预写日志,(write log),在es进行索引(index)或者删除(delete)操作时会将没有提交的数据记录在translog之中,当进行flush 操作的时候会将tranlog中的数据发送给Lucene进行相关的操作.一次flush操作的发生基于如下的几个配置
222. #index.translog.flush_threshold_ops:当发生多少次操作时进行一次flush.默认是 unlimited #index.translog.flush_threshold_size:当translog的大小达到此值时会进行一次flush操作.默认是512mb
223. #index.translog.flush_threshold_period:在指定的时间间隔内如果没有进行flush操作,会进行一次强制flush操作.默认是30m #index.translog.interval:多少时间间隔内会检查一次translog,来进行一次flush操作.es会随机的在这个值到这个值的2倍大小之间进行一次操作,默认是5s
224. #index.gateway.local.sync:多少时间进行一次的写磁盘操作,默认是5s
225.
226. # 以上的translog配置都可以通过API进行动态的设置 - See more at: http://bigbo.github.io/pages/2015/04/10/elasticsearch_config/#sthash.AvOSUcQ4.dpuf
集群操作(查看是否部署好集群等)
如果通过head插件查看,只需要在一台机器上部署head插件即可,就可以看见整个集群的全部机器信息
部署好集群可以通过curl命令行方式查看集群机器信息
curl http://197.255.20.215:9200/_cat/nodes?v
有一点,当master已经启动,两个slave还没有启动的时候,会报一个错:
[haozz_master] master not discovered or elected yet, an election requires at least 2 nodes with ids from
大意是说,master没有被发现或被选举,至少要有两个slave选举它。这个在两个slave启动后就正常了。
另外,启动的时候可能会报错:
master not discovered yet, this node has not previously joined a bootstrapped (v7+) cluster
这个时候需要在elasticsearch.yml中修改配置:
cluster.initial_master_nodes: [“haozz_master”]
你可以通过为cluster.initial_master_nodes参数设置一系列符合主节点条件的节点的主机名或IP地址来引导启动集群。你可以在命令行或elasticsearch.yml中提供这些信息。你还需要配置发现子系统,这样节点就知道如何找到彼此。
如果未设置initial_master_nodes,那么在启动新节点时会尝试发现已有的集群。如果节点找不到可以加入的集群,将定期记录警告消息。
我之前一直是在这个数组配置里面把master和两个slave都加上,结果启动还是报错,后来去掉两个slave,只留master就好了。这个应该是v7.0+的性能特性,网上不太好找解决办法,困扰了比较久,可以参考:https://discuss.elastic.co/t/master-not-discovered-yet-this-node-has-not-previously-joined-a-bootstrapped-v7-cluster/176304。
Filebat传输到Elasticsearch配置
下载filebeat解压
进入filebeat目录,配置filebeat.yml
具体亲测文件如下
###################### Filebeat Configuration Example #########################
# This file is an example configuration file highlighting only the most common
# options. The filebeat.reference.yml file from the same directory contains all the
# supported options with more comments. You can use it as a reference.
#
# You can find the full configuration reference here:
# https://www.elastic.co/guide/en/beats/filebeat/index.html
# For more available modules and options, please see the filebeat.reference.yml sample
# configuration file.
#=========================== Filebeat inputs =============================
filebeat.inputs:
# Each - is an input. Most options can be set at the input level, so
# you can use different inputs for various configurations.
# Below are the input specific configurations.
- type: log
# Change to true to enable this input configuration.
enabled: true
# Paths that should be crawled and fetched. Glob based paths.
paths:
- /home/cui/software/dataLogFile/*.log
#- c:\programdata\elasticsearch\logs\*
registry_file: /home/cui/software/filebeat-7.1.1-linux-x86_64/data/registry
#ignore_older: 1m
#clean_inactive: 3m
close_eof: false
#tail_files: true
# Exclude lines. A list of regular expressions to match. It drops the lines that are
# matching any regular expression from the list.
#exclude_lines: ['^DBG']
# Include lines. A list of regular expressions to match. It exports the lines that are
# matching any regular expression from the list.
#include_lines: ['^ERR', '^WARN']
# Exclude files. A list of regular expressions to match. Filebeat drops the files that
# are matching any regular expression from the list. By default, no files are dropped.
#exclude_files: ['.gz$']
# Optional additional fields. These fields can be freely picked
# to add additional information to the crawled log files for filtering
#fields:
# level: debug
# review: 1
### Multiline options
# Multiline can be used for log messages spanning multiple lines. This is common
# for Java Stack Traces or C-Line Continuation
# The regexp Pattern that has to be matched. The example pattern matches all lines starting with [
#multiline.pattern: ^\[
# Defines if the pattern set under pattern should be negated or not. Default is false.
#multiline.negate: false
# Match can be set to "after" or "before". It is used to define if lines should be append to a pattern
# that was (not) matched before or after or as long as a pattern is not matched based on negate.
# Note: After is the equivalent to previous and before is the equivalent to to next in Logstash
#multiline.match: after
#============================= Filebeat modules ===============================
#filebeat.config.modules:
# Glob pattern for configuration loading
# path: ${path.config}/modules.d/*.yml
# Set to true to enable config reloading
#reload.enabled: false
# Period on which files under path should be checked for changes
#reload.period: 10s
#==================== Elasticsearch template setting ==========================
setup.template.name: "grib2"
setup.template.pattern: "grib2*"
#setup.template.fields: "fields.yml"
setup.template.enabled: true
setup.template.overwrite: true
setup.ilm.enabled: auto
setup.ilm.rollover_alias: "grib2"
setup.ilm.pattern: "{now/d}-000001"
setup.template.settings:
index.number_of_shards: 2
index.number_of_replicas: 1
#setup.template.settings.index.lifecycle.rollover_alias: "grib2"
#setup.template.settings.index.lifecycle.name: "beats-default-policy"
#setup.template.name: "cuiTemplete"
#setup.template.fields: "/home/cui/software/filebeat-7.1.1-linux-x86_64/fields.yml"
#setup.template.pattern: "cuiTemplete*"
#index.codec: best_compression
#_source.enabled: false
#================================ General =====================================
# The name of the shipper that publishes the network data. It can be used to group
# all the transactions sent by a single shipper in the web interface.
#name:
# The tags of the shipper are included in their own field with each
# transaction published.
#tags: ["service-X", "web-tier"]
# Optional fields that you can specify to add additional information to the
# output.
#fields:
# env: staging
#============================== Dashboards =====================================
# These settings control loading the sample dashboards to the Kibana index. Loading
# the dashboards is disabled by default and can be enabled either by setting the
# options here or by using the `setup` command.
setup.dashboards.enabled: false
# The URL from where to download the dashboards archive. By default this URL
# has a value which is computed based on the Beat name and version. For released
# versions, this URL points to the dashboard archive on the artifacts.elastic.co
# website.
#setup.dashboards.url:
#============================== Kibana =====================================
# Starting with Beats version 6.0.0, the dashboards are loaded via the Kibana API.
# This requires a Kibana endpoint configuration.
setup.kibana:
# Kibana Host
# Scheme and port can be left out and will be set to the default (http and 5601)
# In case you specify and additional path, the scheme is required: http://localhost:5601/path
# IPv6 addresses should always be defined as: https://[2001:db8::1]:5601
host: "192.168.1.3:5601"
# Kibana Space ID
# ID of the Kibana Space into which the dashboards should be loaded. By default,
# the Default Space will be used.
#space.id:
#============================= Elastic Cloud ==================================
# These settings simplify using filebeat with the Elastic Cloud (https://cloud.elastic.co/).
# The cloud.id setting overwrites the `output.elasticsearch.hosts` and
# `setup.kibana.host` options.
# You can find the `cloud.id` in the Elastic Cloud web UI.
#cloud.id:
# The cloud.auth setting overwrites the `output.elasticsearch.username` and
# `output.elasticsearch.password` settings. The format is `<user>:<pass>`.
#cloud.auth:
#================================ Outputs =====================================
# Configure what output to use when sending the data collected by the beat.
#-------------------------- Elasticsearch output ------------------------------
output.elasticsearch:
# Array of hosts to connect to.
hosts: ["192.168.1.3:9200"]
enabled: true
pipeline: "grib2"
index: "grib2-cui"
#index: "grib2-%{+yyyy.MM.dd}"
# Optional protocol and basic auth credentials.
#protocol: "https"
#username: "elastic"
#password: "changeme"
#----------------------------- Logstash output --------------------------------
#output.logstash:
# The Logstash hosts
#hosts: ["localhost:5044"]
# Optional SSL. By default is off.
# List of root certificates for HTTPS server verifications
#ssl.certificate_authorities: ["/etc/pki/root/ca.pem"]
# Certificate for SSL client authentication
#ssl.certificate: "/etc/pki/client/cert.pem"
# Client Certificate Key
#ssl.key: "/etc/pki/client/cert.key"
#================================ Processors =====================================
# Configure processors to enhance or manipulate events generated by the beat.
processors:
- add_host_metadata: ~
- add_cloud_metadata: ~
#================================ Logging =====================================
# Sets log level. The default log level is info.
# Available log levels are: error, warning, info, debug
#logging.level: debug
# At debug level, you can selectively enable logging only for some components.
# To enable all selectors use ["*"]. Examples of other selectors are "beat",
# "publish", "service".
#logging.selectors: ["*"]
#============================== Xpack Monitoring ===============================
# filebeat can export internal metrics to a central Elasticsearch monitoring
# cluster. This requires xpack monitoring to be enabled in Elasticsearch. The
# reporting is disabled by default.
# Set to true to enable the monitoring reporter.
#xpack.monitoring.enabled: false
# Uncomment to send the metrics to Elasticsearch. Most settings from the
# Elasticsearch output are accepted here as well. Any setting that is not set is
# automatically inherited from the Elasticsearch output configuration, so if you
# have the Elasticsearch output configured, you can simply uncomment the
# following line.
#xpack.monitoring.elasticsearch:
#================================= Migration ==================================
# This allows to enable 6.7 migration aliases
#migration.6_to_7.enabled: true
Filebeat数据转换到Elasticsearch对应的列,用Elasticsearch管道转换(pipeline)在filebeat.yml中Elasticsearch template setting来设置
配置一个grok文件
如grib2-pipeline.json
{
"description" : "test",
"processors" : [
{
"grok" :{
"field" : "message",
"patterns" :["%{NUMBER:client} %{NUMBER:method} %{NUMBER:request} %{NUMBER:bytes} %{NUMBER:duration}"
]
}
},
{
"remove":{
"field":"client"
}
}
]
}
之后将管道添加到对应服务器中的Elasticsearch中(需要存储数据的每个服务器都配置,因为数据不一定发送到集群中的哪一个服务器)
之后将管道添加到对应服务器中的Elasticsearch中(需要存储数据的每个服务器都配置,因为数据不一定发送到集群中的哪一个服务器)
命令是:
curl -H 'Content-Type: application/json' -XPUT 'http://192.168.1.3:9200/_ingest/pipeline/grib2' -d @/home/cui/software/pipelineJson/grib2-pipeline.json
删除管道API按ID或通配符匹配(my-,)删除管道。
DELETE _ingest / pipeline / my-pipeline-id
设置index别名和生命周期
setup.ilm.enabled: auto
setup.ilm.rollover_alias: “grib2”
setup.ilm.pattern: “{now/d}-000001”
参考官方地址:https://www.elastic.co/guide/en/beats/filebeat/7.1/ilm.html
索引生命周期管理:https://www.elastic.co/guide/en/elasticsearch/reference/7.1/index-lifecycle-management-api.html
启动filebeat
命令是:
./filebeat -e -c filebeat.yml -d “publish”
后台启动示例(是例子,具体自己改动,如下)
nohup ./filebeat -c ack.yml &
-c 指定配置文件;nohup & 后台运行;
Grok Debugger调试
http://grokdebug.herokuapp.com/
filebeat+elasticsearch从massage中提取字段
由于性能问题,采用filebeat+elasticsearch+kiban的架构,该架构中不使用logstash,也就无法进行传统的字段过滤,比如将massage中的信息进行过滤,提出新的字段。但是在elasticsearch 5.0版本以后,elasticsearch中添加了Ingest Node功能,以前如果需要对数据进行加工,都是在索引之前进行处理,比如logstash可以对日志进行结构化和转换,现在直接在es就可以处理了,目前es提供了一些常用的诸如convert、grok之类的处理器,在使用的时候,先定义一个pipeline管道,里面设置文档的加工逻辑,在建索引的时候指定pipeline名称,那么这个索引就会按照预先定义好的pipeline来处理了。
示例如下:
在filebeat.yml中设置
filebeat.prospectors:
- input_type: log
- /work/1.log
output.elasticsearch:
hosts: ["localhost:9200"]
pipeline: "test-pipeline"
在/work下创建pipeline.json文件:
{
"description" : "test-pipeline",
"processors" : [
{
"grok" :{
"field" : "message",
"patterns" :["%{IP:client} %{WORD:method} %{URIPATHPARAM:request} %{NUMBER:bytes} %{NUMBER:duration}"
]
}
}
]
}
将pipeline导入elasticsearch中
curl -XPUT 'http://localhost:9200/_ingest/pipeline/test-pipeline' -d@/work/pipeline.json
在/work下创建文件1.log,内容为:
55.3.244.1 GET /index.html 15824 0.043
输出结果为:
{
"_index": "filebeat-2017.03.15",
"_type": "log",
"_id": "AVrQ6Jg-W7NyrdbKiTgl",
"_score": null,
"_source": {
"request": "/index.html",
"offset": 73,
"method": "GET",
"input_type": "log",
"source": "/work/1.log",
"message": "55.3.244.1 GET /index.html 15824 0.043",
"type": "log",
"duration": "0.043",
"@timestamp": "2017-03-15T07:39:48.649Z",
"bytes": "15824",
"beat": {
"hostname": "xia-VirtualBox",
"name": "xia-VirtualBox",
"version": "5.2.2"
},
"client": "55.3.244.1"
},
"fields": {
"@timestamp": [
1489563588649
]
},
"sort": [
1489563588649
]
}
这样massage中的字段就被提取出来成为了新的字段。
参考资料: https://www.elastic.co/guide/en/beats/filebeat/current/configuring-ingest-node.html
可以在Kibana操作的命令
查询集群健康程度
GET / _cat / health?v
每当我们要求群集健康时,我们要么获得绿色,黄色或红色。
• 绿色 - 一切都很好(集群功能齐全)
• 黄色 - 所有数据都可用,但尚未分配一些副本(群集功能齐全)
• 红色 - 某些数据由于某种原因不可用(群集部分功能)
查询集群节点列表
GET / _cat / nodes?v
可以看出是单个节点还是集群,还可以就看见谁是主节点
列出所有的指数
GET / _cat / indices?v