小打小闹ELK入门之Logstash、Kibana

Filebeat

主要组件:

harvester(收割机)

  • 负责读取单个文件的内容
  • 若读取文件时被删除或者重命名,Filebeat将继续读取文件

prospector(勘探者)

  • 负责管理harvester并找到所有要读取的文件来源

  • 匹配配置文件中的输入数据类型,并匹配其路径下的所有文件,同时为每个文件创建一个harvester

  • 目前支持两种prospector:log和stdin

Filebeat如何保持文件的状态?

  • Filebeat会保存每个文件的状态并经常将状态刷新到磁盘上的注册文件中,路径为 ./data/registory中
  • 该状态会记住harvester正在读取的最后偏移量,并确保发送到所有日志行
  • 如果输出的服务器无法访问时,Filebeat会跟踪最后发送的行,待服务再次可用时,继续读取文件
  • Filebeat在运行时,每个prospector会在内存中保存文件的状态信息,当重启Filebeat时,将使用注册文件来重建文件状态

Filebeat部署

下载tar.gz包 & 解压

wget https://artifacts.elastic.co/downloads/beats/filebeat/filebeat-7.9.3-linux-x86_64.tar.gz
tar -xzvf filebeat-7.9.3-linux-x86_64.tar.gz
cd filebeat-7.9.3-linux-x86_64

创建配置文件(itcast.yaml)

filebeat.inputs:
- type: stdin       # 指定接收的数据来自控制台的标准输出
  enable: true
setup.template.settings:
  index.number_of_shards: 3
output.console:
  pretty: true
  enable: true

启动,测试标准输出
-e:输出到标准输出,默认输出到syslog和logs下
-c:指定配置文件
-d:输出debug信息(当filebeat输出到es等其他服务上时,通过-d可以使输出的信息同时输出到控制台的标准输出当中)

./filebeat -e -c itcast.yaml -d "publish"

读取nginx日志文件

