EFK架构日志分析

elastic search 单点部署

环境准备

  • 主机名及 IP 地址 主机名 IP 地址 elk91 10.0.0.91 elk92 10.0.0.92 elk93 10.0.0.93
  • 硬件配置
  • CPU:2core +
  • Memory:4GB +
  • Disk:50GB +
  • 系统要求:Ubuntu 22.04 LTS
  • 网卡环境:单网卡

什么是 ElasticStack

ElasticStack 早期名称为 elk,elk 分别代表了 3 个组件:

  • ElasticSearch:负责数据存储和检索。
  • Logstash:负责数据的采集,将源数据采集到 ElasticSearch 进行存储。
  • Kibana:负责数据的展示。

由于 Logstash 是一个重量级产品,安装包超过 300MB +,很多同学只是用于采集日志,于是使用其他采集工具代替,比如 flume,fluentd 等产品替代。后来 elastic 公司也发现了这个问题,于是开发了一堆 beats 产品,其中典型代表就是 Filebeat,metricbeat,heartbeat 等。而后,对于安全而言,又推出了 xpack 等相关组件,以及云环境的组件。后期名称命名为 elk stack,后来公司为了宣传 ElasticStack。

二进制部署 ES 单点环境

ElasticSearch 的版本选择及下载

wget https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-7.17.28-linux-x86_64.tar.gz
wget https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-7.17.28-linux-x86_64.tar.gz.sha512
shasum -a 512 -c elasticsearch-7.17.28-linux-x86_64.tar.gz.sha512

svip 示例

[root@elk91 ~]# wget http://192.168.16.253/Resources/ElasticStack/softwares/7.17.28/elasticsearch-7.17.28-linux-x86_64.tar.gz
[root@elk91 ~]# wget http://192.168.16.253/Resources/ElasticStack/softwares/7.17.28/elasticsearch-7.17.28-linux-x86_64.tar.gz.sha512
[root@elk91 ~]# shasum -a 512 -c elasticsearch-7.17.28-linux-x86_64.tar.gz.sha512
elasticsearch-7.17.28-linux-x86_64.tar.gz: OK
[root@elk91 ~]#

解压软件包

[root@elk91 ~]# tar xf elasticsearch-7.17.28-linux-x86_64.tar.gz -C /usr/local/

修改配置文件

[root@elk91 ~]# vim /usr/local/elasticsearch-7.17.28/config/elasticsearch.yml
[root@elk91 ~]#
[root@elk91 ~]# egrep -v "^#|^$" /usr/local/elasticsearch-7.17.28/config/elasticsearch.yml
cluster.name: haoshuaicong-linux
path.data: /var/lib/es7
path.logs: /var/log/es7
network.host: 0.0.0.0
discovery.type: single-node
[root@elk91 ~]#

相关参数说明

  • cluster.name:集群的名称。
  • path.data:ES 的数据存储路径。
  • path.logs:ES 的日志存储路径。
  • network.host:ES 服务监听的地址。
  • discovery.type:指的 ES 集群的部署类型,此处的 “single – node”,表示的是一个单点环境。

创建用户并授权

[root@elk91 ~]# useradd -m haoshuaicong
[root@elk91 ~]# chown haoshuaicong:haoshuaicong -R /usr/local/elasticsearch-7.17.28/
[root@elk91 ~]#
[root@elk91 ~]# ll /usr/local/elasticsearch-7.17.28/jdk/bin -d
drwxr-x--- 2 haoshuaicong haoshuaicong 4096 Feb 20 17:09 /usr/local/elasticsearch-7.17.28/jdk/bin/
[root@elk91 ~]#

创建数据目录和日志目录

[root@elk91 ~]# install -d /var/{lib,log}/es7 -o haoshuaicong -g haoshuaicong
[root@elk91 ~]#
[root@elk91 ~]# ll -d /var/{lib,log}/es7
drwxr-xr-x 2 haoshuaicong haoshuaicong 4096 Mar 11 11:07 /var/lib/es7/
drwxr-xr-x 2 haoshuaicong haoshuaicong 4096 Mar 11 10:59 /var/log/es7/
[root@elk91 ~]#

