ELK框架Logstash配合Filebeats和kafka使用

ELK框架Logstash配合Filebeats和kafka使用

本文目录

配置文件结构

配置文件为:logstash.yml

需要自己新建conf文件,设置inputfilteroutput,文件结构如下,自带的logstash-sample.conf内容如下

复制代码
input { 

}
filter {

}
output {

}

启动命令

复制代码
bin/logstash -f config/logstash.conf

https://www.elastic.co/guide/en/logstash/current/input-plugins.html

input为标准输入,output为标准输出

复制代码
input { 
  stdin { } 
}
output {
  elasticsearch { 
    hosts => ["localhost:9200"] 
  }
  stdout { }
}

input为log文件

output为标准输出

复制代码
input {
  # 从文件读取日志信息
  file {
    path => "/xxx/demolog/logs/myapp-info.log"
    type => "ghn"
    start_position => "beginning"
  }

}

output {
  stdout { codec => rubydebug }
}

output为es

复制代码
input {
  # 从文件读取日志信息
  file {
    path => "/xxx/demolog/log/demolog-*.log"
    type => "ghn"
    start_position => "beginning"
  }

}

output {
  # 输出到 elasticsearch
  elasticsearch {
    hosts => ["localhost:9200"] 
    user => "elastic"
    password => "xxxxxx"
    ssl => "true"
    cacert => "/xxx/elk/logstash-8.9.1/config/certs/http_ca.crt"
    index => "ghn-%{+YYYY.MM.dd}"
  }
  stdout {  }
}

input为tcp

配合springboot/springcloud使用

springboot配置

官方github:https://github.com/logfellow/logstash-logback-encoder
在pom.xml添加依赖

复制代码
        <dependency>
            <groupId>net.logstash.logback</groupId>
            <artifactId>logstash-logback-encoder</artifactId>
            <version>7.4</version>
        </dependency>

在logback-spring.xml添加配置

复制代码
    <appender name="stash" class="net.logstash.logback.appender.LogstashTcpSocketAppender">
        <destination>127.0.0.1:4560</destination>
        <!-- encoder is required -->
        <encoder class="net.logstash.logback.encoder.LogstashEncoder" />
    </appender>

    <root level="info">
        <appender-ref ref="CONSOLE"/>
        <appender-ref ref="stash" />
    </root>

logstash配置

复制代码
input {
  # 从文件读取日志信息
  tcp {
    host => "0.0.0.0"
    mode => "server"
    port => 4560
    codec => json_lines
  }

}

output {
  # 输出到 elasticsearch
  elasticsearch {
    hosts => ["localhost:9200"] 
    user => "elastic"
    password => "xxxxxx"
    ssl => "true"
    cacert => "xxx/logstash-8.9.1/config/certs/http_ca.crt"
    index => "ghn-%{+YYYY.MM.dd}"
  }
  stdout { }
  # stdout { codec => rubydebug }
}
  • logstash终端查看

  • kibana查看

input为filebeats

filebeats配置

  • 配置文件位置:filebeat-8.9.1-darwin-aarch64/filebeat.yml,修改如下部分,指定log位置为springboot的目录

    filebeat.inputs:

    • type: filestream
      enabled: true
      paths:
      • /xxx/xxx/*.log
  • 启动

    ./filebeat -e -c filebeat.yml -d "publish"

与logstash建立了连接,启动成功

logstash配置

复制代码
input {
  beats {
    port => 5044
  }
}

output {
  elasticsearch {
    hosts => ["localhost:9200"] 
    user => "elastic"
    password => "xxxxxx"
    ssl => "true"
    cacert => "/xxxx/logstash-8.9.1/config/certs/http_ca.crt"
    index => "%{[@metadata][beat]}-%{[@metadata][version]}-%{+YYYY.MM.dd}"
  }
}
  • 启动logstash

    bin/logstash -f config/logstash-filebeat.conf

  • 获取从filebeats发来的日志

kibana

  • kibana中数据视图已经能够看到

  • 查看详情

input为kafka

官方文档:https://www.elastic.co/guide/en/logstash/current/use-filebeat-modules-kafka.html

filebeats设置

复制代码
output.kafka:
  hosts: ["localhost:9092"]
  topic: "filebeat"
  codec.json:
    pretty: false

./filebeat -e -c filebeat.yml -d "publish"

logstash配置

复制代码
input {
  kafka {
    bootstrap_servers => ["localhost:9092"]
    topics => ["filebeat"]
    codec => json
  }

}

output {
  # 输出到 elasticsearch
  elasticsearch {
    hosts => ["localhost:9200"] 
    user => "elastic"
    password => "xxxxxx"
    ssl => "true"
    cacert => "/xxx/elk/logstash-8.9.1/config/certs/http_ca.crt"
    index => "ghn-%{+YYYY.MM.dd}"
  }
  stdout { }
  # stdout { codec => rubydebug }
}
  • kafka启动

  • logstash查看

  • kafka关闭

https://www.elastic.co/guide/en/logstash/current/plugins-inputs-kafka.html

相关推荐
武子康11 小时前
大数据-76 Kafka 从发送到消费:Kafka 消息丢失/重复问题深入剖析与最佳实践
大数据·后端·kafka
鼠鼠我捏,要死了捏12 小时前
Kafka Streams vs Apache Flink vs Apache Storm: 实时流处理方案对比与选型建议
kafka·apache flink·apache storm
Tapdata 钛铂数据14 小时前
TapData vs Kafka ETL Pipeline:竞争?共存?——企业实时数据策略的正确打开方式
kafka·数据同步·实时数据·kafka connect
ffyyhh9955111 天前
kafka生产者 消费者工作原理
kafka
香吧香1 天前
kafka 副本集设置和理解
kafka
Rookie小强2 天前
kafka的rebalance机制是什么
分布式·kafka
码农小灰2 天前
Kafka消息持久化机制全解析:存储原理与实战场景
java·分布式·kafka
Raisy_2 天前
05 ODS层(Operation Data Store)
大数据·数据仓库·kafka·flume
纪莫3 天前
Kafka如何保证「消息不丢失」,「顺序传输」,「不重复消费」,以及为什么会发生重平衡(reblanace)
java·分布式·后端·中间件·kafka·队列
poemyang3 天前
千亿消息“过眼云烟”?Kafka把硬盘当内存用的性能魔法,全靠这一手!
kafka·高并发·pagecache·存储架构·顺序i/o·局部性原理