提到ELK,就不得不提到EFK,通常意義上說,EFK是指用filebeat代替logstash形成的新組合。(哈,也有是指Fluentd的,這個我們之後再說)
Filebeat 是基於原先 logstash-forwarder 的源碼改造出來的,無需依賴 Java 環境就能運行,安裝包10M不到。
而且如果日誌的量很大,Logstash 會遇到資源佔用高的問題,爲解決這個問題,我們引入了Filebeat。Filebeat 是基於 logstash-forwarder 的源碼改造而成,用 Golang 編寫,無需依賴 Java 環境,效率高,佔用內存和 CPU 比較少,非常適合作爲 Agent 跑在服務器上,來實現日誌轉發的功能。
還是去官網下載https://www.elastic.co/cn/downloads/beats/filebeat。本次演示還是以最新版的filebeat7.5.1爲例(以前版本的filebeat配置文件格式參數上可能有一些改變,不過大同小異)。
cd /usr/local/src/
wget https://artifacts.elastic.co/downloads/beats/filebeat/filebeat-7.5.1-amd64.deb
dpkg -i filebeat-7.5.1-amd64.deb
基礎配置
配置文件也很簡單,如果想要寫入文件,則配置如下
grep -v "#" /etc/filebeat/filebeat.yml | grep -v "^$"
filebeat.inputs:
- type: log
paths:
- /var/log/syslog
exclude_lines: ["^DBG"]
exclude_files: [".gz$"]
tags: "syslog-filebeat"
output.file:
path: "/tmp"
filename: "filebeat.txt"
paths
路徑支持正則通配符寫法,exclude是設置不匹配的文件格式。而且filebeat也支持同時從多個路徑收集寫成如下配置
filebeat.inputs:
- type: log
paths:
- /var/log/syslog
exclude_lines: ["^DBG"]
exclude_files: [".gz$"]
tags: "syslog-filebeat"
- type: log
paths:
- /var/log/nginx/access.log
exclude_lines: ["^DBG"]
exclude_files: [".gz$"]
document_type: "nginx-accesslog-filebeat
output.file:
path: "/tmp"
filename: "filebeat.txt"
同樣,filebeat支持寫入redis和kafka
filebeat.inputs:
- type: log
paths:
- /var/log/syslog
exclude_lines: ["^DBG"]
exclude_files: [".gz$"]
tags: "filebeat-redis-syslog"
output.redis:
hosts: ["192.168.32.31:6379"]
key: "filebeat-system-log" #爲了後期日誌處理,建議自定義 key 名稱
db: 1 #使用第幾個庫
timeout: 5 #超時時間
password: 123456 #redis 密碼
想要寫入kafka則添加output插件,配置如下
filebeat.inputs:
- type: log
paths:
- /var/log/syslog
exclude_lines: ["^DBG"]
exclude_files: [".gz$"]
tags: "filebeat-kafka-syslog"
output.kafka: #寫入 kafka
hosts: ["192.168.15.11:9092","192.168.15.12:9092","192.168.15.13:9092"]
topic: "systemlog-1512-filebeat"
partition.round_robin:
reachable_only: true
required_acks: 1 #本地寫入完成
compression: gzip #開啓壓縮
max_message_bytes: 1000000 #消息最大值
配置詳解
input
也就是設置日誌收集的來源,需要的屬性有type
,path
,根據官方文檔,現在版本常用寫法爲,
filebeat.inputs:
- type: log
paths:
- /var/log/system.log
- /var/log/wifi.log
- type: log
paths:
- "/var/log/apache2/*"
fields:
apache: true
fields_under_root: true
其中type的類型很多
- Log 日誌文件,必須有PATH,官方示例如下:
filebeat.inputs:
- type: log
paths:
- /var/log/system.log
- /var/log/wifi.log
- /var/log/*.log
- type: log
paths:
- "/var/log/apache2/*"
fields:
apache: true
fields_under_root: true
- Stdin 標準輸入,沒有PATH,官方示例如下:
filebeat.inputs:
- type: stdin
- Container 容器中日誌,必須有PATH,官方示例如下:
filebeat.inputs:
- type: container
paths:
- '/var/lib/docker/containers/*/*.log'
- Kafka 從kafka中讀取數據,官方示例如下:
filebeat.inputs:
- type: kafka
hosts: ["<your event hub namespace>.servicebus.windows.net:9093"]
topics: ["<your event hub instance>"]
group_id: "<your consumer group>"
username: "$ConnectionString"
password: "<your connection string>"
ssl.enabled: true
- Redis 從redis中讀取數據,官方示例如下:
filebeat.inputs:
- type: redis
hosts: ["localhost:6379"]
password: "${redis_pwd}"
- UDP 開放UDP端口來接受數據,可設置單條最大數據上限,不定義默認爲20MiB。
filebeat.inputs:
- type: udp
max_message_size: 10KiB
host: "localhost:8080"
- Docker 也支持直接從容器中讀取數據, containers.ids是必須定義說明,可以用*代表所有容器。
filebeat.inputs:
- type: docker
containers.ids:
- '8b6fe7dc9e067b58476dc57d6986dd96d7100430c5de3b109a99cd56ac655347'
- TCP 用法與UDP相同,設置監聽的主機和端口。
filebeat.inputs:
- type: tcp
max_message_size: 10MiB
host: "localhost:9000"
- Syslog 監聽系統日誌,指定傳輸協議即可。類似TCP和UDP。
filebeat.inputs:
- type: syslog
protocol.udp:
host: "localhost:9000"
filebeat.inputs:
- type: syslog
protocol.tcp:
host: "localhost:9000"
- s3 AWS的對象存儲日誌,不過多介紹
- NetFlow Cisco設備網絡設備的日誌,不過多介紹
- Google Pub/Sub google雲的訂閱發佈模式協議數據,不過多介紹。
一般來說,我們都寫log,就可以滿足我們絕大多數場景的使用了。除了type
、path
這倆常用的input屬性外,還有兩個設置屬性,我們也經常會用到,就是include_lines
、exclude_lines
,顧名思義,就是包括和排除,配合path中的通配符,可以幫助我們更靈活的指定要收集的日誌文件。
還有一個很常用的屬性就是tags
,可以寫多個,用[]
括起來就可以。因爲在filebeat中因爲有自帶type
關鍵字,所以我們在之後篩選日誌的時候,無法通過type字段來區分不同的日誌源了,所以我們可以通過自定義tags
字段,來實現之前在logstash上type的功能,這樣在我們收集到的日誌中,會自動加入tags
標籤屬性,然後通過logstash的篩選時,就可以對tags
關鍵字做判斷了。
output
輸出選項有Elasticsearch
、Logstash
、Kafka
、Redis
、File
、Console
、Elastic Cloud
。
- File
輸出到文件中是最簡單的設置了,一般用於測試。
output.file:
path: "/tmp/filebeat" #輸出文件路徑
filename: filebeat #輸出日誌名稱,超過大小限制後會自動添加數字後綴
#rotate_every_kb: 10000 #每個日誌文件大小限制
#number_of_files: 7 #路徑下最大的儲存日誌文件數量,超過此值後自動刪除最早的日誌文件,默認爲7。
#permissions: 0600 #創建的日誌文件的權限
- Logstash
filebeat支持直接將數據輸出值logstash主機。
output.logstash:
hosts: ["127.0.0.1:5044"]
而logstash主機需要設置輸入爲beats,纔可以順利接收filebeat的數據。
input {
beats {
port => 5044
}
}
output {
elasticsearch {
hosts => ["http://localhost:9200"]
index => "%{[@metadata][beat]}-%{[@metadata][version]}"
}
}
- redis
輸出值redis,上面有說明,這裏就不詳細介紹了。
output.redis:
hosts: ["localhost"]
password: "my_password"
key: "filebeat"
db: 0
timeout: 5
- kafka
輸出至kafka。
output.kafka:
# initial brokers for reading cluster metadata
hosts: ["kafka1:9092", "kafka2:9092", "kafka3:9092"]
# message topic selection + partitioning
topic: '%{[fields.log_topic]}'
partition.round_robin:
reachable_only: false
required_acks: 1
compression: gzip
max_message_bytes: 1000000
也可以輸出至kafka的不同的topic中
Rule settings:
topic
The topic format string to use. If this string contains field references, such as %{[fields.name]}, the fields must exist, or the rule fails.
mappings
A dictionary that takes the value returned by topic and maps it to a new >name.
default
The default string value to use if mappings does not find a match.
when
A condition that must succeed in order to execute the current rule. All the conditions supported by processors are also supported here.
官方示例如下:
output.kafka:
hosts: ["localhost:9092"]
topic: "logs-%{[beat.version]}"
topics:
- topic: "critical-%{[beat.version]}"
when.contains:
message: "CRITICAL"
- topic: "error-%{[beat.version]}"
when.contains:
message: "ERR"
- Elaticsearch
可以直接將數據輸出給elaticsearch服務器,不過一般來說我們不會這樣做,一般是會經過logstash來篩選之後再傳入elaticsearch。官方示例如下:
output.elasticsearch:
hosts: ["https://localhost:9200"]
username: "filebeat_internal"
password: "YOUR_PASSWORD"
- Console
輸出至屏幕終端顯示。pretty官方的介紹爲If pretty is set to true, events written to stdout will be nicely formatted. The default is false
,示例如下:
output.console:
pretty: true