启动 ElasticSearch 服务

[root@elk91 ~]# su - haoshuaicong -c '/usr/local/elasticsearch-7.17.28/bin/elasticsearch -d'
[root@elk91 ~]#
[root@elk91 ~]# ss -ntl  | egrep "9[2|3]00"
LISTEN 0      4096               *:9300            *:*
LISTEN 0      4096               *:9200            *:*
[root@elk91 ~]#

查看服务验证

[root@elk93 ~]# curl http://10.0.0.91:9200
{
  "name": "elk91",
  "cluster_name": "haoshuaicong-linux",
  "cluster_uuid": "f8m0l-IiQAG1ZHKFjQgqFA",
  "version": {
    "number": "7.17.28",
    "build_flavor": "default",
    "build_type": "tar",
    "build_hash": "139cb5a961d8de68b8e02c45cc47f5289a3623af",
    "build_date": "2025-02-20T09:05:31.349013687Z",
    "build_snapshot": false,
    "lucene_version": "8.11.3",
    "minimum_wire_compatibility_version": "6.8.0",
    "minimum_index_compatibility_version": "6.0.0-beta1"
  },
  "tagline": "You Know, for Search"
}
[root@elk93 ~]#
[root@elk93 ~]# curl http://10.0.0.91:9200/_cat/nodes
10.0.0.91 34 97 7 0.11 0.09 0.03 cdfhilmrstw * elk91
[root@elk93 ~]#

常见的报错梳理

Q1
bootstrap check failure [1] of [1]: max virtual memory areas vm.max_map_count [65530] is too low, increase to at least [262144]
ERROR: Elasticsearch did not exit normally - check the logs at /var/log/es7/my-haoshuaicong.log

报错问题:虚拟内存映射过小导致的问题。
解决方案

[root@elk91 config]# echo vm.max_map_count=262144 >> /etc/sysctl.d/es.conf
[root@elk91 config]#
[root@elk91 config]# sysctl -f /etc/sysctl.d/es.conf
vm.max_map_count = 262144
[root@elk91 config]#
Q2
java.net.UnknownHostException: single-node

报错问题:无法识别的主机名称,一般情况下就是配置文件写错了。
解决方案:检查 “discovery.seed_hosts” 配置文件是否指定为服务器主机名。

Q3
expected <block end>, but found '<block mapping start>'
 in 'reader', line 75, column 3:
      discovery.type: single-node
      ^

报错问题:一般情况下就是配置文件写错了。
解决方案:检查 yaml,yml 的配置文件格式。

Q4
java.lang.IllegalStateException: failed to obtain node locks, tried [[/var/lib/es7]] with lock id [0]; maybe these locations are not writable or multiple nodes were started without increasing [node.max_local_storage_nodes] (was [1])?

报错问题:出现 lock 字样说明已经有 ES 实例启动。
解决方案

[root@elk91 config]# pkill java
[root@elk91 config]# rm -rf /var/{lib,log}/es7/* /tmp/*
Q5
{"error":{"root_cause":[{"type":"master_not_discovered_exception","reason":null}],"type":"master_not_discovered_exception","reason":null},"status":503}

报错问题:ES 集群部署有问题,缺少 master 角色。
解决方案:检查集群配置是否正确。

卸载二进制的 ES 程序

停止服务

kill `ps -ef | grep java | grep -v grep | awk '{print $2}'`

删除程序,数据,日志目录

[root@elk91 ~]# rm -rf /usr/local/elasticsearch-7.17.28/ /var/{lib,log}/es7/

移除普通用户

[root@elk91 ~]# userdel -r haoshuaicong

deb 包安装 ES 部署单点

下载软件包

wget https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-7.17.28-amd64.deb

svip 示例

[root@elk91 ~]# wget http://192.168.16.253/Resources/ElasticStack/softwares/7.17.28/elasticsearch-7.17.28-amd64.deb

安装 es

[root@elk91 ~]# dpkg -i elasticsearch-7.17.28-amd64.deb

添加别名

[root@elk91 ~]# tail  -1 .rc
alias yy='egrep -v "^#|^$"'
[root@elk91 ~]#
[root@elk91 ~]# source .rc
[root@elk91 ~]#

