ติดตั้ง Fluent Bit บน VPS รวบรวม Log ส่งไปยัง Loki และ Elasticsearch
Fluent Bit คือ log processor น้ำหนักเบาที่ออกแบบมาสำหรับ environment ที่ทรัพยากรจำกัด เช่น VPS หรือ container — รวบรวม log จากหลายแหล่ง ประมวลผล และส่งต่อไปยัง Loki, Elasticsearch, Kafka หรือ S3 ได้ทันที เขียนด้วย C ทำให้ใช้ RAM เพียง 450 KB และ CPU น้อยกว่า Logstash หรือ Fluentd หลายเท่า เหมาะมากสำหรับ VPS ที่ต้องการ log pipeline ที่เสถียรโดยไม่เปลือง resource
Fluent Bit คืออะไร
Fluent Bit เป็น open-source log processor และ forwarder อยู่ในตระกูล Fluentd แต่ออกแบบใหม่ให้เบากว่ามาก สถาปัตยกรรมหลักแบ่งเป็น 3 ส่วน:
- INPUT: แหล่งที่มาของ log — ไฟล์, systemd journal, syslog, Docker, TCP socket
- FILTER: ประมวลผล/ดัดแปลง log — grep กรอง, parser แยก field, Lua script
- OUTPUT: ปลายทาง — Loki, Elasticsearch, Kafka, S3, stdout
Fluent Bit vs Fluentd
| Feature | Fluent Bit | Fluentd |
|---|---|---|
| Language | C | Ruby |
| Memory (idle) | ~450 KB | ~40 MB |
| CPU usage | ต่ำมาก | ปานกลาง |
| Plugins | ~70 built-in | ~1000+ community |
| เหมาะกับ | VPS, container, edge | Enterprise, complex routing |
| Config | INI / YAML | YAML / fluentd DSL |
ติดตั้ง Fluent Bit
Fluent Bit มี official repository สำหรับ Ubuntu/Debian และ RHEL/CentOS ติดตั้งได้ง่ายด้วย apt หรือ yum
Ubuntu / Debian
# เพิ่ม Fluent Bit repository
curl https://raw.githubusercontent.com/fluent/fluent-bit/master/install.sh | sh
# หรือ manual install:
wget -qO - https://packages.fluentbit.io/fluentbit.key | gpg --dearmor \
-o /usr/share/keyrings/fluentbit-keyring.gpg
echo "deb [signed-by=/usr/share/keyrings/fluentbit-keyring.gpg] \
https://packages.fluentbit.io/ubuntu/$(lsb_release -cs) \
$(lsb_release -cs) main" \
| tee /etc/apt/sources.list.d/fluent-bit.list
apt-get update
apt-get install -y fluent-bit
# ตรวจสอบ version
/opt/fluent-bit/bin/fluent-bit --version
RHEL / CentOS / Rocky Linux
# เพิ่ม repo
cat > /etc/yum.repos.d/fluent-bit.repo << 'EOF'
[fluent-bit]
name=Fluent Bit
baseurl=https://packages.fluentbit.io/centos/$releasever/
gpgcheck=1
gpgkey=https://packages.fluentbit.io/fluentbit.key
enabled=1
EOF
dnf install -y fluent-bit
# หรือ CentOS 7 ใช้ yum
yum install -y fluent-bit
เปิดใช้งาน Service
# เปิด service และตั้งให้ start อัตโนมัติ
systemctl enable fluent-bit
systemctl start fluent-bit
systemctl status fluent-bit
ตั้งค่าพื้นฐาน
ไฟล์ config หลักอยู่ที่ /etc/fluent-bit/fluent-bit.conf แบ่งเป็น section [SERVICE], [INPUT], [FILTER], [OUTPUT]
โครงสร้าง fluent-bit.conf
# /etc/fluent-bit/fluent-bit.conf
[SERVICE]
Flush 5
Daemon Off
Log_Level info
HTTP_Server On
HTTP_Listen 0.0.0.0
HTTP_Port 2020
# Include parsers file
Parsers_File parsers.conf
# --- INPUT: อ่าน log จาก systemd journal ---
[INPUT]
Name systemd
Tag systemd.*
Systemd_Filter _SYSTEMD_UNIT=nginx.service
Read_From_Tail On
# --- INPUT: อ่าน log จากไฟล์ ---
[INPUT]
Name tail
Tag nginx.access
Path /var/log/nginx/access.log
Parser nginx
DB /var/lib/fluent-bit/nginx.db
Mem_Buf_Limit 5MB
Skip_Long_Lines On
Refresh_Interval 10
# --- INPUT: อ่าน Docker container logs ---
[INPUT]
Name forward
Listen 0.0.0.0
Port 24224
# --- FILTER: เพิ่ม hostname label ---
[FILTER]
Name record_modifier
Match *
Record hostname ${HOSTNAME}
# --- OUTPUT: ส่งออก (ตัวอย่าง stdout สำหรับ debug) ---
[OUTPUT]
Name stdout
Match *
.db file) ติดตาม position ของแต่ละไฟล์ ทำให้ restart แล้วไม่อ่าน log ซ้ำ ควรกำหนด path ที่ persistentParser สำหรับ Nginx
# /etc/fluent-bit/parsers.conf
[PARSER]
Name nginx
Format regex
Regex ^(?<remote>[^ ]*) (?<host>[^ ]*) (?<user>[^ ]*) \[(?<time>[^\]]*)\] "(?<method>\S+)(?: +(?<path>[^\"]*?)(?: +\S*)?)?" (?<code>[^ ]*) (?<size>[^ ]*)(?: "(?<referer>[^\"]*)" "(?<agent>[^\"]*)")?$
Time_Key time
Time_Format %d/%b/%Y:%H:%M:%S %z
[PARSER]
Name syslog
Format regex
Regex ^\<(?<pri>[0-9]+)\>(?<time>[^ ]* {1,2}[^ ]* [^ ]*) (?<host>[^ ]*) (?<ident>[a-zA-Z0-9_\/\.\-]*)(?:\[(?<pid>[0-9]+)\])?(?:[^\:]*\:)? *(?<message>.*)$
Time_Key time
Time_Format %b %d %H:%M:%S
ส่ง Log ไปยัง Loki
Fluent Bit มี built-in Loki output plugin ตั้งแต่ version 1.6 ไม่ต้องติดตั้ง plugin เพิ่ม
# เพิ่มใน /etc/fluent-bit/fluent-bit.conf
[OUTPUT]
Name loki
Match *
Host localhost
Port 3100
Labels job=fluent-bit,host=${HOSTNAME}
Label_Keys $level,$container_name
Remove_Keys kubernetes_namespace_name,kubernetes_pod_name
Line_Format json
# ถ้า Loki ต้องการ auth
# HTTP_User admin
# HTTP_Passwd yourpassword
# TLS
# tls on
# tls.verify on
job, host, env — ห้ามใช้ user_id หรือ request_id เป็น labelทดสอบส่ง Log ไปยัง Loki
# ตรวจสอบว่า Loki รับ log แล้ว
curl -s "http://localhost:3100/loki/api/v1/query?query={job=\"fluent-bit\"}&limit=5" \
| python3 -m json.tool | head -30
# ดู label ทั้งหมดที่ Loki มี
curl -s "http://localhost:3100/loki/api/v1/labels" | python3 -m json.tool
ส่ง Log ไปยัง Elasticsearch
Output plugin สำหรับ Elasticsearch รองรับทั้ง Elasticsearch 7.x, 8.x และ OpenSearch
[OUTPUT]
Name es
Match nginx.*
Host localhost
Port 9200
Index fluent-bit
Type _doc
# สำหรับ Elasticsearch 8+ ที่ใช้ TLS และ API key
# HTTP_User elastic
# HTTP_Passwd yourpassword
# tls on
# tls.verify off
Logstash_Format On
Logstash_Prefix fluent-bit
Logstash_DateFormat %Y.%m.%d
Replace_Dots On
Retry_Limit False
# Buffer size
Buffer_Size 5MB
fluent-bit-2026.11.07 ทำให้จัดการ lifecycle ง่ายขึ้นส่งไปหลาย Output พร้อมกัน
# ส่ง log เดียวกันไปทั้ง Loki และ Elasticsearch
[OUTPUT]
Name loki
Match nginx.*
Host localhost
Port 3100
Labels job=nginx,host=${HOSTNAME}
[OUTPUT]
Name es
Match nginx.*
Host localhost
Port 9200
Index nginx-logs
Logstash_Format On
Logstash_Prefix nginx
Filtering และ Parsing
FILTER ช่วยกรอง แปลง และเพิ่ม field ให้กับ log record ก่อนส่งออก
Grep Filter — กรอง log ที่ต้องการ
# เก็บเฉพาะ log ที่มี status code 4xx หรือ 5xx
[FILTER]
Name grep
Match nginx.access
Regex code ^[45]
# ทิ้ง log ที่มาจาก health check
[FILTER]
Name grep
Match nginx.access
Exclude path ^/health
# ทิ้ง log ที่ message ขึ้นต้นด้วย DEBUG
[FILTER]
Name grep
Match app.*
Exclude message ^DEBUG
Record Modifier — เพิ่ม/ลบ Field
# เพิ่ม environment และ service name
[FILTER]
Name record_modifier
Match *
Record environment production
Record service my-app
Record datacenter th-bkk-01
Remove_key _SYSTEMD_UNIT
Remove_key _BOOT_ID
Parser Filter — แยก field จาก message
# Parse JSON log จาก application
[FILTER]
Name parser
Match app.*
Key_Name log
Parser json
Reserve_Data On
Preserve_Key Off
# กำหนด JSON parser ใน parsers.conf
[PARSER]
Name json
Format json
Time_Key timestamp
Time_Format %Y-%m-%dT%H:%M:%S.%LZ
Lua Filter — logic ที่ซับซ้อน
# ใช้ Lua script สำหรับ custom transformation
[FILTER]
Name lua
Match nginx.*
Script /etc/fluent-bit/scripts/enrich.lua
Call enrich_record
# /etc/fluent-bit/scripts/enrich.lua
function enrich_record(tag, timestamp, record)
-- เพิ่ม log level จาก status code
local code = tonumber(record["code"])
if code and code >= 500 then
record["level"] = "error"
elseif code and code >= 400 then
record["level"] = "warn"
else
record["level"] = "info"
end
return 1, timestamp, record
end
ตรวจสอบว่า Fluent Bit ทำงานถูกต้อง
ใช้คำสั่งต่อไปนี้ตรวจสอบสถานะและ debug Fluent Bit:
# 1. ดูสถานะ service
systemctl status fluent-bit
# 2. ดู log ของ Fluent Bit เอง
journalctl -u fluent-bit -f --no-pager
# 3. ดู metrics ผ่าน HTTP API (ต้องเปิด HTTP_Server ใน [SERVICE])
curl -s http://localhost:2020/api/v1/metrics | python3 -m json.tool
# 4. ดู uptime และ pipeline health
curl -s http://localhost:2020/api/v1/health
# 5. ทดสอบ config โดยไม่ start service
/opt/fluent-bit/bin/fluent-bit -c /etc/fluent-bit/fluent-bit.conf --dry-run
/api/v1/metrics แสดงจำนวน record ที่ input รับ, filter ประมวลผล, และ output ส่งออก — ใช้ detect ว่า log หาย (drop) ที่ขั้นตอนไหนแก้ปัญหาที่พบบ่อย
Fluent Bit ไม่อ่านไฟล์ log ใหม่
มักเกิดจาก permission ไม่พอ หรือ Read_From_Tail ทำให้ข้ามไฟล์ที่มีอยู่แล้ว
# ตรวจ permission
ls -la /var/log/nginx/access.log
# ต้องให้ user ที่ Fluent Bit run สามารถอ่านได้
# ดูว่า Fluent Bit run เป็น user อะไร
ps aux | grep fluent-bit
# ถ้าต้องการอ่าน log เก่าด้วย เปลี่ยน Read_From_Tail เป็น Off
# หรือลบ .db file แล้ว restart
rm /var/lib/fluent-bit/nginx.db
systemctl restart fluent-bit
Output ส่งไปยัง Elasticsearch ไม่สำเร็จ
ตรวจสอบ connection และ index mapping conflict
# ทดสอบ connection ไปยัง Elasticsearch ก่อน
curl -s http://localhost:9200/_cluster/health | python3 -m json.tool
# ดู error ใน Fluent Bit log
journalctl -u fluent-bit | grep -i "error\|failed\|retry" | tail -20
# ถ้าเจอ mapping conflict ให้ลบ index แล้วลองใหม่
curl -X DELETE http://localhost:9200/fluent-bit-2026.11.07
Memory สูงผิดปกติ
Fluent Bit มี back-pressure mechanism เมื่อ output ช้ากว่า input
# เพิ่ม Mem_Buf_Limit ใน INPUT เพื่อจำกัด buffer
[INPUT]
Name tail
Path /var/log/app/*.log
Mem_Buf_Limit 10MB
# เมื่อ buffer เต็ม จะหยุดอ่านชั่วคราว
# หรือเปิด storage.type filesystem แทน memory
[SERVICE]
Storage.type filesystem
Storage.path /var/lib/fluent-bit/storage/
Storage.sync normal
Storage.checksum off
Storage.max_chunks_up 128
Loki ขึ้น "stream selector is required"
ต้องกำหนด Labels ใน Loki output ให้ครบอย่างน้อย 1 label
# ตรวจสอบว่า Labels field มีค่า
[OUTPUT]
Name loki
Match *
Host localhost
Port 3100
Labels job=fluent-bit # ← ต้องมีอย่างน้อย 1 label
# ทดสอบ push log ด้วย curl โดยตรง
curl -s -X POST "http://localhost:3100/loki/api/v1/push" \
-H "Content-Type: application/json" \
-d '{"streams":[{"stream":{"job":"test"},"values":[["'$(date +%s%N)'","test message"]]}]}'
คำถามที่พบบ่อย
Fluent Bit รองรับ Windows ได้ไหม?
รองรับ — Fluent Bit มี Windows binary ตั้งแต่ version 1.7 สามารถอ่าน Windows Event Log ได้ด้วย winlog input plugin และ Windows Service ด้วย winevtlog เหมาะสำหรับ environment ที่มีทั้ง Linux และ Windows server
ต่างจาก Promtail อย่างไร?
Promtail เป็น log agent ของ Grafana ที่ออกแบบมาส่งไปยัง Loki เท่านั้น Fluent Bit รองรับ output หลายตัวพร้อมกัน (Loki + ES + Kafka ในไฟล์ config เดียว) และมี filtering/transformation ที่ครอบคลุมกว่า ถ้าใช้แค่ Loki Promtail ก็ง่ายกว่า แต่ถ้าต้องการส่ง log ไปหลายที่ Fluent Bit คุ้มค่ากว่า
จะ monitor Fluent Bit เองได้อย่างไร?
Fluent Bit expose Prometheus metrics ที่ /api/v1/metrics/prometheus endpoint สามารถ scrape ด้วย Prometheus แล้ว alert เมื่อ output error rate สูง หรือ record drop เกิน threshold ที่กำหนด
# ใน fluent-bit.conf
[SERVICE]
HTTP_Server On
HTTP_Listen 0.0.0.0
HTTP_Port 2020
# ใน prometheus.yml
scrape_configs:
- job_name: fluent-bit
static_configs:
- targets: ['localhost:2020']
metrics_path: /api/v1/metrics/prometheus