文章目录
前言
ELK 是elastic公司提供的一套完整的日志收集以及展示的解决方案,是三个产品的首字母缩写,分别是ElasticSearch、Logstash 和 Kibana。这三个产品被设计成一个集成解决方案,称为“Elastic Stack”(以前称为“ELK stack”)。从5.0开始后,Elastic Stack项目全部统一版本号。
官方网站:https://www.elastic.co/products
一、ELK是什么?
ElasticSearch简称ES,基于java,它是一个实时的分布式搜索和分析引擎,它可以用于全文搜索,结构化搜索以及分析。它是一个建立在全文搜索引擎 Apache Lucene 基础上的搜索引擎,使用 Java 语言编写。
Logstash是一个具有实时传输能力的数据收集引擎,基于java,用来进行数据收集(如:读取文本文件)、解析,并将数据发送给ES。
Kibana为 Elasticsearch 提供了分析和可视化的 Web 平台。基于nodejs,它可以在 Elasticsearch 的索引中查找,交互数据,并生成各种维度表格、图形。
Beats 是一个免费且开放的平台,集合了多种单一用途数据采集器。它们从成百上千或成千上万台机器和系统向 Logstash 或 Elasticsearch 发送数据。
如图:Logstash收集AppServer产生的Log,并存放到ElasticSearch集群中,而Kibana则从ES集群中查询数据生成图表,再返回给Browser。
Elastic官方网站下载https://www.elastic.co/cn/downloads/
二、ElasticSearch使用步骤
2.1 elasticsearch下载安装
elasticsearch官网下载:
https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-7.9.1-linux-x86_64.tar.gz
#解压安装包
tar -zxvf elasticsearch-7.9.1-linux-x86_64.tar.gz -C /usr/share/
#增加用户
useradd elsearch
#修改文件属性
chown -R elsearch:elsearch elasticsearch-7.9.1/
2.2 elasticsearch修改配置
You can configure and update dynamic settings on a running cluster using the cluster update settings API. You can also configure dynamic settings locally on an unstarted or shut down node using elasticsearch.yml.
#设置IP地址,任意网络均可访问
vim config/elasticsearch.yml
---------------------
network.host: 0.0.0.0
---------------------
//注意这一行需空一格
#根据自己机器情况,设置最大,最小堆栈。
vim config/jvm.options
----------------
-Xms1g -Xms256m
-Xmx1g -Xmx256m
-----------------
#一个进程可以拥有的VMA(虚拟内存区域)的数量
vim /etc/sysctl.conf
-----------------------
vm.max_map_count=655360
-----------------------
sysctl -p //配置生效
2.3 elasticsearch启动服务
su elsearch
cd /usr/share/elasticsearch-7.9.1/bin
./elasticsearch //./elasticsearch -d 可后台启动
To run Elasticsearch as a daemon, specify -d on the command line, and record the process ID in a file using the -p option:
./bin/elasticsearch -d -p pid
启动bug见总结
查看启动端口 9200:ss -ntlp
访问本地9200端口 :curl 127.0.0.1:9200
2.4可视化工具之elasticsearch-head
可参考:
https://www.sojson.com/blog/85.html
通过docker安装
https://github.com/mobz/elasticsearch-head
通过chrome插件安装
1.谷歌浏览器访问http://extb.cqttech.com/search/elasticsearch%2520head
根据提示下载安装谷歌双核浏览器
2,安装elasticsearch-head插件
2.5 RESTful API
在Elasticsearch中提供了丰富的RESTful API的操作,包括基本的CRUD,创建索引,删除索引等操作。
2.6 IK Analysis for Elasticsearch
下载地址:https://github.com/medcl/elasticsearch-analysis-ik
(1)什么是IK分词器
分词:即把一段中文或者别的划分成一个个的关键字,我们在搜索时候会把自己的信息进行分词,会把数据库中或者索引库中的数据进行分词,然后进行一个匹配操作,默认的中文分词是将每个字看成一个词,
如果要使用中文,建议使用ik分词器!
IK提供了两个分词算法: ik_ smart 和ik_ max_word ,其中ik._smart 为最少切分! ik_max_word为最细粒度划分!
2.7 elasticsearch 基本概念
索引 (index)
索引是Elasticsearch对逻辑数据的逻辑存储。索引的结构是为快速有效的全文索引准备的。
相等于关系数据库中的表。
文档 (document)
Elasticsearch存储中的主要实体叫文档。和MongoDB中的文档类似,可以有不同结构。
相等于关系数据库中的一行记录。
映射(mapping)
文档在写入索引前都将被分析。哪些词条应该被过滤掉,或哪些附加处理有必要被调用。
存储分析链所需的所有信息。
节点 (node)
一个节点是一个Elasticsearch的实例
集群(cluster)
多个协同工作的Elasticsearch节点的集合被称为集群。
分片 (shard)
集群允许系统存储的数据总量超过单机容量。Elasticsearch将数据散布到多个物理的Lucene索引上去。这些Lucene索引被称为分片,Elasticsearch本身自动进行分片处理外,用户可为具体的应用进行参数调优。
三、Kibana使用步骤
3.1 Kibana安装配置
Kibana官网下载:
https://artifacts.elastic.co/downloads/kibana/kibana-7.9.1-linux-x86_64.tar.gz
#解压安装包
tar -zxvf kibana-7.9.1-linux-x86_64.tar.gz -C /usr/share/
#修改文件属性
chown -R elsearch:elsearch kibana-7.9.1-linux-x86_64/
//与elasticsearch文件属性保持一致,否则启动报错。见总结
#修改配置文件
vim config/kibana.yml
----------------------------------------------
#默认端口是5601,如需要改端口在此修改
server.port: 5601
#使其可以被外网访问,显示kibana界面
server.host: "10.37.6.26"
#elasticsearch的地址和端口
elasticsearch.hosts: ["http://10.37.6.26:9200"]
----------------------------------------------
Here is an example of changing the path of the data and logs directories:
path.data: /var/lib/elasticsearch
path.logs: /var/log/elasticsearch
3.2 kibana启动服务
cd /usr/share/kibana-7.9.1-linux-x86_64/bin
./kibana
启动bug见总结
启动成功时info:
查看启动端口 5601:ss -ntlp
浏览器打开:http://10.37.6.26:5601/
四、Filebeat使用步骤
filebeat 官网下载: https://artifacts.elastic.co/downloads/beats/filebeat/filebeat-7.9.1-linux-x86_64.tar.gz
filebeat 是Beats中的一种,作用如下:
4.1 Filebeat 安装
#解压安装包
tar -zxvf kibana-7.9.1-linux-x86_64.tar.gz
mv filebeat-7.9.1-linux-x86_64 /usr/share/
4.2Filebeat 配置启动
#创建配置文件test.yml
filebeat.inputs:
- type: stdin
enabled: true
output.console:
pretty: true
enable: true
./filebeat -e -c test.yml
输入message
4.3Filebeat 读取文件
新增test-log.yml配置文件
#修改inputs
filebeat.inputs:
- type: log
enabled: true
paths:
- /usr/share/filebeat-7.9.1-linux-x86_64/logs/*.log
//索引分区数
setup.template.settings:
index.number_of_shards: 3
output.console:
pretty: true
enable: true
启动 ./filebeat -e -c test-log.yml
,logs目录向文件输入 :echo "test" >> test.log
五、Logstash使用步骤
5.1Logstash下载安装
Logstash官网下载:
https://artifacts.elastic.co/downloads/logstash/logstash-7.9.1.tar.gz
#检测jdk环境,要求jdk1.8+
java -version
#解压安装包
tar -zxvf logstash-7.9.1.tar.gz -C /usr/share/
5.2Logstash启动测试
cd /usr/share/logstash-7.9.1/
//无filter配置 由输入到输出。
bin/logstash -e 'input { stdin { } } output { stdout {} }'
输入hello
5.3Logstash配置
//读取日志基本配置
input {
file {
path => "/usr/share/logstash-7.9.1/logs/test.log"
start_position => "beginning"
}
}
filter{
mutate{
split => {
"message"=>"|"}
}
}
//stdout 输出到屏幕
output{
stdout{
codec => "rubydebug"
}
}
//stdout elasticsearch
output {
elasticsearch {
hosts => "localhost:9200"
}
}
启动logstash: bin/logstash -f test.conf
手动测试:echo "2019-09-19|test|99" >>test.log
input{
file{
path=>"/usr/share/logstash-7.9.1/data/eve.json"
start_position=>"beginning"
sincedb_path=>"/dev/null"
codec=>json{
charset=>"ISO-8859-1"
}
}
}
output{
elasticsearch{
hosts=>"http://10.37.6.30:9200"
index=>"jsontestlogstash"
document_type=>"doc"
}
stdout{
}
}
六、Metricbeat
6.1 install Metricbeat
curl -L -O https://artifacts.elastic.co/downloads/beats/metricbeat/metricbeat-7.9.1-linux-x86_64.tar.gz
tar xzvf metricbeat-7.9.1-linux-x86_64.tar.gz
总结(部分章节bug及解释)
2.1.elasticsearch下载安装
useradd elsearch
:elasticsearch启动不能用root用户启动,否则启动报错:
“Caused by: java.lang.RuntimeException: can not run elasticsearch as root”
chown -R elsearch:elsearch elasticsearch-7.9.1/
:修改文件属性,否则启动报错:
“could not find java in bundled jdk at /usr/share/elasticsearch-7.9.1/jdk/bin/java”
2.2.elasticsearch修改配置
linux参数之max_map_count:
“This file contains the maximum number of memory map areas a process may have. Memory map areas are used as a side-effect of calling malloc, directly by mmap and mprotect, and also when loading shared libraries.While most applications need less than a thousand maps, certain programs, particularly malloc debuggers, may consume lots of them, e.g., up to one or two maps per allocation.the default value is 65536.”
max_map_count文件包含限制一个进程可以拥有的VMA(虚拟内存区域)的数量。虚拟内存区域是一个连续的虚拟地址空间区域。在进程的生命周期中,每当程序尝试在内存中映射文件,链接到共享内存段,或者分配堆空间的时候,这些区域将被创建。调优这个值将限制进程可拥有VMA的数量。限制一个进程拥有VMA的总数可能导致应用程序出错,因为当进程达到了VMA上线但又只能释放少量的内存给其他的内核进程使用时,操作系统会抛出内存不足的错误。如果你的操作系统在NORMAL区域仅占用少量的内存,那么调低这个值可以帮助释放内存给内核用
调整vm.max_map_count的大小
#查看当前值
sysctl -a|grep vm.max_map_count
#临时修改
sysctl -w vm.max_map_count=262144
#永久修改
vim /etc/sysctl.conf
-----------------------
vm.max_map_count=655360
-----------------------
#配置生效
sysctl -p
2.3.elasticsearch启动服务
“[1]: max file descriptors [4096] for elasticsearch process is too low, increase to at least [65535]”
每个进程最大同时打开文件数太小。
#查看当前数量
ulimit -Hn
ulimit -Sn
#增加配置
vim /etc/security/limits.conf
-------------------------------
* soft nofile 65536
* hard nofile 65536
-------------------------------
//注意可切换用户查看修改的配置
[1]类似:“max number of threads [3818] for user [es] is too low, increase to at least [4096]”
最大线程个数太低。
#查看
ulimit -Hu
ulimit -Su
#增加配置
vim /etc/security/limits.conf
--------------------------------------------
* soft nproc 4096
* hard nproc 4096
--------------------------------------------
[1]类似:“max virtual memory areas vm.max_map_count [65530] is too low, increase to at least [262144]”
见2.2.修改配置。
[2]: bootstrap checks failed:
“”the default discovery settings are unsuitable for production use; at least one of [discovery.seed_hosts, discovery.seed_providers, cluster.initial_master_nodes] must be configured”
config目录下,修改elasticsearch.yml配置文件
vim config/elasticsearch.yml
----------------------------------------
node.name: node-1 //没有此项也能启动elasticsearch 但kibana启动会检测.
----------------------------------------
cluster.initial_master_nodes: ["node-1"]
----------------------------------------
[3]:“Caused by: com.fasterxml.jackson.dataformat.yaml.snakeyaml.error.MarkedYAMLException: while parsing a block mapping in ‘reader’, line 55, column 1: network.host: 0.0.0.0”
elasticsearch.yml配置文件 network.host: 0.0.0.0 前空一格
3.2 kibana启动服务
[1]“Babel could not write cache to file: /usr/share/kibana-7.9.1-linux-x86_64/ optimize/
.babel_register_cache.json due to a permission issue. Cache is disabled.”
#修改文件属性 :与elasticsearch文件属性保持一致。
chown -R elsearch:elsearch kibana-7.9.1-linux-x86_64/
[2]“FATAL CLI ERROR YAMLException: end of the stream or a document separator is expected at line 32, column 1:elasticsearch.hosts: ”
#修改配置文件
kibana.yml配置文件 elasticsearch.hosts: ["http://10.37.6.26:9200"]前空一格
[3]http://10...*:5601/ 浏览器访问 页面出现:“Kibana server is not ready yet“
查看启动warning
[warning][savedobjects-service] Unable to connect to Elasticsearch. Error: Request Timeout after 30000ms
kibana.yml配置文件增加 elasticsearch.requestTimeout: 90000
[warning][savedobjects-service] Unable to connect to Elasticsearch. Error: [master_not_discovered_exception] null
elasticsearch.yml配置文件增加 node.name: node-1
kibana启动连接elasticsearch报错,config/elasticsearch.yml相关配置
cluster.name: my-application
node.name: node-1
network.host: 0.0.0.0
cluster.initial_master_nodes: ["node-1"]
//如果不删除node-2,会产生bootstrap checks failed错误
chown -R elastic
4.2Filebeat 启动测试
“What files do you want me to watch? Exiting: no modules or inputs enabled and configuration”
- type: stdin 少了一个小横岗 -
“Exiting: data path already locked by another beat. Please make sure that multiple beats are not sharing the same data path (path.data)”
ps -ef | grep filebeat
5.1Logstash下载安装
测试 bin/logstash -e 'input { stdin { } } output { stdout {} }'
提示:
“could not find java; set JAVA_HOME or ensure java is in PATH”
Logstash需要依赖对应版本的JDK环境, JDK安装可参考博文《Linux环境
Tomcat (含JDK)安装》 可配置elasticsearch下载中的java 14
5.3Logstash配置
“Expected one of [ \t\r\n], “#”, “if”, [A-Za-z0-9_-], ‘"’, “’”, “}” at line··”
注意空格,引号