- fluentd對比Logstash
對比方面 | Logstash | Fluentd |
---|---|---|
內存占用 | 啟動1G左右 | 啟動60M左右 |
CPU占用 | 較高 | 較低 |
支持插件 | 豐富 | 豐富 |
通用日志解析 | 支持grok(基于正則表達式)解析 | 支持正則表達式解析 |
特定日志類型 | 支持JSON等主流格式 | 支持JSON等主流格式 |
數據過濾 | 支持 | 支持 |
數據buffer發送 | 插件支持 | 插件支持 |
運行環境 | JRuby實現,依賴JVM環境 | CRuby、C實現,依賴Ruby環境 |
線程支持 | 支持多線程 | 多線程受GIL限制 |
- 使用docker compose部署
- 準備es-master.yml,fluent.conf ,kibana.yml配置文件
- 執行efk-master-init.sh 創建相應的文件夾、下載啟動對應的鏡像
注意:
一下的配置文件中的ip地址請更換為對應服務器的ip
-
剛創建fluentd鏡像啟動時會失敗,因為官方的docker鏡像默認沒有加入fluent-plugin-elasticsearch 插件,解決步驟如下:
- 1 . 將fluent.conf 配置文件的關于elasticsearch先刪除,再重啟docker鏡像
- 使用root用戶進入Fluentd容器內部
[root@localhost ]# docker exec -it --user root fluentd /bin/sh
- 安裝Fluentd的Elasticsearch插件
/ # fluent-gem install fluent-plugin-elasticsearch
- 更換fluent.conf配置文件的內容:
<source> @type forward @id httpd-input @label @httpd port 24224 <parse> @type json </parse> </source> <filter **> @type stdout </filter> <label @httpd> <match **> @type elasticsearch host 192.168.1.150 port 9200 type_name docker logstash_format true logstash_prefix httpd-${tag}-logs logstash_dateformat %Y-%m-%d flush_interval 1s include_tag_key true </match> </label>
- 重啟fluentd容器
efk-master-init.sh:
#./bin/bash
# 定義顏色
BLUE_COLOR="\033[36m"
RED_COLOR="\033[31m"
GREEN_COLOR="\033[32m"
VIOLET_COLOR="\033[35m"
RES="\033[0m"
echo -e "${BLUE_COLOR}# ######################################################################${RES}"
echo -e "${BLUE_COLOR}# Docker ELK Shell Script #${RES}"
echo -e "${BLUE_COLOR}# Email: #${RES}"
echo -e "${BLUE_COLOR}# ######################################################################${RES}"
# 創建目錄
echo -e "${BLUE_COLOR}---> create [elasticsearch]directory start.${RES}"
if [ ! -d "./elasticsearch/" ]; then
mkdir -p ./elasticsearch/master/conf ./elasticsearch/master/data ./elasticsearch/master/logs
fi
echo -e "${RED_COLOR}---> create [kibana]directory start.${RES}"
if [ ! -d "./kibana/" ]; then
mkdir -p ./kibana/conf ./kibana/logs
fi
# 目錄授權(log 都要授讀/寫權限)
echo -e "${RED_COLOR}---> create [fluentd]directory start.${RES}"
if [ ! -d "./fluentd/" ]; then
mkdir -p ./fluentd/conf ./fluentd/log
chmod 777 ./fluentd/log
fi
echo -e "${BLUE_COLOR}===> create directory success.${RES}"
# 目錄授權(data/logs 都要授讀/寫權限)
echo -e "${BLUE_COLOR}---> directory authorize start.${RES}"
if [ -d "./elasticsearch/" ]; then
chmod 777 ./elasticsearch/master/data/ ./elasticsearch/master/logs/
fi
echo -e "${BLUE_COLOR}===> directory authorize success.${RES}"
# 移動配置文件
echo -e "${BLUE_COLOR}---> move [elasticsearch]config file start.${RES}"
if [ -f "./es-master.yml" ]; then
mv ./es-master.yml ./elasticsearch/master/conf
fi
echo -e "${RED_COLOR}---> move [kibana]config file start.${RES}"
if [ -f "./kibana.yml" ]; then
mv ./kibana.yml ./kibana/conf
fi
echo -e "${GREEN_COLOR}---> move [fluent]config file start.${RES}"
if [ -f "./fluent.conf" ]; then
mv ./fluent.conf ./fluentd/conf
fi
echo -e "${BLUE_COLOR}===> move config files success.${RES}"
echo -e "${GREEN_COLOR}>>>>>>>>>>>>>>>>>> The End <<<<<<<<<<<<<<<<<<${RES}"
# 部署項目
echo -e "${BLUE_COLOR}==================> Docker deploy Start <==================${RES}"
docker-compose up --build -d
- docker-compose.yml文件配置:
version: "3"
services:
web:#方便測試使用,啟動完成后執行curl localhost:1080,可以在kibana的discover界面查看
image: httpd
container_name: httpd
ports:
- 1080:80 #避免和默認的80端口沖突
links:
- fluentd
logging:
driver: fluentd
options:
fluentd-address: localhost:24224
tag: httpd.access
environment:
- "TZ=Asia/Shanghai"
fluentd:
image: fluent/fluentd
container_name: fluentd
user: root
volumes:
- ./fluentd/conf/fluent.conf:/fluentd/etc/fluent.conf
- ./fluentd/log:/fluentd/log
ports:
- 24221:24221
- 24222:24222
- 24223:24223
- 24224:24224
environment:
- "TZ=Asia/Shanghai"
es-master:
container_name: es-master
hostname: es-master
image: elasticsearch:7.1.1
user: root
ports:
- 9200:9200
- 9300:9300
volumes:
- ./elasticsearch/master/conf/es-master.yml:/usr/share/elasticsearch/config/elasticsearch.yml
- ./elasticsearch/master/data:/usr/share/elasticsearch/data
- ./elasticsearch/master/logs:/usr/share/elasticsearch/logs
environment:
- "ES_JAVA_OPTS=-Xms512m -Xmx512m"
- "TZ=Asia/Shanghai"
kibana:
container_name: kibana
hostname: kibana
image: kibana:7.1.1
ports:
- 5601:5601
volumes:
- ./kibana/conf/kibana.yml:/usr/share/kibana/config/kibana.yml
environment:
- "elasticsearch.hosts=http://ip:9200"
- "TZ=Asia/Shanghai"
depends_on:
- "es-master"
- es-master.yml文件配置:
# 集群名稱
cluster.name: es-cluster
# 節點名稱
node.name: es-master
# 是否可以成為master節點
node.master: true
# 是否允許該節點存儲數據,默認開啟
node.data: true
# 網絡綁定
network.host: 0.0.0.0
# 設置對外服務的http端口
http.port: 9200
# 設置節點間交互的tcp端口
transport.port: 9300
# 集群發現
discovery.seed_hosts:
- es-master
# 手動指定可以成為 mater 的所有節點的 name 或者 ip,這些配置將會在第一次選舉中進行計算
cluster.initial_master_nodes:
- es-master
# 支持跨域訪問
http.cors.enabled: true
http.cors.allow-origin: "*"
# 安全認證
xpack.security.enabled: false
#http.cors.allow-headers: "Authorization"
- 完整的fluent.conf文件配置,用作參考 :
<source>
@type tcp
@id debug-input
port 24221
tag debug
<parse>
@type json
</parse>
</source>
<source>
@type tcp
@id error-input
port 24222
tag error
<parse>
@type json
</parse>
</source>
<source>
@type tcp
@id business-input
port 24223
tag business
<parse>
@type json
</parse>
</source>
<source>
@type tcp
@id record-input
port 24224
tag record
<parse>
@type json
</parse>
</source>
<filter record>
@type parser
key_name message
reserve_data true
remove_key_name_field true
<parse>
@type json
</parse>
</filter>
<match fluent.**>
@type stdout
output_type json
</match>
<match **>
@type elasticsearch
host 192.168.3.101
port 9200
type_name docker
logstash_format true
logstash_prefix docker-${tag}-logs
logstash_dateformat %Y-%m-%d
flush_interval 5s
include_tag_key true
</match>
- kibana.yml文件配置:
# 服務端口
server.port: 5601
# 服務IP
server.host: "0.0.0.0"
# ES
elasticsearch.hosts: ["http://192.168.1.150:9200"]
# 漢化
i18n.locale: "zh-CN"