天天看點

Filebeat的安裝和使用

由于logstash采集日志時的資源消耗較大,故搜集日志的工作交給其他輕量級工具,再傳輸給logstash進行處理。Filebeat是比較好用的一款工具,filebeat是本地檔案的日志資料采集器。 作為伺服器上的代理安裝,Filebeat監視日志目錄或特定日志檔案,tail file,并将它們轉發給Elasticsearch或Logstash進行索引、kafka 等。

官方網站:https://www.elastic.co/guide/en/beats/filebeat/current/filebeat-installation.html

使用filebeat之前可根據實際需要選擇安裝好logstash、elastic、kibana等元件,本次采用filebeat和ELK聯合使用,故已經安裝好ELK元件

1、安裝filebeat

本次采用yum安裝,需要配置yum源(由于filebeat會和ELK配套使用,是以該YUM源裡面也會有elk元件的yum安裝,配置好可同時用于安裝ELK)

1.1、yum源配置。進入yum.repos.d建立一個.repo格式的檔案

cd /etc/yum.repos.d

vim 檔案名.repo

然後将下述内容添加到檔案中,儲存退出

[filebeat]

name=Elasticsearch repository for 7.x packages

baseurl=https://artifacts.elastic.co/packages/7.x/yum

gpgcheck=0

gpgkey=https://artifacts.elastic.co/GPG-KEY-elasticsearch

enabled=1

autorefresh=1

type=rpm-md

1.2、yum安裝filebeat

yum -y install filebeat

rpm -qa filebeat     

安裝好之後可用whereis filebeat檢視下安裝情況,友善找配置檔案

2、配置filebeat

2.1、filebeat配置檔案: filebeat.yml

find / -name filebeat.yml

vim /etc/filebeat/filebeat.yml

Filebeat的安裝和使用

2.2 具體子產品的配置

2.2.1、filebeat.inputs

Filebeat的安裝和使用

将 enable 改為 true

paths 為要搜集的目錄的路徑

2.2.2、filebeat.output

Filebeat的安裝和使用

output有多種輸出模式,可直接輸出到logstash、elasticsearch、kibana和redis,根據實際需要選擇,這裡選擇輸出到logstash

将其他的output注釋掉,将logstash釋放進行配置

Filebeat的安裝和使用

hosts為logstash伺服器的位址和端口,端口一般預設為5044

配置好之後啟動filebeat

cd /usr/share/filebeat/bin

背景啟動filebeat

如果直接啟動運作會報錯:

loading config file: stat filebeat.yml: no such file or direct

是因為未找到配置檔案所緻,這裡采取簡單的方法:在運作時直接加上配置檔案路徑

./filebeat -e -c /etc/filebeat/filebeat.yml

注意:filebeat沒有監聽端口,主要看日志和程序

Filebeat的安裝和使用
  1. logstash的設定

在logstash服務中編寫配置檔案

其中input需要用beats接收,設定ip和端口

input {

    beats {

        host => "192.168.1.86"

        port => 5044

    }

}

注意:此處的host為logstash伺服器的ip,如果啟用本機的logstash服務該項可以注釋掉。port為filebeat.yml檔案配置的端口

Output先輸出到控制台調試

output{

 stdout {

                codec => rubydebug

        }

}

調用配置檔案啟動logstash(調試時可不用背景啟動)

./logstash -f filebeattest.conf --path.data=/logdata/filebeat

--path.data=/logdata/filebeat 為在一台伺服器中啟動多執行個體,需要制定具體的資料存放路徑,此處以後再詳解

成功部署後logstash就能成功輸出日志資訊了

其他相關操作:

測試filebeat啟動後,檢視相關輸出資訊:

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

背景方式啟動filebeat:

 ./filebeat -e -c filebeat.yml >/dev/null 2>&1 &  将所有标準輸出及标準錯誤輸出到/dev/null空裝置,即沒有任何輸出

./filebeat -e -c filebeat.yml > filebeat.log &

停止filebeat:查找程序ID并kill掉:

ps -ef |grep filebeat<br>kill -9  程序号

繼續閱讀