修改配置文件

[root@elk91 ~]# yy /etc/elasticsearch/elasticsearch.yml
cluster.name: haoshuaicong-linux
path.data: /var/lib/elasticsearch
path.logs: /var/log/elasticsearch
network.host: 0.0.0.0
http.port: 9200
discovery.type: single-node
[root@elk91 ~]#

启动 ES 服务

[root@elk91 ~]# systemctl enable --now elasticsearch.service
[root@elk91 ~]#
[root@elk91 ~]# ss -ntl  | egrep "9[2|3]00"
LISTEN 0      4096               *:9300            *:*
LISTEN 0      4096               *:9200            *:*
[root@elk91 ~]#

访问 ES 的 WebUI

[root@elk93 ~]# curl http://10.0.0.91:9200/_cat/nodes
10.0.0.91 17 97 0 0.20 0.09 0.03 cdfhilmrstw * elk91
[root@elk93 ~]#
[root@elk93 ~]# curl http://10.0.0.91:9200/
{
  "name": "elk91",
  "cluster_name": "haoshuaicong-linux",
  "cluster_uuid": "h5Eru0tzSKmIHxgyw2q3rg",
  "version": {
    "number": "7.17.28",
    "build_flavor": "default",
    "build_type": "deb",
    "build_hash": "139cb5a961d8de68b8e02c45cc47f5289a3623af",
    "build_date": "2025-02-20T09:05:31.349013687Z",
    "build_snapshot": false,
    "lucene_version": "8.11.3",
    "minimum_wire_compatibility_version": "6.8.0",
    "minimum_index_compatibility_version": "6.0.0-beta1"
  },
  "tagline": "You Know, for Search"
}
[root@elk93 ~]#

elastic search集群部署

ES 集群常见的术语

  • 索引(Index):用户进行数据的读写单元。
  • 分片(Shard):
  • 一个索引至少要有一个分片,如果一个索引仅有一个分片,意味着该索引的数据只能全量存储在某个节点上,且分片是不可拆分的,隶属于某个节点。换句话说,分片是 ES 集群最小的调度单元。
  • 一个索引数据也可以被分散的存储在不同的分片上,且这些分片可以放在不同的节点,从而实现数据的分布式存储。
  • 副本(replica):
  • 副本是针对分片来说的,一个分片可以有 0 个或多个副本。
  • 当副本数量为 0 时,意味着只有主分片(priamry shard),当主分片所在的节点宕机时,数据就无法访问了。
  • 当副本数量大于 0 时,意味着同时存在主分片和副本分片(replica shard):
    • 主分片负责数据的读写(read write,rw)。
    • 副本分片负责数据的读的负载均衡(read only,ro)。
  • 文档(document):指的是用户存储的数据。其中包含元数据和源数据。
  • 元数据:用于描述源数据的数据。
  • 源数据:用户实际存储的数据。
  • 分配(allocation):指的是将索引的不同分片(包含主分片和副本分片)分配到整个集群的过程。

面试题

Q1:ES 端口协议及作用?
  • 9200:
  • 协议:http | https
  • 作用:对外部用户提供访问。
  • 9300:
  • 协议:tcp
  • 作用:对集群内部进行数据传输。相比于 9200 更加优先启动。
Q2:ES 集群的颜色状态有哪些?
  • green:代表所有的主分片和副本分片都可以正常访问。
  • red:代表有部分主分片无法访问。
  • yellow:代表有部分副本分片无法访问。
查看集群状态
[root@elk93 ~]# curl http://10.0.0.91:9200/_cat/health?v
epoch      timestamp cluster           status node.total node.data shards pri relo init unassign pending_tasks max_task_wait_time active_shards_percent
1741676068 06:54:28  haoshuaicong-linux green           1         1      3   3    0    0        0             0                  -                100.0%
[root@elk93 ~]# 

ES 集群环境部署

停止单点服务

[root@elk91 ~]# systemctl stop elasticsearch.service 
[root@elk91 ~]# 

拷贝软件包

