
สารบัญ
ทำไมต้องทำ Monitoring VPS
เมื่อเว็บไซต์หรือแอปพลิเคชันช้าลงหรือล่ม คำถามแรกคือ "เกิดอะไรขึ้น?" หากไม่มีระบบ monitoring คุณจะมองไม่เห็นอะไรเลย ต้อง SSH เข้าไปรัน top หรือ df -h เองทุกครั้ง ซึ่งช้าเกินไปและไม่มีประวัติย้อนหลัง
Prometheus + Grafana คือ stack ยอดนิยมสำหรับ self-hosted monitoring ที่ใช้กันในระดับ production จริง ติดตั้งบน VPS ได้ฟรี และให้ข้อมูลได้ลึกกว่า SaaS ราคาแพงมาก
| ข้อมูลที่ดูได้ | เครื่องมือ | ประโยชน์ |
|---|---|---|
| CPU usage | Node Exporter → Prometheus | จับ spike ก่อน user โดนผลกระทบ |
| RAM free/used | Node Exporter → Prometheus | วางแผน upgrade spec ได้แม่นยำ |
| Disk I/O & usage | Node Exporter → Prometheus | ป้องกัน disk full ที่ทำให้ DB เสีย |
| Network traffic | Node Exporter → Prometheus | ตรวจจับ traffic ผิดปกติ / DDoS เบื้องต้น |
| Custom app metrics | Client library → Prometheus | track request rate, error rate ของแอปเอง |
สถาปัตยกรรม Prometheus + Grafana
ก่อนติดตั้ง ทำความเข้าใจส่วนประกอบก่อน:
- Node Exporter — รันบน VPS ที่ต้องการ monitor เปิด HTTP endpoint ที่ port 9100 ให้ Prometheus มาดึงข้อมูล
- Prometheus — scrape metrics จาก Node Exporter ทุกๆ 15 วินาที เก็บลง time-series database ของตัวเอง
- Grafana — เชื่อมต่อกับ Prometheus เป็น data source แล้วแสดงผลเป็น dashboard กราฟสวยงาม
- Alertmanager (optional) — รับ alert จาก Prometheus แล้วส่งต่อไปยัง Email, Slack, LINE Notify
ติดตั้ง Prometheus
เริ่มต้นด้วยการ download และติดตั้ง Prometheus บน Ubuntu 22.04:
# สร้าง user สำหรับ Prometheus (ไม่ควรรันด้วย root)
sudo useradd --no-create-home --shell /bin/false prometheus
# สร้าง directory
sudo mkdir /etc/prometheus /var/lib/prometheus
# Download Prometheus (ตรวจ version ล่าสุดที่ github.com/prometheus/prometheus)
PROM_VER="2.53.0"
wget https://github.com/prometheus/prometheus/releases/download/v${PROM_VER}/prometheus-${PROM_VER}.linux-amd64.tar.gz
tar -xzf prometheus-${PROM_VER}.linux-amd64.tar.gz
cd prometheus-${PROM_VER}.linux-amd64/
# Copy binary
sudo cp prometheus /usr/local/bin/
sudo cp promtool /usr/local/bin/
sudo chown prometheus:prometheus /usr/local/bin/prometheus /usr/local/bin/promtool
# Copy config files
sudo cp -r consoles/ /etc/prometheus/
sudo cp -r console_libraries/ /etc/prometheus/
sudo chown -R prometheus:prometheus /etc/prometheus /var/lib/prometheus
สร้าง config file เบื้องต้น
sudo nano /etc/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: ['localhost:9100']
สร้าง systemd service
sudo nano /etc/systemd/system/prometheus.service
[Unit]
Description=Prometheus
Wants=network-online.target
After=network-online.target
[Service]
User=prometheus
Group=prometheus
Type=simple
ExecStart=/usr/local/bin/prometheus \
--config.file /etc/prometheus/prometheus.yml \
--storage.tsdb.path /var/lib/prometheus/ \
--web.console.templates=/etc/prometheus/consoles \
--web.console.libraries=/etc/prometheus/console_libraries \
--storage.tsdb.retention.time=30d
[Install]
WantedBy=multi-user.target
sudo systemctl daemon-reload
sudo systemctl enable prometheus
sudo systemctl start prometheus
sudo systemctl status prometheus
ติดตั้ง Node Exporter
Node Exporter เป็นตัวดึง metrics ของ OS Linux — CPU, RAM, Disk, Network:
NODE_VER="1.8.1"
wget https://github.com/prometheus/node_exporter/releases/download/v${NODE_VER}/node_exporter-${NODE_VER}.linux-amd64.tar.gz
tar -xzf node_exporter-${NODE_VER}.linux-amd64.tar.gz
sudo cp node_exporter-${NODE_VER}.linux-amd64/node_exporter /usr/local/bin/
# สร้าง user แยก
sudo useradd --no-create-home --shell /bin/false node_exporter
# สร้าง systemd service
sudo nano /etc/systemd/system/node_exporter.service
[Unit]
Description=Node Exporter
Wants=network-online.target
After=network-online.target
[Service]
User=node_exporter
Group=node_exporter
Type=simple
ExecStart=/usr/local/bin/node_exporter
[Install]
WantedBy=multi-user.target
sudo systemctl daemon-reload
sudo systemctl enable node_exporter
sudo systemctl start node_exporter
ทดสอบว่า Node Exporter ทำงาน: curl http://localhost:9100/metrics | head -20
ตั้งค่า Prometheus ให้ scrape metrics
ไฟล์ prometheus.yml ด้านบนกำหนดให้ scrape localhost:9100 อยู่แล้ว ตรวจสอบว่า Node Exporter ถูก scrape โดยเปิด Prometheus UI:
http://YOUR_VPS_IP:9090 → Status → Targets → ควรเห็น node endpoint ขึ้น UP
ลอง query ตัวอย่างใน Prometheus UI Expression box:
# CPU usage (%)
100 - (avg by(instance)(rate(node_cpu_seconds_total{mode="idle"}[5m])) * 100)
# RAM ที่ใช้อยู่ (bytes)
node_memory_MemTotal_bytes - node_memory_MemAvailable_bytes
# Disk usage (%)
(node_filesystem_size_bytes - node_filesystem_free_bytes) / node_filesystem_size_bytes * 100
ติดตั้ง Grafana
Grafana ใช้ repository อย่างเป็นทางการ ติดตั้งได้ง่ายผ่าน apt:
sudo apt-get install -y apt-transport-https software-properties-common wget
wget -q -O - https://apt.grafana.com/gpg.key | gpg --dearmor | sudo tee /usr/share/keyrings/grafana.gpg > /dev/null
echo "deb [signed-by=/usr/share/keyrings/grafana.gpg] https://apt.grafana.com stable main" | sudo tee /etc/apt/sources.list.d/grafana.list
sudo apt-get update
sudo apt-get install -y grafana
sudo systemctl daemon-reload
sudo systemctl enable grafana-server
sudo systemctl start grafana-server
เปิด browser: http://YOUR_VPS_IP:3000 → login ด้วย admin / admin → ระบบจะให้เปลี่ยนรหัสผ่านทันที
สร้าง Dashboard แรก
ขั้นตอนการเชื่อม Prometheus กับ Grafana:
- ใน Grafana UI → Connections → Data sources → Add data source → เลือก Prometheus
- URL:
http://localhost:9090→ Save & test → ต้องขึ้น "Data source is working" - Import dashboard สำเร็จรูป → Dashboards → Import → ใส่ ID 1860 → Load → เลือก Prometheus data source → Import
| Panel ที่มีใน Dashboard 1860 | ข้อมูลที่แสดง |
|---|---|
| CPU Basic | User, System, IOWait, Idle % |
| Memory Basic | Total, Used, Free, Buffers/Cache |
| Disk Space Used % | Usage % ของแต่ละ partition |
| Network Traffic | Received/Transmitted bytes/s |
| System Load | 1m, 5m, 15m load average |
| Disk I/O | Read/Write bytes/s |
ตั้งค่า Alert เมื่อ CPU/RAM สูง
Prometheus สามารถ trigger alert ได้ผ่าน alert rules เพิ่ม file ใหม่:
sudo nano /etc/prometheus/alert.rules.yml
groups:
- name: node_alerts
rules:
- alert: HighCPU
expr: 100 - (avg by(instance)(rate(node_cpu_seconds_total{mode="idle"}[5m])) * 100) > 85
for: 5m
labels:
severity: warning
annotations:
summary: "CPU สูง {{ $value }}%"
description: "Instance {{ $labels.instance }} มี CPU usage สูงกว่า 85% นานกว่า 5 นาที"
- alert: LowMemory
expr: (node_memory_MemAvailable_bytes / node_memory_MemTotal_bytes) * 100 < 15
for: 5m
labels:
severity: critical
annotations:
summary: "RAM เหลือน้อย {{ $value }}%"
description: "Instance {{ $labels.instance }} มี available RAM น้อยกว่า 15%"
- alert: DiskAlmostFull
expr: (node_filesystem_size_bytes - node_filesystem_free_bytes) / node_filesystem_size_bytes * 100 > 85
for: 10m
labels:
severity: warning
annotations:
summary: "Disk เต็ม {{ $value }}%"
description: "Disk บน {{ $labels.instance }} ({{ $labels.mountpoint }}) ใช้ไปแล้ว {{ $value }}%"
เพิ่ม rule file เข้า prometheus.yml:
rule_files:
- "/etc/prometheus/alert.rules.yml"
sudo systemctl reload prometheus
ดู alert ที่ถูก trigger ได้ที่ Prometheus UI → Alerts
ความปลอดภัย — ปิด Port และ Basic Auth
ค่าเริ่มต้น Prometheus (9090) และ Node Exporter (9100) เปิด port ให้ทุกคนเข้าได้ ต้องล็อก firewall:
# ปิด port 9090, 9100 จากภายนอก (เหลือแค่ 3000 สำหรับ Grafana)
sudo ufw deny 9090
sudo ufw deny 9100
# หรือ allow เฉพาะ IP ของตัวเอง
sudo ufw allow from YOUR_OFFICE_IP to any port 9090
ssh -L 9090:localhost:9090 user@YOUR_VPS_IP แล้วเปิด http://localhost:9090 บนเครื่องตัวเอง ปลอดภัยกว่ามาก
เพิ่ม Basic Auth ให้ Grafana
Grafana มี authentication ในตัวอยู่แล้ว แนะนำให้ตั้งค่าเพิ่มเติม:
sudo nano /etc/grafana/grafana.ini
# แก้ไขส่วน [auth.anonymous]
[auth.anonymous]
enabled = false # ปิด anonymous access
# [security]
disable_initial_admin_creation = false
admin_password = YOUR_STRONG_PASSWORD
สรุป
ระบบ Monitoring ด้วย Prometheus + Grafana ช่วยให้คุณมองเห็น VPS ของตัวเองแบบ real-time และย้อนหลังได้ ขั้นตอนทั้งหมดที่ทำไปคือ:
- ติดตั้ง Prometheus เป็น time-series database สำหรับเก็บ metrics
- ติดตั้ง Node Exporter เพื่อ expose OS metrics ที่ port 9100
- เชื่อม Grafana กับ Prometheus และ import Dashboard 1860 พร้อมใช้ทันที
- ตั้งค่า alert rules สำหรับ CPU/RAM/Disk สูง เพื่อรับ notification ก่อนเกิดปัญหา
- ล็อก firewall ปิด port ที่ไม่จำเป็นจากภายนอก
เมื่อระบบทำงานแล้ว คุณจะมีข้อมูลเพียงพอในการวางแผน scale VPS ได้อย่างมีหลักการ ไม่ต้อง upgrade แบบเดาสุ่มอีกต่อไป หากต้องการ VPS ที่มี performance ดีพอสำหรับการรัน Prometheus + Grafana ดู VPS แพ็กเกจของ AsiaGB ได้เลย
FAQ — คำถามที่พบบ่อย
Prometheus และ Grafana ต่างกันอย่างไร?
Prometheus คือ time-series database ที่เก็บ metrics ส่วน Grafana คือ visualization tool ที่นำ metrics มาแสดงเป็นกราฟ ทั้งสองทำงานร่วมกัน Prometheus เก็บ Grafana แสดงผล
Node Exporter คืออะไร?
Node Exporter เป็น agent ที่รันบน Linux server เปิด HTTP endpoint ที่ port 9100 ให้ Prometheus มา scrape ข้อมูล OS เช่น CPU, RAM, Disk, Network โดยไม่ต้องแก้ OS ใดๆ
ต้องใช้ RAM เท่าไหร่สำหรับ stack นี้?
Prometheus + Grafana + Node Exporter ใช้ RAM รวมประมาณ 400-600 MB แนะนำ VPS ที่มี RAM อย่างน้อย 2 GB เพื่อให้ระบบหลักและ monitoring ทำงานได้ราบรื่น
จะส่ง alert ไป LINE Notify ได้ไหม?
ได้ — ใช้ Alertmanager ร่วมกับ webhook receiver ที่ส่งต่อไปยัง LINE Notify API หรือใช้ Grafana Alerting ที่มี notification channel สำหรับ LINE Notify โดยตรงแล้ว
Grafana dashboard สำเร็จรูป ID 1860 ใช้ได้ทันทีเลยไหม?
ใช้ได้เลย เพียงแค่ import dashboard ID 1860 จาก grafana.com เลือก Prometheus เป็น data source แล้วจะเห็น panel CPU, RAM, Disk, Network ครบถ้วนทันที ไม่ต้องสร้าง query เอง
Prometheus เก็บข้อมูลย้อนหลังได้นานแค่ไหน?
ค่าเริ่มต้น 15 วัน ปรับเป็น 30 วันได้ด้วย flag --storage.tsdb.retention.time=30d แต่ต้องระวัง disk ที่เพิ่มขึ้น โดยทั่วไป 30 วันใช้ disk ประมาณ 2-5 GB ขึ้นกับจำนวน series