Prometheus + Grafana คืออะไร — ทำไมต้องใช้คู่กัน?
Prometheus คือระบบ monitoring แบบ open-source ที่ออกแบบมาสำหรับเก็บ time-series metrics จาก server, container, application ต่างๆ โดยใช้วิธี pull-based คือ Prometheus จะไปดึงข้อมูลจาก target เป็นระยะๆ ผ่าน HTTP endpoint
Grafana คือ visualization platform ที่แสดงข้อมูลจาก Prometheus เป็น dashboard สวยงาม มี graph, alert, และ annotation ทำให้เห็นสถานะระบบแบบ real-time
ทั้งคู่ใช้ร่วมกันเป็น monitoring stack มาตรฐาน ของวงการ DevOps ในปี 2026 แทบทุกบริษัทที่ใช้ Kubernetes หรือ microservices จะใช้ Prometheus + Grafana
สถาปัตยกรรม Prometheus + Grafana Stack
ระบบ monitoring ที่สมบูรณ์ประกอบด้วย:
- Prometheus Server — เก็บ metrics และรัน PromQL queries
- Alertmanager — จัดการ alerts ส่ง notification ไป Slack/Email/PagerDuty
- Grafana — Dashboard visualization
- Node Exporter — เก็บ metrics ของ server (CPU, RAM, Disk, Network)
- cAdvisor — เก็บ metrics ของ Docker containers
- Blackbox Exporter — ตรวจสอบ endpoint availability (HTTP, TCP, ICMP)
ติดตั้ง Prometheus + Grafana บน Ubuntu 22.04
Step 1: ติดตั้ง Prometheus
# ดาวน์โหลด Prometheus
wget https://github.com/prometheus/prometheus/releases/download/v2.50.0/prometheus-2.50.0.linux-amd64.tar.gz
tar xvf prometheus-2.50.0.linux-amd64.tar.gz
sudo mv prometheus-2.50.0.linux-amd64 /opt/prometheus
# สร้าง systemd service
sudo tee /etc/systemd/system/prometheus.service <<EOF
[Unit]
Description=Prometheus Monitoring
After=network.target
[Service]
ExecStart=/opt/prometheus/prometheus --config.file=/opt/prometheus/prometheus.yml --storage.tsdb.retention.time=30d
Restart=always
[Install]
WantedBy=multi-user.target
EOF
sudo systemctl daemon-reload
sudo systemctl enable --now prometheus
Step 2: ตั้งค่า prometheus.yml
# /opt/prometheus/prometheus.yml
global:
scrape_interval: 15s
evaluation_interval: 15s
scrape_configs:
- job_name: 'prometheus'
static_configs:
- targets: ['localhost:9090']
- job_name: 'node'
static_configs:
- targets: ['server1:9100', 'server2:9100']
- job_name: 'nginx'
static_configs:
- targets: ['web1:9113']
Step 3: ติดตั้ง Grafana
sudo apt install -y apt-transport-https software-properties-common
wget -q -O - https://apt.grafana.com/gpg.key | sudo apt-key add -
echo "deb https://apt.grafana.com stable main" | sudo tee /etc/apt/sources.list.d/grafana.list
sudo apt update && sudo apt install grafana -y
sudo systemctl enable --now grafana-server
# เข้า http://server-ip:3000 (admin/admin)
Dashboard สำคัญที่ต้องมี
| Dashboard | Grafana ID | ใช้ดูอะไร |
|---|---|---|
| Node Exporter Full | 1860 | CPU, RAM, Disk, Network ของทุก server |
| Docker Container | 893 | Container resource usage |
| Nginx | 12708 | Request rate, error rate, latency |
| MySQL | 7362 | Queries, connections, InnoDB metrics |
| PostgreSQL | 9628 | Query performance, connections, locks |
| Redis | 11835 | Memory, hit rate, connected clients |
Alerting Rules ที่ Production ต้องมี
# /opt/prometheus/alert.rules.yml
groups:
- name: server-alerts
rules:
- alert: HighCPU
expr: 100 - (avg by(instance) (rate(node_cpu_seconds_total{mode="idle"}[5m])) * 100) > 80
for: 5m
labels: { severity: warning }
annotations:
summary: "CPU สูงเกิน 80% บน {{ $labels.instance }}"
- alert: DiskAlmostFull
expr: (node_filesystem_avail_bytes / node_filesystem_size_bytes) * 100 < 15
for: 10m
labels: { severity: critical }
annotations:
summary: "Disk เหลือน้อยกว่า 15% บน {{ $labels.instance }}"
- alert: InstanceDown
expr: up == 0
for: 2m
labels: { severity: critical }
annotations:
summary: "{{ $labels.instance }} ไม่ตอบสนองมากกว่า 2 นาที"
FAQ — คำถามที่พบบ่อย
Q: Prometheus เก็บข้อมูลได้นานแค่ไหน?
Default คือ 15 วัน แต่ตั้งได้ผ่าน --storage.tsdb.retention.time=30d สำหรับ long-term storage แนะนำใช้ Thanos หรือ VictoriaMetrics ที่เก็บข้อมูลบน S3 ได้หลายปี
Q: Prometheus vs Zabbix vs Datadog เลือกตัวไหนดี?
Prometheus เหมาะกับ cloud-native และ Kubernetes ecosystem ฟรี แต่ต้องดูแลเอง Zabbix เหมาะกับ traditional infrastructure มี agent-based monitoring Datadog เป็น SaaS ไม่ต้องดูแลแต่แพงมาก สำหรับ DevOps ในปี 2026 Prometheus คือ standard
Q: Grafana ใช้ฟรีจริงไหม?
Grafana OSS (open-source) ใช้ฟรีตลอด ไม่มีข้อจำกัด ใช้ได้ทั้ง commercial และ personal Grafana Cloud มี free tier ให้ 10,000 metrics 50GB logs ต่อเดือน เพียงพอสำหรับทีมเล็ก
Prometheus PromQL — ภาษาสำหรับ Query Metrics
PromQL เป็นภาษาเฉพาะของ Prometheus สำหรับ query ข้อมูล metrics เป็นทักษะสำคัญที่ DevOps Engineer ต้องเรียนรู้ ตัวอย่าง PromQL ที่ใช้บ่อยในงานจริง:
- CPU Usage เฉลี่ย — ใช้สำหรับดูว่า server แต่ละตัวใช้ CPU มากแค่ไหน ถ้าเกิน 80 เปอร์เซ็นต์ต่อเนื่อง 5 นาทีควร alert
- Memory Available — ดู RAM ที่เหลือ ถ้าน้อยกว่า 10 เปอร์เซ็นต์ระบบอาจ OOM kill process
- Disk Usage — ดู disk ที่เหลือ ถ้าต่ำกว่า 15 เปอร์เซ็นต์ควร alert เพื่อเคลียร์ log หรือขยาย disk
- HTTP Request Rate — ดูจำนวน request ต่อวินาทีของ web server ช่วยวางแผน capacity
- Error Rate — ดูสัดส่วน request ที่ error ถ้าเกิน 1 เปอร์เซ็นต์ควรตรวจสอบ
การ Scale Prometheus สำหรับระบบขนาดใหญ่
Prometheus ตัวเดียวรองรับได้ประมาณ 1 ล้าน time series ถ้ามีมากกว่านี้ต้อง scale โดยมี 2 วิธีหลัก วิธีแรกคือ Federation คือใช้ Prometheus หลายตัว แต่ละตัวดูแล cluster หรือ service กลุ่มหนึ่ง แล้วมี global Prometheus มา scrape จากตัวลูกอีกที วิธีที่สองคือใช้ Thanos หรือ Cortex หรือ VictoriaMetrics ซึ่งเป็น long-term storage ที่เก็บข้อมูลบน object storage เช่น S3 ได้หลายปีโดยไม่ต้องเพิ่ม disk บน Prometheus server
สำหรับองค์กรที่เพิ่งเริ่มต้นและมี server น้อยกว่า 50 เครื่อง Prometheus ตัวเดียวก็เพียงพอ ไม่ต้อง over-engineer ด้วย Thanos ตั้งแต่แรก เริ่มจาก Prometheus ตัวเดียวกับ Grafana แล้วค่อยขยายเมื่อจำเป็น
Grafana Dashboard Best Practices
การสร้าง Grafana Dashboard ที่ดีสำหรับ production ควรปฏิบัติตามหลักการเหล่านี้:
- แบ่ง Dashboard ตาม Layer — สร้าง dashboard แยกสำหรับ Infrastructure Overview Service Level และ Business Metrics อย่ารวมทุกอย่างใน dashboard เดียว
- ใช้ Variables — ทำ dropdown ให้เลือก server instance หรือ service ไม่ต้องสร้าง dashboard ซ้ำสำหรับทุก server
- ตั้ง Alert Thresholds — ใช้สี เขียว เหลือง แดง แสดงสถานะ ดูแล้วเข้าใจทันทีว่าอะไรเป็นปัญหา
- เพิ่ม Annotations — mark deployment events บน graph เพื่อ correlate กับ performance changes
- ใช้ Dashboard as Code — เก็บ dashboard JSON ใน Git ใช้ Grafana provisioning ให้ restore ได้เมื่อ Grafana ตาย
Monitoring สำหรับองค์กรไทยในปี 2026
องค์กรไทยหลายแห่งยังใช้วิธี monitoring แบบดั้งเดิม เช่น SSH เข้าไปดู htop หรือรอให้ลูกค้าแจ้งว่าระบบ down การลงทุนสร้างระบบ monitoring ด้วย Prometheus และ Grafana ช่วยลดเวลา downtime ได้อย่างมาก หลายองค์กรรายงานว่า MTTR ลดลงจากหลายชั่วโมงเหลือไม่กี่นาที เพราะสามารถตรวจพบปัญหาและ alert ทีม IT ได้ทันที ก่อนที่ user จะรู้สึกถึงปัญหา
ค่าใช้จ่ายในการ setup Prometheus Grafana stack คือศูนย์บาทสำหรับ software เพราะเป็น open-source ทั้งหมด ลงทุนแค่ server สำหรับรัน Prometheus ใช้ VM ขนาดเล็ก RAM 4 GB ก็เพียงพอสำหรับ monitor server 20 ถึง 30 เครื่อง เทียบกับ Datadog ที่เริ่มต้นที่ 15 เหรียญต่อ host ต่อเดือน ถ้ามี 50 hosts ก็ประมาณ 750 เหรียญต่อเดือนหรือกว่า 25,000 บาท Prometheus จึงเป็นทางเลือกที่คุ้มค่าที่สุดสำหรับองค์กรที่มีทีม DevOps ดูแลเอง