[root@elk91 ~]# scp elasticsearch-7.17.28-amd64.deb 10.0.0.92:~
[root@elk91 ~]# scp elasticsearch-7.17.28-amd64.deb 10.0.0.93:~

其他节点安装 ES 环境

[root@elk92 ~]# dpkg -i elasticsearch-7.17.28-amd64.deb 
[root@elk93 ~]# dpkg -i elasticsearch-7.17.28-amd64.deb 

修改 ES 集群配置文件

[root@elk91 ~]# vim /etc/elasticsearch/elasticsearch.yml 
[root@elk91 ~]# 
[root@elk91 ~]# yy /etc/elasticsearch/elasticsearch.yml 
cluster.name: haoshuaicong-linux
path.data: /var/lib/elasticsearch
path.logs: /var/log/elasticsearch
network.host: 0.0.0.0
http.port: 9200
discovery.seed_hosts: ["10.0.0.91", "10.0.0.92","10.0.0.93"]
cluster.initial_master_nodes: ["10.0.0.91","10.0.0.92","10.0.0.93"]
[root@elk91 ~]# 
[root@elk91 ~]# scp /etc/elasticsearch/elasticsearch.yml 10.0.0.92:/etc/elasticsearch/
[root@elk91 ~]# scp /etc/elasticsearch/elasticsearch.yml 10.0.0.93:/etc/elasticsearch/

所有节点同时启动 ES 服务

[root@elk91 ~]# systemctl enable --now elasticsearch.service
[root@elk91 ~]# ss -ntl | egrep "9[2|3]00"
LISTEN 0      4096               *:9300            *:*          
LISTEN 0      4096               *:9200            *:*          
[root@elk91 ~]# 

[root@elk92 ~]# systemctl enable --now elasticsearch.service
[root@elk92 ~]# ss -ntl | egrep "9[2|3]00"
LISTEN 0      4096               *:9300            *:*          
LISTEN 0      4096               *:9200            *:*          
[root@elk92 ~]# 

[root@elk93 ~]# systemctl enable --now elasticsearch.service
[root@elk93 ~]# ss -ntl | egrep "9[2|3]00"
LISTEN 0      4096               *:9300            *:*          
LISTEN 0      4096               *:9200            *:*          
[root@elk93 ~]# 

检查集群是否正常工作

[root@elk93 ~]# curl http://10.0.0.91:9200/_cat/nodes?v
ip        heap.percent ram.percent cpu load_1m load_5m load_15m node.role   master name
10.0.0.93           27          97   0    0.28    0.17     0.06 cdfhilmrstw -      elk93
10.0.0.91            8          97   0    0.23    0.14     0.05 cdfhilmrstw *      elk91
10.0.0.92           27          97   4    0.80    0.33     0.12 cdfhilmrstw -      elk92
[root@elk93 ~]# 

快速部署校验 ES 集群解决方案

报错信息:集群缺少 master
[root@elk3 ~]# curl http://10.0.0.92:9200/_cat/nodes?v
{"error":{"root_cause":[{"type":"master_not_discovered_exception","reason":null}],"type":"master_not_discovered_exception","reason":null},"status":503}
[root@elk3 ~]# curl 10.0.0.91:9200
{
  "name" : "elk91",
  "cluster_name" : "haoshuaicong-linux",
  "cluster_uuid" : "_na_",
  ...
}
[root@elk3 ~]# 
[root@elk3 ~]# curl 10.0.0.92:9200
{
  "name" : "elk2",
  "cluster_name" : "haoshuaicong-linux",
  "cluster_uuid" : "_na_",
  ...
}
[root@elk3 ~]# 
[root@elk3 ~]# 
[root@elk3 ~]# curl 10.0.0.93:9200
{
  "name" : "elk3",
  "cluster_name" : "haoshuaicong-linux",
  "cluster_uuid" : "_na_",
  ...
}
[root@elk3 ~]# 
解决方案
  • 停止集群的 ES 服务
[root@elk91 ~]# systemctl stop elasticsearch.service 
[root@elk92 ~]# systemctl stop elasticsearch.service 
[root@elk93 ~]# systemctl stop elasticsearch.service 
  • 删除数据,日志,和临时数据
