1.elasticsearch是一个基于Lucene的搜索服务器。它提供了一个分布式多用户能力的全文搜索引擎,基于RESTful web接口。Elasticsearch是用Java语言开发的,并作为Apache许可条款下的开放源码发布,是一种流行的企业级搜索引擎;
2.filebeat:轻量型日志采集器,Beats中的一员,Beats在是一个轻量级日志采集器,其实Beats家族有6个成员,早期的ELK架构中使用Logstash收集、解析日志,但是Logstash对内存、cpu、io等资源消耗比较高。相比Logstash,Beats所占系统的CPU和内存几乎可以忽略不计。
3.kibana:一个与Elasticsearch协同工作的开源分析和可视化平台,Kibana 可以让你更方便地对 Elasticsearch 中数据进行操作,包括高级的数据分析以及在图表中可视化您的数据。
下载地址:https://elasticsearch.cn/download/
注:efk版本保持一致,但选择安装方式可不相同,本人elasticsearch+kibana用的是rpm方式安装,filebeat则是选择直接用安装包(因为rpm安装会出现即使配置了运行日志路径,也不会打印运行日志的问题,有可能是因为版本问题)
#需要先安装jdk(别用系统自带的openjdk)
1.安装elasticsearch
rpm -ivh elasticsearch-7.6.1.rpm
2.修改配置文件:vim /etc/elasticsearch/elasticsearch.yml
#修改运行日志和数据存放位置(如果默认的存储位置磁盘空间够大,也可以不用改) # ----------------------------------- Paths ------------------------------------ # # Path to directory where to store the data (separate multiple locations by comma): #数据存放位置 path.data: /var/lib/elasticsearch # # Path to log files: #运行日志存放位置 path.logs: /var/lib/elasticsearch #注:如果改了存放位置,如path.logs: /data/elasticsearch7.6.1/log/elasticsearch,则需要用“chown elasticsearch 文件夹名称”命令更改elasticsearch文件夹所属用户,否则会导致无法启动
#修改访问地址 # ---------------------------------- Network ----------------------------------- # # Set the bind address to a specific IP (IPv4 or IPv6): # network.host: 本机ip # # Set a custom port for HTTP: # http.port: 9200
#修改节点 # ---------------------------------- Cluster ----------------------------------- # # Use a descriptive name for your cluster: # cluster.name: my-application # # ------------------------------------ Node ------------------------------------ # # Use a descriptive name for the node: # node.name: node-1 # --------------------------------- 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: ["host1", "host2"] discovery.seed_hosts: ["127.0.0.1"] # # Bootstrap the cluster using an initial set of master-eligible nodes: # #cluster.initial_master_nodes: ["node-1", "node-2"] cluster.initial_master_nodes: ["node-1"]
3.启动服务
#重新加载服务配置文件 systemctl daemon-reload #将服务设置为每次开机启动 systemctl enable elasticsearch #启动服务 systemctl start elasticsearch #查看启动状态 systemctl status elasticsearch
4.查看是否正常:curl http://elasticsearchip:elasticsearch端口,出现以下画面说明elasticsearch正常启动
6.nginx配置(如果不需要,则不用配置)
location /elasticsearch/{
proxy_pass "http://ip:9200/";
#用于解决302重定向问题,这里
proxy_set_header Host $http_host;
proxy_read_timeout 300s;
proxy_set_header X-Forwarded-For $http_x_forwarded_for;
}
filebeat安装
1.安装
tar -xzf filebeat-7.6.1-linux-x86_64.tar.gz
2.配置环境变量
#编辑profie文件 vim /etc/profie #添加文件路径后保存 export PATH=$PATH:/usr/local/filebeat-7.6.1 #执行(只在当前终端生效) source /etc/profie 永久生效方法(可不使用该方法) a.vim ~/.bashrc b.在末尾添加如下代码并保存 if [ -f /etc/profile ]; then . /etc/profile fi
3.修改配置文件:vim filebeat.yml
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:
#日志路径
- /data/log/miniapp/info.log
fields:
#相当于一个标签
log_topics: "miniapp-log"
fields_under_root: true
#==================== Elasticsearch template setting ==========================
setup.template.settings:
index.number_of_shards: 1
#index.codec: best_compression
#_source.enabled: false
setup.template.name: "filebeat"
setup.template.pattern: "filebeat-*"
setup.ilm.enabled: false
#-------------------------- Elasticsearch output ------------------------------
output.elasticsearch:
# Array of hosts to connect to.
hosts: ["172.51.67.25:9200"]
#没有匹配到以下配置的索引时,存放到other-log索引
index: "other-log"
indices:
#指定存储索引(elasticsearch不存在该名称的索引时,会自动创建该索引)
- index: "miniapp-log-%{+yyyy-MM-dd}"
when.contains:
#匹配上面日志配置的log_topics,匹配上日志就会发送到该索引上去
log_topics: "miniapp-log"
4.启动filebeat
nohup filebeat -e -c filebeat文件夹路径/filebeat.yml >filebeat文件夹路径/tmp/filebeat.log 2>&1 &
5.查看运行日志
tail -f filebeat文件夹路径/tmp/filebeat.log
注:运行日志中harvester里可以看到读取到几个文件,重启filebeat后,期间新增的日志会在filebeat启动后发送到elasticsearch
1.安装
rpm -ivh kibana-7.6.1-x86_64.rpm
2.修改配置文件
#访问端口 server.port: 5601 #本机地址 server.host: 0.0.0.0 #代理路径(用nginx代理时该配置要跟随nginx配置的路径),配置时kibana所有访问路径会自动加上该路径 #如加载http://ip:端口/bundles/56.bundle.js会变成http://ip:端口/kibana/bundles/56.bundle.js server.basePath:"/kibana" #elasticsearch访问地址 elasticsearch.hosts: ["http://elasticsearch配置的ip:端口"]
3.启动服务
#重新加载服务配置文件 systemctl daemon-reload #将服务设置为每次开机启动 systemctl enable kibana #启动服务 systemctl start kibana #查看启动状态 systemctl status kibana
4.访问测试:curl http://ip:端口
#查看输出日志 systemctl status kibana.service
5.nginx配置
location /kibana/{
proxy_pass "http://kibana访问ip:端口/";
#用于解决302重定向问题,这里
proxy_set_header Host $http_host;
proxy_read_timeout 300s;
proxy_set_header X-Forwarded-For $http_x_forwarded_for;
}
6.索引模式配置:访问http://ip:端口/kibana/app/kibana
- 查看从elasticsearch获取到的索引(索引:相当于数据库)
- 配置索引模式(索引模式:告诉kibana你需要用到哪个索引或者哪些索引组合,比如filebeat按照时间传输日志到不同的elasticsearch索引中,此时可以用通配符来在几个索引中搜索你想要的日志)
7.设置不显示不想要的字段,默认显示的字段众多,这会导致查询日志时,显示的内容过于繁杂
8.日志搜索:搜索框内,不加引号为模糊搜索,加引号为精准搜索,搜索内容带引号要加转义符,与用and,或用or
注:因为每天会产生大量的日志,久了会因为索引太多而占用太多的磁盘空间,还影响响应速度,所以要额外增加定时任务来清理索引,后续再补充



