搬瓦工部署 Fluentd 日志收集器教程
Fluentd 是 CNCF 毕业项目,是 Kubernetes 生态中最常用的日志收集工具之一。它以统一的日志层(Unified Logging Layer)理念著称,能够将来自不同来源的日志数据收集、处理并路由到各种目标存储。Fluentd 拥有超过 500 个社区插件,几乎可以对接任何数据源和目标系统。本文将介绍如何在搬瓦工 VPS 上部署 Fluentd。部署前请确保已安装 Docker 和 Docker Compose。
一、Fluentd 核心特性
- 统一日志层:将所有日志统一为 JSON 格式,简化下游处理。
- 插件生态:500+ 插件支持各种输入、输出和过滤操作。
- 内置缓冲:支持内存和文件缓冲,确保数据不丢失。
- 高可用:支持主备模式和负载均衡。
- 灵活路由:基于标签(Tag)的路由机制,配置直观。
二、系统要求
- 操作系统:Ubuntu 20.04+ 或 Debian 11+。
- 内存:至少 512MB,推荐 1GB。
- 磁盘:至少 5GB 可用空间。
- Docker:已安装 Docker 和 Docker Compose。
搬瓦工 VPS 方案推荐参考 全部方案,购买时使用优惠码 NODESEEK2026 可享 6.77% 折扣,购买入口:bwh81.net。
三、创建项目目录
mkdir -p /opt/fluentd/{conf,log,buffer} && cd /opt/fluentd
四、编写 Fluentd 配置文件
cat > /opt/fluentd/conf/fluent.conf <<'EOF'
# ===== 输入插件 =====
# 接收 Syslog
<source>
@type syslog
port 5140
tag system
</source>
# 监控文件
<source>
@type tail
path /var/log/nginx/access.log
pos_file /var/log/fluentd/nginx-access.pos
tag nginx.access
<parse>
@type nginx
</parse>
</source>
# 接收 HTTP 数据
<source>
@type http
port 9880
tag http.input
</source>
# 接收 Forward 协议(来自其他 Fluentd 或 Fluent Bit)
<source>
@type forward
port 24224
</source>
# ===== 过滤器 =====
# 为 Nginx 日志添加字段
<filter nginx.**>
@type record_transformer
<record>
hostname "#{Socket.gethostname}"
service nginx
</record>
</filter>
# 过滤掉健康检查请求
<filter nginx.access>
@type grep
<exclude>
key path
pattern /health|/ping/
</exclude>
</filter>
# ===== 输出插件 =====
# 输出到 Elasticsearch
<match nginx.**>
@type elasticsearch
host elasticsearch
port 9200
index_name nginx-logs
logstash_format true
logstash_prefix nginx
<buffer>
@type file
path /var/log/fluentd/buffer/elasticsearch
flush_interval 10s
chunk_limit_size 8M
retry_max_interval 30s
retry_forever true
</buffer>
</match>
# 输出到文件(备份)
<match system.**>
@type file
path /var/log/fluentd/output/system
append true
<format>
@type json
</format>
<buffer time>
timekey 1d
timekey_wait 10m
</buffer>
</match>
# 未匹配的日志输出到标准输出
<match **>
@type stdout
</match>
EOF
五、创建自定义 Dockerfile
官方镜像不包含所有插件,需要自定义安装:
cat > /opt/fluentd/Dockerfile <<'EOF'
FROM fluent/fluentd:v1.16-1
USER root
RUN gem install fluent-plugin-elasticsearch fluent-plugin-prometheus fluent-plugin-s3 --no-document
USER fluent
EOF
六、编写 Docker Compose 配置
cat > /opt/fluentd/docker-compose.yml <<'EOF'
version: '3.8'
services:
fluentd:
build: .
restart: always
ports:
- "24224:24224"
- "24224:24224/udp"
- "5140:5140/udp"
- "9880:9880"
volumes:
- ./conf:/fluentd/etc
- ./log:/var/log/fluentd
- ./buffer:/var/log/fluentd/buffer
- /var/log/nginx:/var/log/nginx:ro
EOF
七、启动服务
cd /opt/fluentd
docker compose build
docker compose up -d
验证运行状态:
docker compose logs -f fluentd
八、测试日志收集
通过 HTTP 接口发送测试数据:
curl -X POST -d 'json={"message":"Hello Fluentd","level":"info"}' http://localhost:9880/http.input
九、与 Docker 日志驱动集成
配置 Docker 将容器日志直接发送到 Fluentd:
# 在 /etc/docker/daemon.json 中添加
{
"log-driver": "fluentd",
"log-opts": {
"fluentd-address": "localhost:24224",
"tag": "docker.{{.Name}}"
}
}
重启 Docker 服务后,所有容器日志将自动发送到 Fluentd。
十、常见问题
缓冲区溢出
增大缓冲区或使用文件缓冲替代内存缓冲。检查下游系统是否可达。
日志解析失败
使用 @type none 先收集原始日志,确认格式后再配置正确的解析器。
总结
Fluentd 是经过生产验证的日志收集工具,生态成熟且稳定性高。如果你需要更轻量的方案,可以参考 Fluent Bit;如果追求更高的性能,参考 Vector。选购搬瓦工 VPS 使用优惠码 NODESEEK2026 可享 6.77% 折扣。