[root@elk91 ~]# rm -rf /var/{lib,log}/elasticsearch/* /tmp/*
[root@elk92 ~]# rm -rf /var/{lib,log}/elasticsearch/* /tmp/*
[root@elk93 ~]# rm -rf /var/{lib,log}/elasticsearch/* /tmp/*
  • 检查集群各节点的配置
[root@elk91 ~]# yy /etc/elasticsearch/elasticsearch.yml 
cluster.name: haoshuaicong-linux
path.data: /var/lib/elasticsearch
path.logs: /var/log/elasticsearch
network.host: 0.0.0.0
http.port: 9200
discovery.seed_hosts: ["10.0.0.91", "10.0.0.92","10.0.0.93"]
cluster.initial_master_nodes: ["10.0.0.91","10.0.0.92","10.0.0.93"]
[root@elk91 ~]# 
[root@elk91 ~]# scp /etc/elasticsearch/elasticsearch.yml 10.0.0.92:/etc/elasticsearch/
[root@elk91 ~]# scp /etc/elasticsearch/elasticsearch.yml 10.0.0.93:/etc/elasticsearch/
  • 启动 ES 服务
[root@elk91 ~]# systemctl restart elasticsearch.service 
[root@elk92 ~]# systemctl restart elasticsearch.service 
[root@elk93 ~]# systemctl restart elasticsearch.service 
  • 测试验证
[root@elk91 ~]# for i in `seq 91 93`; do curl -s 10.0.0.$i:9200 | grep cluster_uuid; done
  "cluster_uuid" : "-5ly4d8-Tl6biIMmp4pzKw",
  "cluster_uuid" : "-5ly4d8-Tl6biIMmp4pzKw",
  "cluster_uuid" : "-5ly4d8-Tl6biIMmp4pzKw",
[root@elk91 ~]# 
[root@elk91 ~]# curl 10.0.0.93:9200/_cat/nodes
10.0.0.91 11 94 4 0.43 0.36 0.29 cdfhilmrstw - elk91
10.0.0.93 11 96 2 0.47 0.43 0.28 cdfhilmrstw - elk93
10.0.0.92  5 97 3 0.32 0.44 0.33 cdfhilmrstw * elk92
[root@elk91 ~]# 

部署 Kibana 创建索引查询

ES 与 MySQL 对比

  • MySQL:属于关系型数据库,其增删改查操作基于 SQL 语句。
  • ES:属于文档型数据库,和 MongoDB 很相似,增删改查使用 DSL 语句,这是 ES 独有的一种查询语言。在处理模糊查询时,MySQL 无法充分利用索引,性能较低,而 ES 查询模糊数据非常高效。

ES 的 DSL 语句初体验

往 ES 集群添加单条数据

curl --location --request POST '10.0.0.91:9200/haoshuaicong_linux/doc' \
--header 'Content-Type: application/json' \
--data-raw '{
    "name": "孙悟空",
    "hobby": [
        "蟠桃",
        "金丹",
        "紫霞"
    ]
}'

往 ES 集群添加多条数据

curl --location --request POST 'http://10.0.0.91:9200/_bulk' \
--header 'Content-Type: application/json' \
--data-raw '{ "create" : { "_index" : "haoshuaicong-linux", "_id" : "1001" } }
{ "name" : "猪八戒","hobby": ["猴哥","高老庄"] }
{ "create" : { "_index" : "haoshuaicong-linux", "_id" : "1002" } }
{ "name" : "沙和尚","hobby": ["流沙河","挑行李"] }
{ "create" : { "_index" : "haoshuaicong-linux", "_id" : "1003" } }
{ "name" : "白龙马","hobby": ["大师兄,师傅被妖怪抓走啦"] }
'

查询单条数据

curl --location --request GET '10.0.0.93:9200/haoshuaicong-linux/_doc/1003'

过滤查询数据

curl --location --request GET '10.0.0.93:9200/haoshuaicong-linux/_search' \
--header 'Content-Type: application/json' \
--data-raw '{
    "query": {
        "match": {
            "hobby": "猴哥"
        }
    }
}'

删除数据

curl --location --request DELETE '10.0.0.93:9200/haoshuaicong-linux/_doc/1003'

