ติดตั้ง Fluent Bit บน VPS — Log Collector น้ำหนักเบา

เผยแพร่: 7 ตุลาคม 2569

Fluent Bit log collector บน VPS Linux

Fluent Bit คือ log processor และ forwarder น้ำหนักเบาสุดขีด — ใช้ RAM เพียง ~1 MB เมื่อ idle เทียบกับ Logstash ที่ต้องการหลายร้อย MB เขียนด้วยภาษา C ออกแบบมาสำหรับ embedded systems และ Kubernetes แต่ทำงานได้ดีมากบน VPS ทั่วไปเช่นกัน

บทความนี้จะพาติดตั้ง Fluent Bit บน Ubuntu/Debian ตั้งค่ารับ log จาก systemd journal และ nginx access log แล้วส่งไปยัง Loki หรือ Elasticsearch พร้อม parser และ filter ที่จำเป็น

Fluent Bit vs Logstash vs Filebeat

เครื่องมือภาษาRAMจุดเด่น
Fluent BitC~1-5 MBเบาที่สุด, built-in parser, Lua filter
FilebeatGo~50 MBส่วนหนึ่งของ ELK stack, config ง่าย
LogstashJVM~300-500 MBทรงพลังที่สุด, plugin หลากหลาย
FluentdRuby~40 MBecosystem ใหญ่, plugin มาก

สถาปัตยกรรม Fluent Bit

Fluent Bit ทำงานเป็น pipeline: Input → Parser → Filter → Buffer → Output

ขั้นที่ 1 — ติดตั้ง Fluent Bit

ติดตั้งจาก official repo ของ Fluent Bit:

# เพิ่ม GPG key และ repo
curl https://raw.githubusercontent.com/fluent/fluent-bit/master/install.sh | sh

หรือติดตั้งแบบ manual สำหรับ Ubuntu 22.04:

curl -fsSL https://packages.fluentbit.io/fluentbit.key | sudo gpg --dearmor -o /usr/share/keyrings/fluentbit-keyring.gpg

echo "deb [signed-by=/usr/share/keyrings/fluentbit-keyring.gpg] https://packages.fluentbit.io/ubuntu/jammy jammy main" \
  | sudo tee /etc/apt/sources.list.d/fluent-bit.list

sudo apt update
sudo apt install -y fluent-bit

ตรวจสอบการติดตั้ง:

fluent-bit --version

ขั้นที่ 2 — ทดสอบ Quick Start

ทดสอบ run แบบง่ายที่สุด — รับ CPU metrics แล้วแสดงบน stdout:

fluent-bit -i cpu -o stdout -f 1

ควรเห็น output ประมาณนี้:

[0] cpu.0: [[1728269100.000000000, {}], {"cpu_p"=>5.5, "user_p"=>3.2, "system_p"=>2.3, ...}]

ขั้นที่ 3 — ตั้งค่า Config ไฟล์หลัก

Config ไฟล์หลักอยู่ที่ /etc/fluent-bit/fluent-bit.conf:

sudo nano /etc/fluent-bit/fluent-bit.conf
[SERVICE]
    flush         5
    log_level     info
    parsers_file  parsers.conf
    plugins_file  plugins.conf

# --- Inputs ---

# อ่าน log จาก systemd journal
[INPUT]
    name              systemd
    tag               systemd.*
    read_from_tail    on
    strip_underscores on

# อ่าน nginx access log
[INPUT]
    name              tail
    tag               nginx.access
    path              /var/log/nginx/access.log
    parser            nginx
    refresh_interval  5
    mem_buf_limit     10MB
    skip_long_lines   on

# --- Filters ---

# เพิ่ม hostname ใน record ทุกอัน
[FILTER]
    name    record_modifier
    match   *
    record  hostname ${HOSTNAME}

# กรองเอา healthcheck ออก (ไม่ต้องการ noise)
[FILTER]
    name    grep
    match   nginx.access
    exclude request GET /health

# --- Outputs ---

# ส่งไปยัง stdout (debug)
[OUTPUT]
    name   stdout
    match  *

ขั้นที่ 4 — ส่ง Log ไปยัง Loki

เพิ่ม output section สำหรับ Loki (แทนที่ stdout หรือเพิ่มคู่กัน):

[OUTPUT]
    name            loki
    match           *
    host            localhost
    port            3100
    labels          job=fluent-bit,host=${HOSTNAME}
    label_keys      $app,$level
    line_format     json
Note: ต้องติดตั้ง Loki ก่อน ดูบทความ ติดตั้ง Loki + Grafana บน VPS

ขั้นที่ 5 — ส่ง Log ไปยัง Elasticsearch

ถ้าใช้ ELK stack แทน Loki:

[OUTPUT]
    name            es
    match           *
    host            localhost
    port            9200
    index           fluent-bit
    type            _doc
    logstash_format on
    logstash_prefix fluent-bit
    time_key        @timestamp
    retry_limit     3

ขั้นที่ 6 — ตั้งค่า Parser สำหรับ Nginx

แก้ไขไฟล์ /etc/fluent-bit/parsers.conf ให้มี parser สำหรับ nginx:

[PARSER]
    name        nginx
    format      regex
    regex       ^(?[^ ]*) (?[^ ]*) (?[^ ]*) \[(?

ขั้นที่ 7 — เปิดใช้งาน Service

sudo systemctl enable fluent-bit
sudo systemctl start fluent-bit
sudo systemctl status fluent-bit
Tip: ใช้ fluent-bit -c /etc/fluent-bit/fluent-bit.conf --dry-run เพื่อ validate config ก่อน restart service

ขั้นที่ 8 — ใช้ Lua Filter สำหรับ Logic ซับซ้อน

Fluent Bit รองรับ Lua script สำหรับ transform log แบบซับซ้อน:

[FILTER]
    name    lua
    match   nginx.access
    script  /etc/fluent-bit/enrich.lua
    call    enrich_record
-- /etc/fluent-bit/enrich.lua
function enrich_record(tag, timestamp, record)
    -- จัดกลุ่ม HTTP status code
    local code = tonumber(record["code"])
    if code then
        if code >= 500 then
            record["severity"] = "error"
        elseif code >= 400 then
            record["severity"] = "warning"
        else
            record["severity"] = "info"
        end
    end
    return 1, timestamp, record
end

ตรวจสอบว่า Fluent Bit ทำงานถูกต้อง

# ดู status
sudo systemctl status fluent-bit

# ดู log ของ Fluent Bit เอง
sudo journalctl -u fluent-bit -n 50 --no-pager

# ทดสอบ config ก่อน restart
fluent-bit -c /etc/fluent-bit/fluent-bit.conf --dry-run

# ดู metric ผ่าน HTTP (เปิด HTTP server ใน [SERVICE] ก่อน)
# เพิ่มใน [SERVICE]: http_server on, http_port 2020
curl http://localhost:2020/api/v1/metrics

# ดู uptime และ records ที่ processed
curl http://localhost:2020/api/v1/uptime
Tip: เปิด http_server on ใน [SERVICE] เพื่อดู Fluent Bit metrics ผ่าน HTTP — ใช้ scrape กับ Prometheus ได้โดยตรง

แก้ปัญหาที่พบบ่อย

Error: "tail plugin: inotify_init() failed"

เกิดเมื่อ inotify watches หมด — เพิ่ม limit

# ตรวจสอบ limit ปัจจุบัน
cat /proc/sys/fs/inotify/max_user_watches

# เพิ่ม limit ชั่วคราว
sudo sysctl fs.inotify.max_user_watches=524288

# เพิ่ม limit ถาวร
echo "fs.inotify.max_user_watches=524288" | sudo tee -a /etc/sysctl.conf
sudo sysctl -p

Error: "connection refused" เมื่อส่งไป Loki/Elasticsearch

ตรวจสอบว่า service ปลายทางทำงานอยู่และ firewall เปิด port

# ตรวจ Loki
curl -s http://localhost:3100/ready

# ตรวจ Elasticsearch
curl -s http://localhost:9200/_cluster/health

# ตรวจ port
sudo ss -tlnp | grep -E "3100|9200"

Error: "Permission denied" อ่าน log ไม่ได้

Fluent Bit ต้องมีสิทธิ์อ่านไฟล์ log — เพิ่ม user ลง group ที่เหมาะสม

# เพิ่ม fluent-bit user เข้า adm group (เพื่ออ่าน /var/log)
sudo usermod -aG adm fluent-bit

# หรือเปลี่ยน service ให้ run เป็น root
sudo systemctl edit fluent-bit --force
# เพิ่ม:
# [Service]
# User=root

Log เข้ามาช้าหรือ delay

ปรับค่า flush ใน [SERVICE] ให้น้อยลง และตรวจ mem_buf_limit

[SERVICE]
    flush     1      # ลดจาก 5 เป็น 1 วินาที

[INPUT]
    name              tail
    mem_buf_limit     5MB  # ถ้าน้อยเกินจะ flush บ่อยขึ้น
    buffer_chunk_size 512k
    buffer_max_size   5MB

Memory สูงผิดปกติ

เกิดเมื่อ output ส่งไม่ออก buffer เต็ม — ตรวจสอบ retry และ backpressure

[OUTPUT]
    name          loki
    match         *
    retry_limit   3    # จำกัด retry ไม่ให้ queue ใหญ่ไม่มีที่สิ้นสุด

# หรือเพิ่ม storage สำหรับ buffer ลงดิสก์
[SERVICE]
    storage.path  /var/lib/fluent-bit/
    storage.sync  normal

คำถามที่พบบ่อย

Fluent Bit กับ Fluentd ต่างกันอย่างไร?

Fluent Bit เป็นเวอร์ชัน lightweight ของ Fluentd — เขียนด้วย C แทน Ruby จึงเร็วกว่าและกินทรัพยากรน้อยกว่ามาก Fluentd มี plugin มากกว่าและยืดหยุ่นกว่าสำหรับ use case ซับซ้อน ใน Kubernetes มักใช้ Fluent Bit เป็น DaemonSet ส่งต่อไปยัง Fluentd aggregator อีกที

ใช้ Fluent Bit กับ Docker ได้ไหม?

ได้ — Docker รองรับ Fluent Bit เป็น logging driver โดยตรง ใส่ใน docker run ด้วย --log-driver=fluentd --log-opt fluentd-address=localhost:24224 หรือใช้ image fluent/fluent-bit เป็น sidecar container

Fluent Bit รับ log จาก Kubernetes ได้ไหม?

รองรับดีมาก — Fluent Bit เป็น default log collector ของ Kubernetes หลายเจ้า ใช้ input plugin kubernetes เพื่อ enrich log ด้วย pod/namespace metadata อัตโนมัติ และ deploy เป็น DaemonSet เพื่อรับ log จากทุก node