1、Elastic Stack简介
Elasticsearch的组成
Elasticsearch
Elasticsearch 基于java,是个开源分布式搜索引擎,它的特点有:分布式,零配置,自动发现,索引自动分片,索引,副本机制,restful风格接口,多数据源,自动搜索负载等。
Logstash
Logstash 基于java,是一个开源的用于收集,分析和存储日志的工具。
Kibana
Kibana 基于nodejs,也是一个开源和免费的工具,Kibana可以为 Logstash 和 ElasticSearch 提供的日志分析友好的 Web 界面,可以汇总、分析和搜索重要数据日志。
2、Elasticsearch
2.1、简介
官网:https://www.elastic.co/cn/products/elasticsearch
Elasticsearch是一个基于Lucene的搜索服务器,它提供了一哥分布式多用户能力的搜索引擎,基于RESTful web接口。Elasticsearch是用java开发的,并作为Apache许可条款下的开放源码发布,是当前流行的企业级搜索引擎,设计用于云计算中,能够达到实时搜索,稳定,可靠。快速,安装使用方便
安装
2.2.1、版本说明
Elasticsearch的发簪是非常快速的,所以在ES5.0之前。ELK的各个版本都不统一,出现了版本号混乱的状态,所以从5.0开始,所有的Elastic Stack中的项目全部统一版本号
2.2.2、下载
地址:https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-7.9.0-linux-x86_64.tar.gz
2.2.3、单机版安装
#创建elsearch用户,Elasticsearch不支持root用户运行
[root@master ~]# useradd elsearch
[root@master ~]# su - elsearch
#解压安装包
[elsearch@master ~]# cd /home/elsearch/
[elsearch@master ~]# mkidr es
[elsearch@master ~]# pwd /home/elsearch/es
[elsearch@master ~]# tar -xvf elasticsearch-7.9.0-linux-x86_64.tar.gz -C /home/elsearch/es
[elsearch@master ~]# cd /home/elsearch/es
#修改配置文件
vim conf/elasticsearch.yml
network.host: 0.0.0.0 #设置ip地址,任意网络均可访问
#说明:在Elasticsearch中如果,network.host不是localhost或者127.0.0.1的话,就会认为是生产环境,
会对环境的要求比较高,我们的测试环境不一定能够满足,一般情况下需要修改2处配置,如下:
#1:修改jvm启动参数
vim conf/jvm.options
-Xms128m #根据自己机器情况修改
-Xmx128m
#2:一个进程在VMAs(虚拟内存区域)创建内存映射最大数量
vim /etc/sysctl.conf
vm.max_map_count=655360
sysctl -p #配置生效
#启动ES服务
./bin/elasticsearch 或 ./bin/elasticsearch -d #后台启动
#通过访问进行测试,看到如下信息,就说明ES启动成功了
{
"name" : "master",
"cluster_name" : "elasticsearch",
"cluster_uuid" : "h0RP1efSSveaKjgtXgndvw",
"version" : {
"number" : "7.9.0",
"build_flavor" : "default",
"build_type" : "tar",
"build_hash" : "a479a2a7fce0389512d6a9361301708b92dff667",
"build_date" : "2020-08-11T21:36:48.204330Z",
"build_snapshot" : false,
"lucene_version" : "8.6.0",
"minimum_wire_compatibility_version" : "6.8.0",
"minimum_index_compatibility_version" : "6.0.0-beta1"
},
"tagline" : "You Know, for Search"
}
#停止服务
[elsearch@master ~]# jps
68709 Jps
68072 Elasticsearch
[elsearch@master ~]# kill 68072 #通过kill结束进程
#启动出错,环境:
[1]: max file descriptors [4096] for elasticsearch process is too low, increase to at
least [65536]
#解决:切换到root用户,编辑limits.conf 添加类似如下内容
vi /etc/security/limits.conf
添加如下内容:
* soft nofile 65536
* hard nofile 131072
* soft nproc 4096
* hard nproc 4096
*
[2]: max number of threads [1024] for user [elsearch] is too low, increase to at least
[4096]
#解决:切换到root用户,进入limits.d目录下修改配置文件。
vi /etc/security/limits.d/90-nproc.conf
#修改如下内容:
* soft nproc 1024
#修改为
* soft nproc 4096
[3]: 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.0默认bootstrap.system_call_filter为true
vim config/elasticsearch.yml
添加:
bootstrap.system_call_filter: false
注意:修改玩之后需要退出当前用户,再重新登录进行重启
目录信息
2.2.4、安装elasticsearch-head
由于ES官方并没有为ES提供界面管理工具,仅仅是提供了后台的服务。elasticsearch-head是一个为ES开发的一个页 面客户端工具,其源码托管于GitHub,地址为:https://github.com/mobz/elasticsearch-head
2.2.5、head提供了4种安装方式:
源码安装,通过npm run start启动(不推荐) 通过docker安装(推荐) 通过chrome插件安装(推荐) 通过ES的plugin方式安装(不推荐)
2.2.6、通过docker安装
#拉取镜像
[root@es es]# docker pull mobz/elasticsearch-head:5
#创建容器
[root@es es]# docker run -d --name elasticsearch-head -p 9100:9100 mobz/elasticsearch-head:5
通过浏览器进行访问:192.168.241.50:9100
注意:
由于前后端分离开发,所以会存在跨域问题,需要在服务端做CORS的配置,如下:
vim elasticsearch.yml
http.cors.enabled: true
http.cors.allow-origin: "*"
基本概念
2.2.7、索引
索引(index)是Elasticsearch对逻辑数据的逻辑存储,所以它可以分为更小的部分。可以把索引看成关系型数据库的表,索引的结构是为快速有效的全文索引准备的,特别是它不存储原始值。Elasticsearch可以把索引存放在一台机器或者分散在多台服务器上,每个索引有一或多个分片(shard),每个分片可以有多个副本(replica) 映射 所有文档写进索引之前都会先进行分析,如何将输入的文本分割为词条、哪些词条又会被过滤,这种行为叫做映射(mapping)。一般由用户自己定义规则
使用Postman工具进行操作
2.2.8、创建空索引
2.2.9、插入数据
2.3、更新数据
2.3.1、删除数据
DELETE /haoke/user/1001
3、Filebeat
架构
3.1、安装
#下载:
https://www.elastic.co/downloads/beats
#创建工作目录:
[root@logstash ~]# mkdir /root/filebeat
#进入工作目录:
[root@logstash ~]# cd /root/filebeat
#解压
[root@logstash filebeat]# tar -xf filebeat-7.9.0-linux-x86_64.tar.gz
#进入文件夹
[root@logstash filebeat]# cd filebeat-7.9.0-linux-x86_64
3.2、创建如下配置文件
vim test.yml
filebeat.inputs:
- type: stdin
enabled: true
setup.template.settings:
index.number_of_shards: 3
output.console:
pretty: true
enable: true
3.3、启动filebeat
[root@logstash filebeat-7.9.0-linux-x86_64]# ./filebeat -e -c test.yml
输入hello运行结果如下
###### 3.4、读取日志文件
编辑yml
vim path-log.yml
filebeat.inputs:
- type: log
enabled: true
paths:
- /root/filebeat/logs/*.log
setup.template.settings:
index.number_of_shards: 3
output.console:
pretty: true
enabel: true
启动filebeat
[root@logstash filebeat-7.9.0-linux-x86_64]# ./filebeat -e -c path-log.yml
3.5、自定义字段
编辑yml文件
filebeat.inputs:
- type: log
enabled: true
paths:
- /root/filebeat/logs/*.log
tags: ["log"] 添加自定义tag
fileds:
from: log
fileds_under_root: true //true为添加到根节点,false为添加到子节点
setup.template.settings:
index.number_of_shards:
output.console:
pretty: true
enable: true
3.6、输出到Elasticsearch
编辑yml文件
filebeat.inputs:
- type: log
enabled: true
paths:
- /root/filebeat/logs/*.log
tags: ["test"]
fileds:
from: test
setup.template.settings:
index.number_of_shards: 3
output.elasticsearch:
hosts: ["192.168.241.50"]
在日志文件中输入新的内容进行测试
3.7、Filebeat工作原理
Filebeat由两个主要组件组成:prospector 和 harvester harvester 负责读取单个文件的内容。 如果文件在读取时被删除或重命名,Filebeat将继续读取文件 prospector prospector 负责管理harvester并找到所有要读取的文件来源 如果输入类型为日志,则查找器将查找路径匹配的所有文件,并为每个文件启动一个harvester Filebeat目前支持两种prospector类型:log和stdin Filebeat如何保持文件的状态 Filebeat 保存每个文件的状态并经常将状态刷新到磁盘上的注册文件中。该状态用于记住harvester正在读取的最后偏移量,并确保发送所有日志行。如果输出(例如Elasticsearch或Logstash)无法访问,Filebeat会跟踪最后发送的行,并在输出再次可用时继续读取文件
3.8、Module
前面要想实现日志数据的读取以及处理都是自己手动配置的,其实,在Filebeat中,有大量的Module,可以简化我们的配置,直接就可以使用,如下
./filebeat modules list
Enabled:
nginx
Disabled:
activemq
apache
auditd
aws
azure
barracuda
bluecoat
cef
checkpoint
cisco
citrix
coredns
crowdstrike
cylance
elasticsearch
envoyproxy
f5
fortinet
googlecloud
gsuite
haproxy
ibmmq
icinga
iis
imperva
infoblox
iptables
juniper
kafka
kaspersky
kibana
logstash
microsoft
misp
mongodb
mssql
mysql
nats
netflow
netscout
o365
okta
osquery
panw
postgresql
rabbitmq
radware
rapid7
redis
santa
sonicwall
sophos
squid
suricata
system
tenable
tomcat
traefik
zeek
zscaler
可以看到,内置了很多的module,但是都没有启用,如果需要启用需要进行enable操作
./filebeat modules enable nginx #启动
./filebeat modules disable nginx #禁用
nginx module 配置
- module: nginx
# Access logs
access:
enabled: true
var.paths: ["/usr/local/nginx/logs/access.log*"]
# Set custom paths for the log files. If left empty,
# Filebeat will choose the paths depending on your OS.
#var.paths:
# Error logs
error:
enabled: true
var.paths: ["/usr/local/nginx/logs/error.log*"]
配置filebeat
vim modues-nginx.yml
filebeat.inputs:
setup.template.settings:
index.number_of_shards: 3
output.elasticsearch:
hosts: ["192.168.241.50"]
filebeat.config.modules:
path: ${path.config}/modules.d/*.yml
reload.enabled: false
4、Kibana
Kibana 是一款开源的数据分析和可视化平台,它是 Elastic Stack 成员之一,设计用于和 Elasticsearch 协作。您可以使用 Kibana 对 Elasticsearch 索引中的数据进行搜索、查看、交互操作。您可以很方便的利用图表、表格及地图对数据进行多元化的分析和呈现 官网:https://www.elastic.co/cn/products/kibana
4.1、配置安装
#创建工作目录
[root@kibana ~]# mkdir /root/kibana
#下载kibana
[root@kibana kibana]# wget https://artifacts.elastic.co/downloads/kibana/kibana-7.9.0-linux-x86_64.tar.gz
#解压
[root@kibana kibana]# tar -xv kibana-7.9.0-linux-x86_64.tar.gz
#修改配置文
[root@kibana kibana-7.9.0-linux-x86_64]# vim config/kibana.yml
server.host: "192.168.40.133" #对外暴露服务的地址
elasticsearch.url: "http://192.168.40.133:9200" #配置Elasticsear
#启动
[root@kibana kibana-7.9.0-linux-x86_64]# ./bin/kibana
#通过浏览器进行访问
http://192.168.241.52:5601
4.2、功能说明
4.3、数据探索
首先先添加索引信息 即可查看索引数据
4.4、自定义图表
在Kibana中,也可以进行自定义图表,如制作柱形图
4.5、开发者工具
在Kibana中,为开发者的测试提供了便捷的工具使用,如下
5、Logstash
用途
5.1、部署安装
#检查jdk环境,要求jdk1.8
[root@kibana ~]# java -version
#解压安装包
[root@kibana ~]# tar -xvf logstash-7.9.0.tar.gz
第一个logstash示例
[root@kibana ~]# bin/logstash -e’input { stdin { } } output { stdout
5.2、配置详解
Logstash的配置有三部分,如下
input { #输入
stdin { ... } #标准输入
}
filte { #过滤,对数据进行分割、截取等
......
}
output { #输出
stdout { ... } #标准输出
}
5.3、输入
5.4、过滤
5.5、输出
5.6、读取自定义日志
日志结构
2019-03-1521:21:21|ERROR|读取数据出错|参数:id=1002
可以看到,日志中的内容是使用“|”进行分割的,使用,我们在处理的时候,也需要对数据做分割处理
编写配置文件
vim logstash-pipeline.conf
input {
file {
path => "/itcast/logstash/logs/app.log"
start_position => "beginning
}
}
filter {
mutate {
split => {"message"=>"|"}
}
}
output {
stdout { codec => rubydebug }
}
启动测试
./bin/logstash -f logstash-pipeline.conf
写日志到文件
echo"2019-03-15 21:21:21|ERROR|读取数据出错|参数:id=1002" >> app.log
输出的结果
5.6、输出到Elasticsearch
input {
file {
path => "/itcast/logstash/logs/app.log"
start_position => "beginning
}
}
filter {
mutate {
split => {"message"=>"|"}
}
}
output {
elasticsearch {
hosts => [ "192.168.241.50:9200"]
}
}