
FastAPI ขั้นสูง 2026 Dependency Injection, Background Tasks, WebSocket สำหรับ Python
Logging ใน Kubernetes เป็นความท้าทายพิเศษเพราะ Pod เกิดขึ้นและหายไปตลอดเวลา ถ้าไม่มีระบบเก็บ Log แบบ Centralized เมื่อ Pod ถูกลบ Log ก็หายไปด้วย Fluentd และ Fluent Bit เป็นเครื่องมือ Open-Source ยอดนิยมที่สุดสำหรับเก็บและส่ง Log จาก Kubernetes ไปยัง Backend เช่น Elasticsearch, Loki หรือ S3
Fluentd vs Fluent Bit

| คุณสมบัติ | Fluentd | Fluent Bit |
|---|---|---|
| ภาษา | Ruby + C | C (Lightweight) |
| Memory | ~60-100 MB | ~1-5 MB |
| Plugins | 1,000+ Plugins | 70+ Built-in |
| ใช้เป็น | Log Aggregator (กลาง) | Log Collector (แต่ละ Node) |
| แนะนำ | Aggregation Layer | DaemonSet บนทุก Node |
Architecture: Fluent Bit + Elasticsearch
# Kubernetes Logging Architecture:
#
# Pod (stdout/stderr) -> Container Runtime -> /var/log/containers/
# |
# v
# Fluent Bit (DaemonSet บนทุก Node)
# |
# v
# Fluentd (Aggregator - Optional)
# |
# v
# Elasticsearch / Loki / S3 / CloudWatch
# |
# v
# Kibana / Grafana (Dashboard)
ติดตั้ง Fluent Bit ด้วย Helm
# ติดตั้ง Fluent Bit บน Kubernetes:
helm repo add fluent https://fluent.github.io/helm-charts
helm repo update
# ติดตั้ง:
helm install fluent-bit fluent/fluent-bit \
--namespace logging \
--create-namespace \
--set config.outputs="[OUTPUT]\n Name es\n Match *\n Host elasticsearch.logging.svc\n Port 9200\n Index fluent-bit\n Type _doc"
# ตรวจสอบ:
kubectl get pods -n logging
# fluent-bit-xxxxx 1/1 Running (ทุก Node)
Fluent Bit Configuration
# fluent-bit.conf:
[SERVICE]
Flush 5
Log_Level info
Parsers_File parsers.conf
[INPUT]
Name tail
Path /var/log/containers/*.log
Parser cri
Tag kube.*
Mem_Buf_Limit 5MB
Skip_Long_Lines On
Refresh_Interval 10
[FILTER]
Name kubernetes
Match kube.*
Kube_URL https://kubernetes.default.svc:443
Kube_CA_File /var/run/secrets/kubernetes.io/serviceaccount/ca.crt
Kube_Token_File /var/run/secrets/kubernetes.io/serviceaccount/token
Merge_Log On
K8S-Logging.Parser On
K8S-Logging.Exclude On
[OUTPUT]
Name es
Match *
Host elasticsearch.logging.svc
Port 9200
Index k8s-logs
Type _doc
Logstash_Format On
Logstash_Prefix k8s
Retry_Limit 5
ส่ง Log ไป Grafana Loki (Alternative)

# Fluent Bit -> Loki (แทน Elasticsearch):
# Loki เบากว่า Elasticsearch มาก!
[OUTPUT]
Name loki
Match *
Host loki.logging.svc
Port 3100
Labels job=fluent-bit
Auto_Kubernetes_Labels On
# ติดตั้ง Loki Stack:
helm install loki grafana/loki-stack \
--namespace logging \
--set grafana.enabled=true \
--set loki.persistence.enabled=true \
--set loki.persistence.size=50Gi
# ข้อดีของ Loki vs Elasticsearch:
# - ใช้ Storage น้อยกว่า 10x (Index แค่ Labels)
# - ตั้งค่าง่ายกว่า
# - ใช้ Grafana เป็น Dashboard (ไม่ต้อง Kibana)
# - เหมาะกับ Log Volume ปานกลาง
DaemonSet vs Sidecar Pattern
| Pattern | ข้อดี | ข้อเสีย | เหมาะกับ |
|---|---|---|---|
| DaemonSet | ติดตั้งครั้งเดียว ทำงานทุก Node | ไม่ Customize ต่อ Pod ได้ | ส่วนใหญ่ (แนะนำ) |
| Sidecar | Customize ต่อ Pod, จัดการ Log เฉพาะ App | ใช้ Resource มากขึ้น ต่อ Pod | App ที่ Log ซับซ้อน |
Log Parsing สำหรับ Application
# Parse JSON Logs:
[FILTER]
Name parser
Match kube.var.log.containers.myapp*
Key_Name log
Parser json
Reserve_Data On
# Parse Custom Format:
[PARSER]
Name myapp
Format regex
Regex ^(?<time>[^ ]*) (?<level>[^ ]*) (?<message>.*)$
Time_Key time
Time_Format %Y-%m-%dT%H:%M:%S.%L
# แนะนำ: ให้ Application Log เป็น JSON
# เพราะ Parse ง่าย, ไม่ต้อง Custom Regex
Monitoring Fluent Bit
# เปิด Prometheus Metrics:
[SERVICE]
HTTP_Server On
HTTP_Listen 0.0.0.0
HTTP_Port 2020
# Prometheus ServiceMonitor:
apiVersion: monitoring.coreos.com/v1
kind: ServiceMonitor
metadata:
name: fluent-bit
spec:
selector:
matchLabels:
app.kubernetes.io/name: fluent-bit
endpoints:
- port: http
path: /api/v1/metrics/prometheus
# Metrics สำคัญ:
# fluentbit_input_records_total — จำนวน Records ที่รับ
# fluentbit_output_retries_total — จำนวน Retry
# fluentbit_output_errors_total — จำนวน Error
# fluentbit_output_proc_bytes_total — ขนาด Data ที่ส่ง
Best Practices
| Practice | คำอธิบาย |
|---|---|
| ใช้ Fluent Bit เป็น DaemonSet | เบา ใช้ Memory น้อย เหมาะเป็น Collector |
| Log เป็น JSON | Parse ง่าย ไม่ต้อง Custom Regex |
| ตั้ง Mem_Buf_Limit | ป้องกัน Fluent Bit ใช้ Memory มากเกินไป |
| ใช้ Loki สำหรับ Small-Medium | เบากว่า Elasticsearch ตั้งค่าง่ายกว่า |
| ตั้ง Retention Policy | ลบ Log เก่าอัตโนมัติ ป้องกัน Disk เต็ม |
| Monitor Fluent Bit | ใช้ Prometheus Metrics ดู Error/Retry |
| แยก Namespace ใน Index | ง่ายในการค้นหาและจัดการ |
สรุป
Centralized Logging เป็นสิ่งจำเป็นสำหรับ Kubernetes Production Fluent Bit เป็นตัวเลือกที่ดีที่สุดสำหรับ Log Collector เพราะเบา ใช้ Memory น้อย และ Integrate กับ Backend ได้หลากหลาย ไม่ว่าจะเป็น Elasticsearch/Kibana หรือ Loki/Grafana การเข้าใจ Architecture และ Best Practices จะช่วยให้คุณ Debug ปัญหาได้เร็วขึ้น และมี Visibility ครบถ้วนในทุก Pod ของ Cluster
อ่านเพิ่ม: Elixir Ecto Freelance IT Career — วิธีตั้งค่าและใช้งานจริงพร · อ่านเพิ่ม: Elixir Ecto Event Driven Design — วิธีตั้งค่าและใช้งานจริงพร · อ่านเพิ่ม: Elixir Ecto Metric Collection — วิธีตั้งค่าและใช้งานจริงพร้อ