部署 Kibana 环境

Kibana 是针对 ES 做的一款可视化工具,web页面图形化操作,后续操作可在 Kibana 中完成。

下载 Kibana

[root@elk91 ~]# wget https://artifacts.elastic.co/downloads/kibana/kibana-7.17.28-amd64.deb

SVIP

[root@elk91 ~]# wget http://192.168.16.253/Resources/ElasticStack/softwares/7.17.28/kibana-7.17.28-amd64.deb

安装 Kibana

[root@elk91 ~]# dpkg -i kibana-7.17.28-amd64.deb 

修改 Kibana 的配置文件

[root@elk91 ~]# vim /etc/kibana/kibana.yml 
[root@elk91 ~]# 
[root@elk91 ~]# yy /etc/kibana/kibana.yml 
server.port: 5601
server.host: "0.0.0.0"
elasticsearch.hosts: ["http://10.0.0.91:9200","http://10.0.0.92:9200","http://10.0.0.93:9200"]
i18n.locale: "zh-CN"
[root@elk91 ~]# 

相关参数说明

  • server.port:Kibana 监听的端口。
  • server.host:Kibana 监听的 IP 地址。
  • elasticsearch.hosts:Kibana 管理的 ES 集群信息。
  • i18n.locale:安装时选择的语言。

启动 Kibana 服务

[root@elk91 ~]# systemctl enable --now kibana.service
[root@elk91 ~]# ss -ntl | grep 5601
LISTEN 0      511          0.0.0.0:5601      0.0.0.0:*          
[root@elk91 ~]# 

访问 Kibana 的 WebUI

http://10.0.0.91:5601/

Kibana 基于 KQL 的基本使用

略,见图片。

1.创建索引
2.使用索引查看数据

Filebeat 部署

部署 Filebeat 及 log 类型底层逻辑验证

  1. 下载 Filebeat
   wget https://artifacts.elastic.co/downloads/beats/filebeat/filebeat-7.17.28-amd64.deb

SVIP

   [root@elk92 ~]# wget http://192.168.16.253/Resources/ElasticStack/softwares/7.17.28/filebeat-7.17.28-amd64.deb
  1. 安装 Filebeat
   [root@elk92 ~]# dpkg -i filebeat-7.17.28-amd64.deb
  1. 编写 Filebeat 配置文件
   [root@elk92 ~]# cat /etc/filebeat/config/01-log-to-console.yaml
   # 定义数据从哪里来
   filebeat.inputs:
     # 指定数据源的类型是log,表示从文件读取数据
   - type: log
     # 指定文件的路径
     paths:
       - /tmp/student.log

   # 定义数据到终端
   output.console:
     pretty: true
   [root@elk92 ~]#
  1. 启动 Filebeat 实例
   [root@elk92 ~]# echo 123 > /tmp/student.log
   [root@elk92 ~]# filebeat -e -c /etc/filebeat/config/01-log-to-console.yaml
   # -e表示内容输出到终端,调试模式。-c指定配置文件
   # 注:必须用绝对路径指定配置文件
  1. 向源数据文件 (“/tmp/student.log”) 写入测试数据
    略,见视频。
# 假设采集到一条服务程序的数据 
echo 123 > /tmp/student.log
# 然后启动实例 
filebeat -e -c /etc/filebeat/config/01-log-to-console.yaml

# 采集信息输出实例,-e输出到了终端屏幕

"@timestamp": "2025-03-11T12:47:38.262Z",
  "@metadata": {
    "beat": "filebeat",
    "type": "_doc",
    "version": "7.17.28"
  },
  "agent": {
    "hostname": "elk92",
    "ephemeral_id": "3c6d077c-92f8-42e0-a2b8-4d6aa08fc83b",
    "id": "33c41ce7-ef5d-4e7f-92cd-e6766b1fa397",
    "name": "elk92",
    "type": "filebeat",
    "version": "7.17.28"
  },
  "log": {
    "file": {
      "path": "/tmp/student.log"
    },
    "offset": 4
  },
  "message": "123",
  "input": {
    "type": "log"
  },
  "ecs": {
    "version": "1.12.0"
  },
  "host": {
    "name": "elk92"
  }
}