filebeat.inputs:
- type: log        # 指定接受的数据类型是日志文件
  enable: true
  paths:
    - /var/log/nginx/*.log
  tags: ["web","test"]    # 添加自定义tag,方便管理
  fields:
    from: web-log   # 字段名字
  fields_under_root: true   # 指定自定义的字段输出为根节点,方便查看
setup.template.settings:
  index.number_of_shards: 3    # 指定索引的分区数
output.console:
  pretty: true
  enable: true

输出到elasticsearch

filebeat.inputs:
- type: log
  enable: true
  paths:
    - /var/log/nginx/*.log
  tags: ["web","test"]
  fields:
    from: web-log
  fields_under_root: true
setup.template.settings:
  index.number_of_shards: 3
output.elasticsearch:      # 指定输出到es集群中,填入es集群各节点的ip
  hosts: ["192.168.100.21:9200","192.168.100.22:9200","192.168.100.23:9200"]

前面通过filebeat采集nginx的日志文件,一些重要的信息不是很一目了然,因此可以使用一些模块
Filebeat中默认有打量的模块可用于简化配置,方便提取关键数据

[root@vms24 filebeat-7.9.3-linux-x86_64]$ ./filebeat modules list
Enabled:         # 已启用的模块

Disabled:        # 未启用的模块(将近60个可用模块)
activemq
apache
.....

为了方便观察采集到的nginx数据,可以启动nginx模块(禁用则改为disable)

[root@vms24 filebeat-7.9.3-linux-x86_64]$ ./filebeat modules enable nginx
Enabled nginx
[root@vms24 filebeat-7.9.3-linux-x86_64]$ ./filebeat modules list
Enabled:
nginx

Disabled:
activemq
apache
......

同时需要修改其配置文件

vim modules.d/nginx.yml
# 添加了两行var.path来指定要采集的日志路径
- module: nginx
  # Access logs
  access:
    enabled: true
  # 注意此处,因为日志文件是以日期进行存储,因此.log后要加上"*",error日志同理
    var.paths: ["/var/log/nginx/access.log*"]
  # Error logs
  error:
    enabled: true
    var.paths: ["/var/log/nginx/error.log*"]

再去修改filebeat的配置文件启用nginx模块

filebeat.inputs:
setup.template.settings:
  index.number_of_shards: 3    # 指定索引的分区数
output.elasticsearch:
  hosts: ["192.168.100.21:9200","192.168.100.22:9200","192.168.100.23:9200"]
filebeat.config.modules:
  path: ${path.config}/modules.d/*.yml
  reload.enabled: false

重新启动filebeat,再到es-head上查看filebeat收集到的数据会更加详细


Kibana

通过可视化的方式与elasticsearch进行交互,对索引中的数据进行搜索、查看,利用图表、表格等方式对数据进行多元化的呈现

安装kibana(tar包安装或者yum安装)

修改配置文件

[root@vms24 kibana]$ vim /etc/kibana/kibana.yml
server.host: "192.168.100.24"     # 对外暴露的ip地址
elasticsearch.hosts: ["http://192.168.100.21:9200","http://192.168.100.22:9200","http://192.168.100.23:9200"]
i18n.locale:"zh-CN"    # 可根据个人需求,修改kibana语言为中文

保存退出后,通过对外暴露的 ip:5601 启动kibana(需要稍微等待)

可以在管理中创建已经存在的索引的图表

创建Nginx日志仪表盘

修改 itcast.yaml 文件

filebeat.inputs:
setup.template.settings:
  index.number_of_shards: 3    # 指定索引的分区数
output.elasticsearch:
  hosts: ["192.168.100.21:9200","192.168.100.22:9200","192.168.100.23:9200"]
filebeat.config.modules:
  path: ${path.config}/modules.d/*.yml
  reload.enabled: false
setup.kibana:         # 在原有yaml文件基础上添加这两行,表示
  host: "192.168.100.24:5601"

执行命令,安装kibana的nginx仪表盘

[root@vms24 filebeat-7.9.3-linux-x86_64]$ ./filebeat -c itcast.yaml setup

注意!!!

有可能在执行上述命令时出现错误!!!研究了一下午得出结果

[root@vms24 filebeat-7.9.3-linux-x86_64]$ ./filebeat -c itcast.yaml setup
Overwriting ILM policy is disabled. Set `setup.ilm.overwrite: true` for enabling.

Exiting: resource 'filebeat-7.9.3' exists, but it is not an alias

会提示这个资源已经存在,但是没有别名,因此,需要删除原本创建的filebeat索引,再重新执行上述命令让其自动创建索引即可正常地进行仪表盘安装

[root@vms24 filebeat-7.9.3-linux-x86_64]$ ./filebeat -c itcast.yaml setup
Overwriting ILM policy is disabled. Set `setup.ilm.overwrite: true` for enabling.

Index setup finished.
Loading dashboards (Kibana must be running and reachable)
Loaded dashboards
Setting up ML using setup --machine-learning is going to be removed in 8.0.0. Please use the ML app instead.
See more: https://www.elastic.co/guide/en/machine-learning/current/index.html
Loaded machine learning job configurations
Loaded Ingest pipelines

安装完成后,可以在kibana的dashboard中看到filebeat安装好的各种仪表盘


Logstash

安装部署:yum或tar包

第一个logstash示例(速度极慢!)

[root@vms24 logstash]$ bin/logstash -e 'input { stdin { } } output { stdout {} }'

配置详解:
Input:可以将file、syslog或者数据库中的数据输入到 logstash中
Filter:用于过滤、格式化日志
Output:输出处理完成的数据

input {
    stdin { ... }
}
filter {
    ...
}
output {
    stdout { ... }
}

自定义日志采集

vim itcast-pipeline.conf
input {
    file {
      path => "/data/logstash/app.log"
      start_position => "beginning"
    }
}

filter {
    mutate {
      split => {"message"=>"|"}
    }
}

output {
    stdout { codec => rubydebug }
}

启动logstash

[root@vms24 logstash]$ bin/logstash -f itcast-pipeline.conf

在另一个终端往刚刚指定的日志路径写入自定义数据,观察控制台输出

echo "Nov 11 20:47:41|ERROR|读取数据失败|参数:id=1001" >> /data/logstash/app.log

可以看到控制台自动根据我们的分隔符将数据分割

{
       "host" => "vms24.rhce.cc",
      "@version" => "1",
       "message" => [
        [0] "Nov 11 20:47:41",
        [1] "ERROR",
        [2] "读取数据失败",
        [3] "参数:id=1001"
    ],
    "@timestamp" => 2020-11-11T12:49:00.292Z,
          "path" => "/data/logstash/app.log"
}

输出到elasticsearch

vim itcast-pipeline.conf
input {
    file {
      path => "/data/logstash/app.log"
      start_position => "beginning"
    }
}

filter {
    mutate {
      split => {"message"=>"|"}
    }
}

output {
    elasticsearch {
        hosts => [ "192.168.100.21:9200","192.168.100.22:9200","192.168.100.23:9200" ]
    }
}

同样指定配置文件并运行logstash后,输入数据到app.log中,可以在es-head中查看到新创建出来的logstash索引,同时可以查看到其中的数据

课后感:由于其运行速度问题,logstash中常传入那些有需要过滤或者格式化的数据,而其他数据则是由beats直接发往elasticsearch上进行存储
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值