前言
logstash本身就可以具有文件數據采集的功能了,為什么還需要在前面加一層filebeat?理由如下:
logstash是使用Java編寫,插件是使用JRuby編寫,對機器的資源要求會比較高,在logstash中做數據的邏輯過濾已經很吃服務器性能了(即logstash 具有filter功能,能過濾分析日志)。為了分攤當前服務器cpu資源,所以將使用GO編寫的輕量級的filebeat作為單獨組件,放在待收集日志的服務器上使用。
簡單概述
最近在了解ELK做日志采集相關的內容,這篇文章主要講解通過filebeat來實現日志的收集。日志采集的工具有很多種,如fluentd, flume, logstash,betas等等。首先要知道為什么要使用filebeat呢?因為logstash是jvm跑的,資源消耗比較大,啟動一個logstash就需要消耗500M左右的內存,而filebeat只需要10來M內存資源。常用的ELK日志采集方案中,大部分的做法就是將所有節點的日志內容通過filebeat送到kafka消息隊列,然后使用logstash集群讀取消息隊列內容,根據配置文件進行過濾。然后將過濾之后的文件輸送到elasticsearch中,通過kibana去展示。
官網下載地址:https://www.elastic.co/cn/downloads/beats/filebeat
官網配置說明:https://www.elastic.co/guide/en/beats/filebeat/current/configuring-howto-filebeat.html
工作原理:
Filebeat由兩個主要組件組成:prospector 和harvester。這些組件一起工作來讀取文件(tail file)並將事件數據發送到您指定的輸出
啟動Filebeat時,它會啟動一個或多個查找器,查看您為日志文件指定的本地路徑。 對於prospector 所在的每個日志文件,prospector 啟動harvester。 每個harvester都會為新內容讀取單個日志文件,並將新日志數據發送到libbeat,后者將聚合事件並將聚合數據發送到您為Filebeat配置的輸出。
harvester(收割機)
harvester :負責讀取單個文件的內容。讀取每個文件,並將內容發送到 the output
每個文件啟動一個harvester, harvester 負責打開和關閉文件,這意味着在運行時文件描述符保持打開狀態
如果文件在讀取時被刪除或重命名,Filebeat將繼續讀取文件。
這有副作用,即在harvester關閉之前,磁盤上的空間被保留。默認情況下,Filebeat將文件保持打開狀態,直到達到close_inactive狀態
關閉harvester會產生以下結果:
1)如果在harvester仍在讀取文件時文件被刪除,則關閉文件句柄,釋放底層資源。
2)文件的采集只會在scan_frequency過后重新開始。
3)如果在harvester關閉的情況下移動或移除文件,則不會繼續處理文件。
要控制收割機何時關閉,請使用close_ *配置選項
prospector(采礦者)
prospector 負責管理harvester並找到所有要讀取的文件來源。
如果輸入類型為日志,則查找器將查找路徑匹配的所有文件,並為每個文件啟動一個harvester。
每個prospector都在自己的Go協程中運行。
以下示例將Filebeat配置為從與指定的匹配的所有日志文件中收集行:
filebeat.prospectors:
- type: log paths: - /var/log/*.log - /var/path2/*.log
Filebeat目前支持兩種prospector類型:log和stdin。
每個prospector類型可以定義多次。
日志prospector檢查每個文件以查看harvester是否需要啟動,是否已經運行,
或者該文件是否可以被忽略(請參閱ignore_older)。
只有在harvester關閉后文件的大小發生了變化,才會讀取到新行。
注:Filebeat prospector只能讀取本地文件, 沒有功能可以連接到遠程主機來讀取存儲的文件或日志。
Filebeat如何保持文件的狀態?
Filebeat 保存每個文件的狀態並經常將狀態刷新到磁盤上的注冊文件中。
該狀態用於記住harvester正在讀取的最后偏移量,並確保發送所有日志行。
如果輸出(例如Elasticsearch或Logstash)無法訪問,Filebeat會跟蹤最后發送的行,並在輸出再次可用時繼續讀取文件。
在Filebeat運行時,每個prospector內存中也會保存的文件狀態信息,
當重新啟動Filebeat時,將使用注冊文件的數據來重建文件狀態,Filebeat將每個harvester在從保存的最后偏移量繼續讀取。
每個prospector為它找到的每個文件保留一個狀態。
由於文件可以被重命名或移動,因此文件名和路徑不足以識別文件。
對於每個文件,Filebeat存儲唯一標識符以檢測文件是否先前已采集過。
如果您的使用案例涉及每天創建大量新文件,您可能會發現注冊文件增長過大。請參閱注冊表文件太大?編輯有關您可以設置以解決此問題的配置選項的詳細信息。
Filebeat如何確保至少一次交付
Filebeat保證事件至少會被傳送到配置的輸出一次,並且不會丟失數據。 Filebeat能夠實現此行為,因為它將每個事件的傳遞狀態存儲在注冊文件中。
在輸出阻塞或未確認所有事件的情況下,Filebeat將繼續嘗試發送事件,直到接收端確認已收到。
如果Filebeat在發送事件的過程中關閉,它不會等待輸出確認所有收到事件。
發送到輸出但在Filebeat關閉前未確認的任何事件在重新啟動Filebeat時會再次發送。
這可以確保每個事件至少發送一次,但最終會將重復事件發送到輸出。
也可以通過設置shutdown_timeout選項來配置Filebeat以在關閉之前等待特定時間。
注意:
Filebeat的至少一次交付保證包括日志輪換和刪除舊文件的限制。如果將日志文件寫入磁盤並且寫入速度超過Filebeat可以處理的速度,或者在輸出不可用時刪除了文件,則可能會丟失數據。
在Linux上,Filebeat也可能因inode重用而跳過行。有關inode重用問題的更多詳細信息,請參閱filebeat常見問題解答。
示例:Filebeat --> Kafka (單個topic)
filebeat.prospectors: - type: log paths: - /home/hottopic/logs/b612/8086/b612.json - /home/hottopic/logs/b612/8088/b612.json json.keys_under_root: true json.overwrite_keys: true processors: - drop_fields: fields: ["offset","prospector", "tags","beat.name", "beat.version"] output: kafka: enabled: true hosts: ["172.17.65.210:9092", "172.17.65.211:9092"] topic: adsdk compression: gzip required_acks: 1 max_message_bytes: 1000000 max_procs: 1
Filebeat --> Kafka (多個topic)
filebeat.prospectors: - type: log paths: - /home/hottopic/logs/b612/8086/b612.json - /home/hottopic/logs/b612/8088/b612.json fields: log_topics: adsdk json.keys_under_root: true json.overwrite_keys: true - type: log paths: - /home/hottopic/logs/foodie/8086/foodie.json - /home/hottopic/logs/foodie/8088/foodie.json fields: log_topics: foodie-adsdk json.keys_under_root: true json.overwrite_keys: true processors: - drop_fields: fields: ["offset","prospector", "tags","beat.name", "beat.version"] output: kafka: enabled: true hosts: ["172.17.65.210:9092", "172.17.65.211:9092", "172.17.65.212:9092"] topic: '%{[fields][log_topics]}' compression: gzip required_acks: 1 max_message_bytes: 1000000 max_procs: 1
Filebeat ---> ES (單個index)
filebeat.inputs: - type: log enabled: true paths: - /home/www-data/logs/prod-zepeto-access.log json.keys_under_root: true json.overwrite_keys: true processors: - drop_fields: fields: ["agent.ephemeral_id","agent.hostname","agent.id","agent.type","agent.version","ecs.version","input.type","log.offset","log.file.path"] setup.template.enabled: false #禁用自動加載模板 setup.template.overwrite: true #覆蓋已存在的模板 #setup.template.settings: # index.number_of_shards: 3 # index.number_of_replicas: 1 #自定義index名稱時,需要指定以下三項模板配置: setup.ilm.enabled: false setup.template.name: "zepeto-nginx" setup.template.pattern: "zepeto-nginx-*" output.elasticsearch: hosts: ["172.17.213.59:9200", "172.17.213.60:9200", "172.17.213.61:9200"] index: "zepeto-nginx-access.%{+YYYY-MM}" max_procs: 1 #logging.level: warning logging.level: info logging.to_files: true logging.files: path: /var/log/filebeat name: filebeat keepfiles: 7 permissions: 0644
Filebeat ---> ES (多個index)
filebeat.inputs: - type: log enabled: true paths: - /home/www-data/logs/prod-zepeto-access.log json.keys_under_root: true json.overwrite_keys: true fields: type: prod-zepeto - type: log enabled: true paths: - /home/www-data/logs/test-zepeto-access.log json.keys_under_root: true json.overwrite_keys: true fields: type: test-zepeto processors: - drop_fields: fields: ["agent.ephemeral_id","agent.hostname","agent.id","agent.type","agent.version","ecs.version","input.type","log.offset","log.file.path"] setup.template.enabled: false #禁用自動加載模板 setup.template.overwrite: true #覆蓋已存在的模板 setup.ilm.enabled: false #禁用索引生命周期管理ilm功能(開啟時索引名稱只能為filebeat-*,所以禁用后即可使用自定義的索引名稱) output.elasticsearch: hosts: ["172.17.213.59:9200", "172.17.213.60:9200", "172.17.213.61:9200"] indices: - index: "prod-zepeto-nginx-access.%{+YYYY-MM}" when.equals: fields.type: "prod-zepeto" - index: "test-zepeto-nginx-access.%{+YYYY-MM}" when.equals: fields.type: "test-zepeto" max_procs: 1 logging.level: info logging.to_files: true logging.files: path: /var/log/filebeat name: filebeat keepfiles: 7 permissions: 0644
RPM安裝方式使用時遇到的問題
在使用RPM包進行安裝時出現日志無法輸出到/var/log/filebeat文件下,經調試后發現,標紅部按如下修改/lib/systemd/system/filbeat.service后,可正常打印日志
[Unit] Description=Filebeat sends log files to Logstash or directly to Elasticsearch. Documentation=https://www.elastic.co/products/beats/filebeat Wants=network-online.target After=network-online.target [Service] Type=simple Environment="BEAT_LOG_OPTS=-e" Environment="BEAT_CONFIG_OPTS=-c /etc/filebeat/filebeat.yml" Environment="BEAT_PATH_OPTS=-path.home /usr/share/filebeat -path.config /etc/filebeat -path.data /var/lib/filebeat -path.logs /var/log/filebeat" ExecStart=/usr/bin/filebeat Restart=always [Install] WantedBy=multi-user.target
# cat /usr/bin/filebeat
#!/usr/bin/env bash # Script to run Filebeat in foreground with the same path settings that # the init script / systemd unit file would do. exec /usr/share/filebeat/bin/filebeat \ -path.home /usr/share/filebeat \ -path.config /etc/filebeat \ -path.data /var/lib/filebeat \ -path.logs /var/log/filebeat \ "$@"
可見,/usr/bin/filebeat文件啟動時亦可加載環境配置