一、安裝filebeat
簡介
Beats 是安裝在服務器上的數據中轉代理。
Beats 可以將數據直接傳輸到 Elasticsearch 或傳輸到 Logstash 。
Beats 有多種類型,可以根據實際應用需要選擇合適的類型。
常用的類型有:
- Packetbeat:網絡數據包分析器,提供有關您的應用程序服務器之間交換的事務的信息。
- Filebeat:從您的服務器發送日志文件。
- Metricbeat:是一個服務器監視代理程序,它定期從服務器上運行的操作系統和服務收集指標。
- Winlogbeat:提供Windows事件日志。
參考
更多 Beats 類型可以參考:community-beats
FileBeat 的作用
相比 Logstash,FileBeat 更加輕量化。
在任何環境下,應用程序都有停機的可能性。 Filebeat 讀取並轉發日志行,如果中斷,則會記住所有事件恢復聯機狀態時所在位置。
Filebeat帶有內部模塊(auditd,Apache,Nginx,System和MySQL),可通過一個指定命令來簡化通用日志格式的收集,解析和可視化。
FileBeat 不會讓你的管道超負荷。FileBeat 如果是向 Logstash 傳輸數據,當 Logstash 忙於處理數據,會通知 FileBeat 放慢讀取速度。一旦擁塞得到解決,FileBeat 將恢復到原來的速度並繼續傳播。
基於docker安裝Filebeat
官網安裝文檔,請參考鏈接:
https://www.elastic.co/guide/en/beats/filebeat/current/filebeat-installation.html
新建目錄
mkdir /opt/filebeat
dockerfile
FROM ubuntu:16.04 # 修改更新源為阿里雲 ADD sources.list /etc/apt/sources.list ADD filebeat-6.4.3-amd64.deb / ADD filebeat.yml / # 安裝jdk RUN apt-get update && apt-get install -y openjdk-8-jdk --allow-unauthenticated && apt-get clean all && \ dpkg -i filebeat-6.4.3-amd64.deb && rm -rf filebeat-6.4.3-amd64.deb && \ \cp filebeat.yml /etc/filebeat/filebeat.yml && rm -rf filebeat.yml #EXPOSE 9092 # 添加啟動腳本 ADD run.sh . RUN chmod 755 run.sh ENTRYPOINT [ "/run.sh"]
filebeat.yml
這個是默認的配置文件,已經去除了空行和注釋部分
filebeat.inputs: - type: log enabled: false paths: - /var/log/*.log filebeat.config.modules: path: ${path.config}/modules.d/*.yml reload.enabled: false setup.template.settings: index.number_of_shards: 3 setup.kibana: output.elasticsearch: hosts: ["localhost:9200"]
run.sh
#!/bin/bash set -e # 添加時區 TZ=Asia/Shanghai ln -snf /usr/share/zoneinfo/$TZ /etc/localtime && echo $TZ > /etc/timezone # 判斷變量是否存在 if [ -z $elasticsearch ];then echo "elasticsearch參數為空" exit fi # 開啟日志收集 sed -i "3s?false?true?g" /etc/filebeat/filebeat.yml # 修改elasticsearch地址 sed -i "13s?localhost?$elasticsearch?g" /etc/filebeat/filebeat.yml # 啟動filebeat /etc/init.d/filebeat start # hold住進 tail -f /etc/filebeat/filebeat.yml
目錄結構如下:
./ ├── dockerfile ├── filebeat-6.4.3-amd64.deb ├── filebeat.yml ├── run.sh └── sources.list
生成鏡像
docker build -t filebeat-6.4.3 /opt/filebeat
配置
配置文件
首先,需要知道的是:filebeat.yml
是 filebeat 的配置文件。配置文件的路徑會因為你安裝方式的不同而變化。
Beat 所有系列產品的配置文件都基於 YAML 格式,FileBeat 當然也不例外。
filebeat.yml 部分配置示例:
filebeat: prospectors: - type: log paths: - /var/log/*.log multiline: pattern: '^[' match: after
更多 filebeat 配置內容可以參考:配置 filebeat
更多 filebeat.yml 文件格式內容可以參考:filebeat.yml 文件格式
重要配置項
filebeat.prospectors
(文件監視器)用於指定需要關注的文件。
示例
filebeat.prospectors: - type: log enabled: true paths: - /var/log/*.log
output.elasticsearch
如果你希望使用 filebeat 直接向 elasticsearch 輸出數據,需要配置 output.elasticsearch 。
示例
output.elasticsearch: hosts: ["192.168.1.42:9200"]
output.logstash
如果你希望使用 filebeat 向 logstash輸出數據,然后由 logstash 再向elasticsearch 輸出數據,需要配置 output.logstash。
注意
相比於向 elasticsearch 輸出數據,個人更推薦向 logstash 輸出數據。
因為 logstash 和 filebeat 一起工作時,如果 logstash 忙於處理數據,會通知 FileBeat 放慢讀取速度。一旦擁塞得到解決,FileBeat 將恢復到原來的速度並繼續傳播。這樣,可以減少管道超負荷的情況。
示例
output.logstash: hosts: ["127.0.0.1:5044"]
此外,還需要在 logstash 的配置文件(如 logstash.conf)中指定 beats input 插件:

input { beats { port => 5044 # 此端口需要與 filebeat.yml 中的端口相同 } } # The filter part of this file is commented out to indicate that it is # optional. # filter { # # } output { elasticsearch { hosts => "localhost:9200" manage_template => false index => "%{[@metadata][beat]}-%{[@metadata][version]}-%{+YYYY.MM.dd}" document_type => "%{[@metadata][type]}" } }
setup.kibana
如果打算使用 Filebeat 提供的 Kibana 儀表板,需要配置 setup.kibana 。
示例
setup.kibana: host: "localhost:5601"
setup.template.settings
在 Elasticsearch 中,索引模板用於定義設置和映射,以確定如何分析字段。
在 Filebeat 中,setup.template.settings 用於配置索引模板。
Filebeat 推薦的索引模板文件由 Filebeat 軟件包安裝。如果您接受 filebeat.yml 配置文件中的默認配置,Filebeat在成功連接到 Elasticsearch 后自動加載模板。
您可以通過在 Filebeat 配置文件中配置模板加載選項來禁用自動模板加載,或加載自己的模板。您還可以設置選項來更改索引和索引模板的名稱。
參考
更多內容可以參考:filebeat-template
說明
如無必要,使用 Filebeat 配置文件中的默認索引模板即可。
setup.dashboards
Filebeat 附帶了示例 Kibana 儀表板。在使用儀表板之前,您需要創建索引模式 filebeat- *
,並將儀表板加載到Kibana 中。為此,您可以運行 setup
命令或在 filebeat.yml
配置文件中配置儀表板加載。
為了在 Kibana 中加載 Filebeat 的儀表盤,需要在 filebeat.yml
配置中啟動開關:
setup.dashboards.enabled: true
參考
更多內容可以參考:configuration-dashboards
命令
filebeat 提供了一系列命令來完成各種功能。
執行命令方式:
./filebeat COMMAND
參考
更多內容可以參考:command-line-options
說明
個人認為命令行沒有必要一一掌握,因為絕大部分功能都可以通過配置來完成。且通過命令行指定功能這種方式要求每次輸入同樣參數,不利於固化啟動方式。
最重要的當然是啟動命令 run 了。
示例 指定配置文件啟動
./filebeat run -e -c filebeat.yml -d "publish" ./filebeat -e -c filebeat.yml -d "publish" # run 可以省略
模塊
Filebeat 提供了一套預構建的模塊,讓您可以快速實施和部署日志監視解決方案,並附帶示例儀表板和數據可視化。這些模塊支持常見的日志格式,例如Nginx,Apache2和MySQL 等。
運行模塊的步驟
- 配置 elasticsearch 和 kibana
output.elasticsearch: hosts: ["myEShost:9200"] username: "elastic" password: "elastic" setup.kibana: host: "mykibanahost:5601" username: "elastic" password: "elastic
- 初始化環境
執行下面命令,filebeat 會加載推薦索引模板。
./filebeat setup -e
- 指定模塊
執行下面命令,指定希望加載的模塊。
./filebeat -e --modules system,nginx,mysql
參考
更多內容可以參考: 配置 filebeat 模塊 | filebeat 支持模塊
原理
Filebeat 有兩個主要組件:
harvester:負責讀取一個文件的內容。它會逐行讀取文件內容,並將內容發送到輸出目的地。
prospector:負責管理 harvester 並找到所有需要讀取的文件源。比如類型是日志,prospector 就會遍歷制定路徑下的所有匹配要求的文件。
filebeat.prospectors: - type: log paths: - /var/log/*.log - /var/path2/*.log
Filebeat保持每個文件的狀態,並經常刷新注冊表文件中的磁盤狀態。狀態用於記住 harvester 正在讀取的最后偏移量,並確保發送所有日志行。
Filebeat 將每個事件的傳遞狀態存儲在注冊表文件中。所以它能保證事件至少傳遞一次到配置的輸出,沒有數據丟失。
資料
二、測試filebeat
環境介紹
操作系統 | IP地址 | 相關鏡像 |
ubuntu-16.04.5-server-amd64 | 192.168.0.162 | elasticsearch-6.4.3,elasticsearch-head-ubuntu,kibana-6.4.3 |
ubuntu-16.04.5-server-amd64 | 192.168.0.156 | filebeat-6.4.3 |
安裝elasticsearch
關於elasticsearch的安裝,請參考鏈接:
https://www.cnblogs.com/xiao987334176/p/9957879.html#autoid-3-6-0
安裝ElasticSearch-head
關於ElasticSearch-head插件安裝,請參考鏈接:
https://www.cnblogs.com/xiao987334176/p/10006460.html
本文使用的是基於ubuntu16.04的鏡像
請注意要修改 elasticsearch 中的2處配置,允許head插件可以訪問es
安裝kibana
關於kibana的安裝,請參考鏈接:
https://www.cnblogs.com/xiao987334176/p/9957879.html#autoid-5-2-0
注意修改run.sh腳本,這里的ip定死了,要改成動態的。

#!/bin/bash # 添加時區 TZ=Asia/Shanghai ln -snf /usr/share/zoneinfo/$TZ /etc/localtime && echo $TZ > /etc/timezone #elasticservers="192.168.91.128" if [ -z $elasticservers ];then echo "elasticservers參數為空" exit fi # 修改配置文件 sed -i '7s@#server.host: "localhost"@server.host: "0.0.0.0"@g' /etc/kibana/kibana.yml sed -i '28s@#elasticsearch.url: "http://localhost:9200"@elasticsearch.url: "http://localhost:9200"@g' /etc/kibana/kibana.yml sed -i "s?localhost:9200?$elasticservers?g" /etc/kibana/kibana.yml # 啟動 /usr/share/kibana/bin/kibana "-c /etc/kibana/kibana.yml"
然后重新生成鏡像
docker build -t kibana-6.4.3 /opt/kibana
啟動相關容器
注意啟動順序是 elasticsearch-->elasticsearch-head-->filebeat-->kibana
啟動elasticsearch
登錄到 192.168.0.162 服務器
docker run -d -it --restart=always -p 9200:9200 elasticsearch-6.4.3
訪問elasticsearch頁面
http://192.168.0.162:9200/
效果如下:
啟動elasticsearch-head
登錄到 192.168.0.162 服務器
docker run -d -it --restart=always -p 9100:9100 elasticsearch-head-ubuntu
訪問elasticsearch-head頁面,輸入elasticsearch地址,點擊連接,效果如下:
啟動 filebeat
登錄到 192.168.0.156 服務器
docker run -d -it --restart=always -e elasticsearch=192.168.0.162 filebeat-6.4.3
注意要指定elasticsearch參數才行
等待1分鍾,刷新elasticsearch-head頁面
點擊 數據瀏覽,發現數據已經發送過來了
啟動kibana
登錄到 192.168.0.162 服務器
docker run -d -it --restart=always -p 5601:5601 -e elasticsearch=192.168.0.162 kibana-6.4.3
等待1分鍾,訪問kibana頁面
http://192.168.0.162:5601
選擇No,不參加體驗計划
點擊左邊的Discover,下面已經提示了index值
將index值復制過來,選擇下一步
選擇時間戳
點擊左側的discover,效果如下:
上面只是用的默認配置,修改了2個參數而已。
filebeat還可以配置一些復雜的參數,比如:
#path logs filebeat.inputs: - type: log paths: #- /var/log/*.log - /opt/*log/*.log - /opt/*log/*/*.log - /opt/*log/*/*/*.log - /opt/*log/*/*/*/*.log multiline.pattern: '^\d{4}\-\d{2}\-\d{2}' multiline.negate: true multiline.match: after processors: - drop_fields: # This is optional, doesn't make a difference fields: ['@timestamp'] - rename: fields: - from: message to: '@timestamp' fail_on_error: true #elasticsearch config output.elasticsearch.hosts: ["192.168.0.24:9200"] output.elasticsearch.index: "xxx-%{+yyyy.MM.dd}" setup.template.name: "xxx" setup.template.pattern: "xxx-*" #setup.dashboards.index: "customname-*" #filebeat filebeat.registry_file: /var/log/filebeat/filebeat-reg logging.to_files: true logging.files: path: /var/log/filebeat/logs
如果需要屏幕和文件日志同時寫入,使用命令
filebeat -c filebeat.yml -e -d "*"
本文參考鏈接:
https://www.cnblogs.com/jingmoxukong/p/8185321.html