1. 程式人生 > >ELK的使用——Filebeat給Logstash採集日誌

ELK的使用——Filebeat給Logstash採集日誌

為什麼使用Filebeat給Logstash收集日誌

Logstash本身也是能收集日誌的,只不過佔用的cup和記憶體比較多,不是安全傳輸,是框架級別。filter功能過濾分析,過濾能力強,使用面很廣。

Filebeat收集日誌是佔用的CPU和記憶體給Logstash少很多,效能給Logstash好很多,收集日誌速度比Logstash快很多。

即使Logstash既能過濾也可以收集日誌,但是我們考慮而資源佔用和效能問題,果斷的選擇使用Filebeat給Logstash收集日誌。

那這樣,Filebeat能都代替Logstash呢? 必然是不能的,Filebeat的過濾功能弱於Logstash,雖然使用Filebeat收集日誌,但過濾還是使用自己的更強大。

具體操作:

1、先將Filebeat安裝好。

2、配置logstash.conf檔案

input {
     beats {
    port => 5041  //配置檔案輸入的埠號。
  }
}

output {
  stdout {
   codec => rubydebug
  }

  elasticsearch {
    hosts => "elasticsearch 的ip:9200" 
    index => "log-%{+YYYY.MM.dd}"


  }
}

3、配置filebeat.yml檔案

在開頭配置輸入*.log的位置。

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: - /var/log/*.log //日誌的具體位置。
output.logstash:
  # The Logstash hosts
  hosts: ["Logstash的IP:5041"]  
  //5041埠號要和logstash輸入的埠號一致。

4、執行命令進行測試 在filebeat安裝的目錄下執行

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

在logstash安裝目錄下執行

bin/logstash -f logstash.conf

A、在/var/log目錄下新建T.log檔案並寫入”tw”。

a、filebeat輸出的資訊是

2018-09-16T16:11:02.533+0800
INFO
log/harvester.go:251    
Harvester started for file: /var/log/1.log

b、logstash輸出的資訊是

這裡寫圖片描述

通過以上的配置讓Filebeat收集日誌扔給Logstash來過濾,然後再扔給Elasticsearch。

ELK使用Filebeat給Logstash採集日誌,成功!