# log.json记录数据的状态信息,特别是已经采集过的数据的偏移量,方便重启后可以继续采集。可以看offset数据位置点
grep offset /var/lib/filebeat/registry/filebeat/log.json  # 注换行符占一个位置,一行一个换行符

# 总结:
Filebeat 会监控 student.log 文件,当文件有新内容写入时,Filebeat 会按行读取这些新内容。
读取到的内容会根据 Filebeat 的配置被发送到指定的输出端,如控制台、Elasticsearch 等。
同时,Filebeat 会更新 log.json 文件中的偏移量信息,记录它已经读取到了 student.log 文件的哪个位置。

温馨提示

  • filebeat 默认是按行采集数据。
  • filebeat 默认会在 “/var/lib/filebeat” 目录下记录已经采集的文件 offset 信息,以便于下一次采集接着该位置继续采集数据。

EFK 架构梳理

  1. 编写配置文件
   [root@elk92 ~]# cat  /etc/filebeat/config/02-log-to-es.yaml
   filebeat.inputs:
   - type: log
     paths:
       - /tmp/student.log

   # 将数据写入到ES集群
   output.elasticsearch:
     hosts:
     - 10.0.0.91:9200
     - 10.0.0.92:9200
     - 10.0.0.93:9200
   [root@elk92 ~]#
  1. 启动 Filebeat 实例
   [root@elk92 ~]# rm -rf /var/lib/filebeat/
   [root@elk92 ~]# echo 123 > /tmp/student.log
   [root@elk92 ~]# filebeat -e -c  /etc/filebeat/config/02-log-to-es.yaml
   # 然后去kifana 网页查看数据
  1. 数据流走向
   echo ---> /tmp/student.log ---> filebeat  ---> ES集群  ---> Kibana

Filebeat 写入 ES 集群自定义索引实战案例

  1. 编写配置文件
   [root@elk92 ~]# cat /etc/filebeat/config/03-log-to-es-custom-index.yaml
   filebeat.inputs:
   - type: log
     paths:
       - /tmp/student.log

   # 将数据写入到ES集群
   output.elasticsearch:
     hosts:
     - 10.0.0.91:9200
     - 10.0.0.92:9200
     - 10.0.0.93:9200
     # 指定索引的名称
     index: haoshuaicong-linux-tmp-%{+yyyy.MM.dd}

   # 禁用索引生命周期管理(index lifecycle management,ILM)
   # 如果启用了此配置,则忽略自定义索引的所有信息
   setup.ilm.enabled: false
   # 定义索引模板(就是创建索引的规则)的名称
   setup.template.name: "haoshuaicong-linux"
   # 定义索引模板的匹配模式,表示当前索引模板针对哪些索引生效。
   setup.template.pattern: "haoshuaicong-linux-*"
   # 如果索引模板存在,是否覆盖,默认值为false,如果明确需要,则可以将其设置为ture。
   # 但是官方建议将其设置为false,原因是每次写数据时,都会建立tcp链接,消耗资源。
   setup.template.overwrite: true
   # 定义索引模板的规则信息
   setup.template.settings:
     # 指定索引能够创建的分片的数量
     index.number_of_shards: 5
     # 指定每个分片有多少个副本
     index.number_of_replicas: 0
   [root@elk92 ~]#
  1. 启动 Filebeat 实例
   [root@elk92 ~]# rm -rf /var/lib/filebeat/ 
   [root@elk92 ~]# filebeat -e -c /etc/filebeat/config/03-log-to-es-custom-index.yaml

