認識Beats
Beats是用於單用途數據托運人的平台。它們以輕量級代理的形式安裝,並將來自成百上千台機器的數據發送到Logstash或Elasticsearch。
(畫外音:通俗地理解,就是采集數據,並上報到Logstash或Elasticsearch)
Beats對於收集數據非常有用。它們位於你的服務器上,將數據集中在Elasticsearch中,Beats也可以發送到Logstash來進行轉換和解析。
為了捕捉(捕獲)數據,Elastic提供了各種Beats:
Beats可以直接(或者通過Logstash)將數據發送到Elasticsearch,在那里你可以進一步處理和增強數據,然后在Kibana中將其可視化。
Filebeat
第1步:安裝Filebeat
第2步:配置Filebeat
配置文件:filebeat.yml
為了配置Filebeat:
1. 定義日志文件路徑
對於最基本的Filebeat配置,你可以使用單個路徑。例如:
filebeat.inputs: - type: log enabled: true paths: - /var/log/*.log
在這個例子中,獲取在/var/log/*.log路徑下的所有文件作為輸入,這就意味着Filebeat將獲取/var/log目錄下所有以.log結尾的文件。
為了從預定義的子目錄級別下抓取所有文件,可以使用以下模式:/var/log/*/*.log。這將抓取/var/log的子文件夾下所有的以.log結尾的文件。它不會從/var/log文件夾本身抓取。目前,不可能遞歸地抓取這個目錄下的所有子目錄下的所有.log文件。
(畫外音:
假設配置的輸入路徑是/var/log/*/*.log,假設目錄結構是這樣的:
那么只會抓取到2.log和3.log,而不會抓到1.log和4.log。因為/var/log/aaa/ccc/1.log和/var/log/4.log不會被抓到。
)
2. 如果你發送輸出目錄到Elasticsearch(並且不用Logstash),那么設置IP地址和端口以便能夠找到Elasticsearch:
output.elasticsearch: hosts: ["192.168.1.42:9200"]
3. 如果你打算用Kibana儀表盤,可以這樣配置Kibana端點:
setup.kibana: host: "localhost:5601"
4. 如果你的Elasticsearch和Kibana配置了安全策略,那么在你啟動Filebeat之前需要在配置文件中指定訪問憑據。例如:
output.elasticsearch: hosts: ["myEShost:9200"] username: "filebeat_internal" password: "{pwd}" setup.kibana: host: "mykibanahost:5601" username: "my_kibana_user" password: "{pwd}"
第3步:配置Filebeat以使用Logstash
如果你想使用Logstash對Filebeat收集的數據執行額外的處理,那么你需要將Filebeat配置為使用Logstash。
output.logstash: hosts: ["127.0.0.1:5044"]
第4步:在Elasticsearch中加載索引模板
在Elasticsearch中,索引模板用於定義設置和映射,以確定如何分析字段。(畫外音:相當於定義索引文檔的數據結構,因為要把采集的數據轉成標准格式輸出)
Filebeat包已經安裝了推薦的索引模板。如果你接受filebeat.yml中的默認配置,那么Filebeat在成功連接到Elasticsearch以后會自動加載模板。如果模板已經存在,不會覆蓋,除非你配置了必須這樣做。
通過在Filebeat配置文件中配置模板加載選項,你可以禁用自動模板加載,或者自動加載你自己的目標。
配置模板加載
默認情況下,如果Elasticsearch輸出是啟用的,那么Filebeat會自動加載推薦的模板文件 ——— fields.yml。
- 加載不同的模板
-
setup.template.name: "your_template_name" setup.template.fields: "path/to/fields.yml"
覆蓋一個已存在的模板
-
setup.template.overwrite: true
禁用自動加載模板
-
setup.template.enabled: false
修改索引名稱
-
# 默認情況下,Filebeat寫事件到名為filebeat-6.3.2-yyyy.MM.dd的索引,其中yyyy.MM.dd是事件被索引的日期。為了用一個不同的名字,你可以在Elasticsearch輸出中設置index選項。例如:
output.elasticsearch.index: "customname-%{[beat.version]}-%{+yyyy.MM.dd}" setup.template.name: "customname" setup.template.pattern: "customname-*" setup.dashboards.index: "customname-*"
手動加載模板
./filebeat setup --template -E output.logstash.enabled=false -E 'output.elasticsearch.hosts=["localhost:9200"]'
第5步:設置Kibana dashboards
Filebeat附帶了Kibana儀表盤、可視化示例。在你用dashboards之前,你需要創建索引模式,filebeat-*,並且加載dashboards到Kibana中。為此,你可以運行setup命令或者在filebeat.yml配置文件中配置dashboard加載。
./filebeat setup --dashboards
第6步:啟動Filebeat
./filebeat -e -c filebeat.yml -d "publish"
第7步:查看Kibana儀表板示例
http://127.0.0.1:5601
完整的配置
#=========================== Filebeat inputs ============== filebeat.inputs: - type: log enabled: true paths: - /var/log/*.log #============================== Dashboards =============== setup.dashboards.enabled: false #============================== Kibana ================== setup.kibana: host: "192.168.101.5:5601" #-------------------------- Elasticsearch output --------- output.elasticsearch: hosts: ["localhost:9200"]
啟動Elasticsearch
/usr/local/programs/elasticsearch/elasticsearch-6.3.2/bin/elasticsearch
啟動Kibana
/usr/local/programs/kibana/kibana-6.3.2-linux-x86_64/bin/kibana
設置dashboard
./filebeat setup --dashboards
啟動Filebeat
./filebeat -e -c filebeat.yml -d "publish"
瀏覽器訪問 http://192.168.101.5:5601
查看索引
請求:
curl -X GET "localhost:9200/_cat/indices?v"
響應:
health status index uuid pri rep docs.count docs.deleted store.size pri.store.size yellow open bank 59jD3B4FR8iifWWjrdMzUg 5 1 1000 0 475.1kb 475.1kb green open .kibana DzGTSDo9SHSHcNH6rxYHHA 1 0 153 23 216.8kb 216.8kb yellow open filebeat-6.3.2-2018.08.08 otgYPvsgR3Ot-2GDcw_Upg 3 1 255 0 63.7kb 63.7kb yellow open customer DoM-O7QmRk-6f3Iuls7X6Q 5 1 1 0 4.5kb 4.5kb
其它相關