EFK架构介绍
EFK三个开源软件组成,分别是Elasticsearch、FileBeat和Kibana。这三个软件各自在日志管理和数据分析领域发挥着重要作用,它们之间互相配合使用,完美衔接,高效地满足了多种场合的应用需求,是目前主流的一种日志分析系统解决方案。
Elasticsearch:负责日志的保存和搜索。它是一个分布式、高扩展、高实时的搜索与数据分析引擎,基于Lucene开发,通过RESTful web接口提供全文搜索和分析功能。Elasticsearch能够高效地存储和索引各种类型的数据,并支持快速搜索和实时分析。FileBeat:负责日志的收集。它由Elastic公司开发,专为日志收集而设计,具有资源占用低、易于部署的特点。FileBeat直接从服务器上的日志文件读取数据,进行初步的归集和简单处理,然后将其转发到Elasticsearch或其他中间件。Kibana:负责日志数据的界面展示。它是一个开源的数据分析和可视化平台,提供丰富的数据可视化选项,如柱状图、线图、饼图等,帮助用户以图形化的方式理解数据。此外,它还支持强大的数据探索功能,用户可以使用Elasticsearch的查询语言进行数据查询和筛选。
架构图
EFK与ELK的对比
组件组成区别:EFK使用FileBeat作为日志收集工具,而ELK使用Logstash。FileBeat相对于Logstash来说,具有侵入低(无需修改Elasticsearch和Kibana的配置)和性能高(IO占用率比Logstash小)的优点。而Logstash则提供了更复杂的数据处理功能,适合复杂的数据清洗、转换和处理场景。资源消耗:由于Logstash提供了更复杂的数据处理功能,因此它通常比FileBeat在资源消耗上更高。这可能会增加应用服务器端的负载压力。而FileBeat则更加轻量级,资源占用更低。部署和扩展性:EFK和ELK都具有集群线性扩展的能力,可以通过增加节点来提高系统的处理能力和性能。然而,在部署和配置方面,两者可能有所不同,具体取决于所使用的软件和版本。适用场景:ELK适合需要复杂数据清洗、转换和处理的场景,特别是在数据预处理阶段需要较多自定义逻辑的场景。而EFK更适合轻量级的日志收集和分析场景,特别是在资源有限或需要快速部署的场景中。
基础环境准备
(1) 创建文件夹
mkdir /opt/es
(2)关闭防火墙
systemctl status firewalld
systemctl stop firewalld
setenforce 0
(3)修改主机名
hostnamectl set-hostname monitor_01
hostnamectl set-hostname monitor_02
(4)修改本地host文件(两台主机均添加)
vim /etc/hosts
echo "172.16.208.12 monitor-01" >> /etc/hosts
echo "172.16.208.13 monitor-02" >> /etc/hosts
(5)修改sshd服务优化
sed -ri 's/^#UseDNS yes/UseDNS no/g' /etc/ssh/sshd_config
sed -ri '/^GSSAPIAuthentication yes$/s/yes/no/' /etc/ssh/sshd_config
grep ^UseDNS /etc/ssh/sshd_config
grep ^GSSAPIAuthentication /etc/ssh/sshd_config
(6)在monitor_01节点生成密钥对
ssh-keygen -t rsa -P '' -f ~/.ssh/id_rsa -q
ll ~/.ssh/id_rsa
-rw-------. 1 root root 1675 Oct 16 15:37 /root/.ssh/id_rsa
ll ~/.ssh/
-rw-------. 1 root root 1675 Oct 16 15:37 id_rsa
-rw-r--r--. 1 root root 397 Oct 16 15:37 id_rsa.pub
(7)在monitor_01节点上配置所有集群节点免密登录(确认两次)
ssh-copy-id monitor-01
ssh-copy-id monitor-02
(8)连接测试
ssh 'monitor-01'
ssh 'monitor-02'
(9)时间同步
主节点:
yum install chrony
vim /etc/chrony.conf
server ntp.ntsc.ac.cn iburst
allow 172.16.208.13
systemctl restart chronyd
systemctl enable chronyd
从节点:
yum install chrony
vim /etc/chrony.conf
server 172.16.208.12 iburst
systemctl restart chronyd
systemctl enable chronyd
Elasticsearch
下载安装
获取rpm包
wget https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-7.17.20-x86_64.rpm
安装
yum -y localinstall elasticsearch-7.17.20-x86_64.rpm
查看jdk版本 (内置了jdk,所以不用安装jdk运行环境)
/usr/share/elasticsearch/jdk/bin/java --version
启动服务 --要一会时间
systemctl start elasticsearch
查看端口
netstat -ntl
其中 9200是与集群外部进行通信,9300是集群中的节点相互通信
访问测试
curl 127.0.0.1:9200
配置文件修改
其中两个文件比较常用:/etc/elasticsearch/elasticsearch.yml和/etc/elasticsearch/jvm.options一个是配置文件,一个是调优文件注意: 在8.0之前的版本es的安全特性并没有开启,8.0版本以后的开启了安全特性。
配置文件介绍
/etc/elasticsearch/elasticsearch.yml
cluster.name: my-application 修改集群名称,默认就是elasticsearch
node.name: node-1 修改节点名称,默认是主机名
path.data: /var/lib/elasticsearch 默认的数据存放位置
path.logs: /var/log/elasticsearch 默认日志存放位置
network.host: 192.168.0.1 默认只能访问本机,设置为0.0.0.0,当前节点的所有地址都能访问
http.port: 9200 默认的发现端口
discovery.seed_hosts: ["host1", "host2"] 不设置的话默认为127.0.0.1只能发现自己,当然也可以使用主机名。设置[“172.16.208.12”]
重启生效
systemctl restart elasticsearch
启动日志查看
tail -40 /var/log/elasticsearch/Tgqs_elasticsearch.log
前面修改的networkhost配置,现在curl monitor-01:9200和curl 172.16.208.12:9200都可以访问
然后发现显示的节点名称和集群名称都改成了配置的数据
集群部署
针对monotor-02节点
wget https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-7.17.20-x86_64.rpm
安装
yum -y localinstall elasticsearch-7.17.20-x86_64.rpm
修改monitor-01的配置文件,添加208.13节点,如果集群还有别的节点同样添加
vim /etc/elasticsearch/elasticsearch.yml
discovery.seed_hosts: ["172.16.208.12", "172.16.208.13"]
修改monitor-02配置文件,除了节点名称,别参数的都与主节点一致
cluster.name: Tgqs_elasticsearch
node.name: elk-02
network.host: 0.0.0.0
discovery.seed_hosts: ["172.16.208.12", "172.16.208.13"]
网页查询集群状态
http://172.16.208.12:9200/_cluster/health?pretty
status显示green表示正常
http://172.16.208.12:9200/_cat/nodes?V
其中带*表示master节点,其他的为工作节点
kibana
下载安装
注意版本与elasticsearch完全相同
wget https://artifacts.elastic.co/downloads/kibana/kibana-7.17.20-x86_64.rpm
yum -y localinstall kibana-7.17.20-x86_64.rpm
配置文件 /etc/kibana/kibana.yml
#server.port: 5601 默认端口
#server.host: "localhost" 改为server.host: "0.0.0.0"
elasticsearch.hosts: ["http://172.16.208.12:9200","http://172.16.208.13:9200"] 添加集群
i18n.locale: "en" 默认语言为英语 修改为中文 i18n.locale: "zh-CN"
启动过程中查看kibana的配置
systemctl cat kibana
查看报错
journalctl -u kibana
tail -40 /var/log/kibana/kibana.log
web界面访问
http://172.16.208.12:5601/app/home#/
菜单-堆栈检测 可以看到一些监控数据
菜单-stack management 可以看到索引管理,其中.开头的一般都是隐藏索引,自己创建不以.开头
filebeat
下载安装
filebeat的部署应该在需要收集日志的节点的上,如MySQL服务,就部署在MySQL的节点上。
注意与前面的elastic和kibana版本相同
wget https://artifacts.elastic.co/downloads/beats/filebeat/filebeat-7.17.20-x86_64.rpm
安装
yum -y localinstall filebeat-7.17.20-x86_64.rpm
官方配置文档地址
https://www.elastic.co/guide/en/beats/filebeat/current/configuration-filebeat-options.html
配置文件位置(建议自己重新编辑)
/etc/filebeat/filebeat.yml
测试
filebeat -e -c /etc/filebeat/filebeat.yml
配置文件编写
主要关注input、output信息(这里只是参考,具体配置文件看测试文件)
filebeat.inputs:
- type: log
#默认是ture,设置为false是不启用改日志收集
enbaled: false
paths:
- /var/log/system.log
- /var/log/wifi.log
#给当前输入加上标签
tags: ["json"]
#自定义字段,key-vaule
fields:
name: “撒旦”
- type: log
paths:
- /var/log/test.log
tags: ["test"]
fields:
name: lisa
#是否设置为顶级字段,默认不开启,开启后将直接显示字段值,不在将字段放在field字段中显示
fields_under_root: true
output.elasticsearch:
hosts: ["http://172.16.208.12:9200","http://172.16.208.13:9200"]
index: "Tgqs-elk-%{+yyyy.MM.dd}"
#禁用索引生命周期
setup.ilm.enabled: false
#设置索引模板的名称,设置索引模板的匹配模式
setup.template.enabled: false
setup.template.name: "Tgqs"
setup.template.pattern: "Tgqs-*"
注意事项
配置文件修改完成后在kibana索引模式中创建索引,然后在菜单栏-discover选项
.exclude_files是匹配的文件路径,如/home/davinciyxw/files/test.log,不能设置[‘^test’],而是设置[‘files/test’]来匹配
默认不会重复收集日志,如果想重复收集之前的日志删除/var/lib/filebeat/下面的所有文件
输出The default is "filebeat-%{[agent.version]}-%{+yyyy.MM.dd}", for example, "filebeat-7.17.24-2024-10-14". If you change this setting, you also need to configure the setup.template.name and setup.template.pattern options
If index lifecycle management is enabled (which is typically the default), setup.template.name and setup.template.pattern are ignored.
测试实时数据展示
1.创建一个log测试文件
vim /var/log/test.log
ddd
qqq
2.配置filebeat的配置文件
vim /etc/filebeat/filebeat.yml
filebeat.inputs:
- type: log
paths:
- /var/log/test.log
tags: ["test"]
fields:
name: "lisa"
fields_under_root: true
output.elasticsearch:
hosts: ["http://172.16.208.12:9200","http://172.16.208.13:9200"]
index: "Tgqs-elk-%{+yyyy.MM.dd}"
setup.template.enabled: false
setup.template.name: "Tgqs"
setup.template.pattern: "Tgqs-*"
setup.ilm.enabled: false
3.删除之前重复收集的日志,启动filebeat
rm -rf /var/lib/filebeat/*
filebeat -e -c /etc/filebeat/filebeat.yml
4.在kibana的web界面创建索引,查看log文件
5.添加信息到log测试文件中
echo TES >> /var/log/test.log
6.刷新web界面查看日志更新情况