项目案例: filebeat 采集 nginx 实战案例

  1. 安装 nginx 服务
   [root@elk92 ~]# apt -y install nginx
  1. 启动 nginx 服务
   [root@elk92 ~]# systemctl enable --now nginx
  1. 访问测试
   [root@elk92 ~]# curl 10.0.0.92
   <!DOCTYPE html>
   <html>
   <head>
   <title>Welcome to nginx!</title>
   <style>
       body {
           width: 35em;
           margin: 0 auto;
           font-family: Tahoma, Verdana, Arial, sans-serif;
       }
   </style>
   </head>
   <body>
   <h1>Welcome to nginx!</h1>
   <p>If you see this page, the nginx web server is successfully installed and
   working. Further configuration is required.</p>

   <p>For online documentation and support please refer to
   <a href="http://nginx.org/">nginx.org</a>.<br/>
   Commercial support is available at
   <a href="http://nginx.com/">nginx.com</a>.</p>

   <p><em>Thank you for using nginx.</em></p>
   </body>
   </html>
   [root@elk92 ~]#
  1. 查看日志信息
   [root@elk92 ~]# cat /var/log/nginx/access.log 
   10.0.0.92 - - [11/Mar/2025:18:27:23 +0800] "GET / HTTP/1.1" 200 612 "-" "curl/7.81.0"
   [root@elk92 ~]#
  1. 使用 Filebeat 采集 nginx 日志
   [root@elk92 ~]# cat /etc/filebeat/config/04-nginx-to-es.yaml
   filebeat.inputs:
   - type: log
     paths:
       - /var/log/nginx/access.log*

   output.elasticsearch:
     hosts:
     - 10.0.0.91:9200
     - 10.0.0.92:9200
     - 10.0.0.93:9200
     index: haoshuaicong-linux-nginx-%{+yyyy.MM.dd}

   setup.ilm.enabled: false
   setup.template.name: "haoshuaicong-linux"
   setup.template.pattern: "haoshuaicong-linux-*"
   setup.template.overwrite: true
   setup.template.settings:
     index.number_of_shards: 5
     index.number_of_replicas: 0
   [root@elk92 ~]#
  1. 启动 Filebeat 实例
   [root@elk92 ~]# filebeat -e -c /etc/filebeat/config/04-nginx-to-es.yaml
  1. Kibana 查看数据
    略,见视频。
  1. 发送测试数据
   [root@elk93 ~]# for i in `seq 10`;do curl http://10.0.0.92;done

filebeat 多实例

  1. 启动实例 1
   filebeat -e -c /etc/filebeat/config/01-log-to-console.yaml  --path.data /tmp/xixi
  1. 启动实例 2
   filebeat -e -c /etc/filebeat/config/02-log-to-es.yaml  --path.data /tmp/haha

总结

- ES单点部署
    - 二进制ES的单点部署
    - deb包实现单点部署

- ES集群部署

- ES的常用术语:
    - index 
    - shard 
    - replica 
    - document
    - allocation

- kibana环境部署 

- Filebeat环境部署 

- Filebeat的基本使用
    - input: 数据从哪里来
        - log 
    - output: 数据到哪去
        - console
        - elasticsearch     

- EFK架构采集nginx访问日志

- filebeat多实例

暂无评论

发送评论 编辑评论


				
|´・ω・)ノ
ヾ(≧∇≦*)ゝ
(☆ω☆)
(╯‵□′)╯︵┴─┴
 ̄﹃ ̄
(/ω\)
∠( ᐛ 」∠)_
(๑•̀ㅁ•́ฅ)
→_→
୧(๑•̀⌄•́๑)૭
٩(ˊᗜˋ*)و
(ノ°ο°)ノ
(´இ皿இ`)
⌇●﹏●⌇
(ฅ´ω`ฅ)
(╯°A°)╯︵○○○
φ( ̄∇ ̄o)
ヾ(´・ ・`。)ノ"
( ง ᵒ̌皿ᵒ̌)ง⁼³₌₃
(ó﹏ò。)
Σ(っ °Д °;)っ
( ,,´・ω・)ノ"(´っω・`。)
╮(╯▽╰)╭
o(*////▽////*)q
>﹏<
( ๑´•ω•) "(ㆆᴗㆆ)
😂
😀
😅
😊
🙂
🙃
😌
😍
😘
😜
😝
😏
😒
🙄
😳
😡
😔
😫
😱
😭
💩
👻
🙌
🖕
👍
👫
👬
👭
🌚
🌝
🙈
💊
😶
🙏
🍦
🍉
😣
Source: github.com/k4yt3x/flowerhd
颜文字
Emoji
小恐龙
花!
上一篇
下一篇