ติดตั้ง Prometheus บน Cloud VPS เก็บ Metrics ของ Server
Prometheus เป็นระบบ monitoring แบบ open-source ที่พัฒนาโดย Cloud Native Computing Foundation ใช้สำหรับเก็บและวิเคราะห์ metrics ของเซอร์เวอร์และแอปพลิเคชัน เป็น time-series database ที่เก็บข้อมูลเป็นชุดเลขตามลำดับเวลา ทำให้สามารถดูข้อมูลเหตุการณ์ในอดีตและปัจจุบันได้ สำหรับ Cloud VPS คุณสามารถติดตั้ง Prometheus เพื่อ monitor resource usage อย่างแบบ real-time
ทำไมต้องใช้ Prometheus?
Prometheus มีข้อดีหลายประการที่ทำให้มันเป็นตัวเลือกที่ดีสำหรับ monitoring Cloud VPS:
- Pull-based model: Prometheus ดึง metrics จาก exporter มากกว่ารอให้ exporter push ข้อมูล ทำให้ง่ายกว่าในการจัดการ
- Time-series database: เคยเก็บข้อมูลตามลำดับเวลา ดึงข้อมูลออกมาวิเคราะห์ได้ง่าย
- Flexible query language (PromQL): มีภาษา query ที่ทรงพลัง ช่วยให้ query metrics ได้อย่างยืดหยุ่น
- Built-in alerting: สามารถตั้ง alert rules ภายใน Prometheus ได้โดยตรง
- Lightweight: ขนาดเล็ก resources ใช้ไม่มาก ติดตั้งได้บน Cloud VPS ระดับกลาง
- Integration with Grafana: รวมกับ Grafana ง่ายๆ เพื่อทำ visualization
สถาปัตยกรรม Prometheus
Prometheus มี architecture ดังนี้:
- Prometheus Server: เซอร์เวอร์หลักที่ดึง metrics และเก็บใน database
- Exporters: ตัวแปลงข้อมูล (เช่น Node Exporter สำหรับ Linux) ที่ expose metrics ให้ Prometheus เข้าถึง
- Alertmanager: จัดการและส่ง alert ไปยัง Slack, email, เป็นต้น
- Pushgateway: รับ metrics จากงาน short-lived (เช่น cron job)
ขั้นตอนติดตั้ง Prometheus บน Cloud VPS
ขั้นที่ 1: ดาวน์โหลดและติดตั้ง Prometheus
# ไปยัง home directory
cd /opt
# ดาวน์โหลด Prometheus เวอร์ชันล่าสุด
wget https://github.com/prometheus/prometheus/releases/download/v2.51.0/prometheus-2.51.0.linux-amd64.tar.gz
# แตกไฟล์
tar xzf prometheus-2.51.0.linux-amd64.tar.gz
# ย้ายชื่อ
mv prometheus-2.51.0.linux-amd64 prometheus
# สร้าง directory สำหรับเก็บ data
mkdir -p /var/lib/prometheus
chown -R prometheus:prometheus /var/lib/prometheus
ขั้นที่ 2: สร้าง Systemd Service
สร้างไฟล์ /etc/systemd/system/prometheus.service:
[Unit]
Description=Prometheus
Wants=network-online.target
After=network-online.target
[Service]
Type=simple
User=prometheus
Group=prometheus
ExecStart=/opt/prometheus/prometheus --config.file=/opt/prometheus/prometheus.yml --storage.tsdb.path=/var/lib/prometheus
Restart=on-failure
RestartSec=10
[Install]
WantedBy=multi-user.target
อัปเดต systemd และเปิด service:
sudo systemctl daemon-reload
sudo systemctl enable prometheus
sudo systemctl start prometheus
sudo systemctl status prometheus
ขั้นที่ 3: ตั้งค่า Prometheus Configuration
แก้ไข /opt/prometheus/prometheus.yml:
global:
scrape_interval: 15s
evaluation_interval: 15s
alerting:
alertmanagers:
- static_configs:
- targets: []
rule_files:
- 'alert_rules.yml'
scrape_configs:
# Prometheus self-monitoring
- job_name: 'prometheus'
static_configs:
- targets: ['localhost:9090']
# Node Exporter สำหรับ Linux system metrics
- job_name: 'node'
static_configs:
- targets: ['localhost:9100']
labels:
instance: 'cloud-vps-01'
# อื่นๆ exporter เพิ่มเติม
- job_name: 'docker'
static_configs:
- targets: ['localhost:9323']
ขั้นที่ 4: ติดตั้ง Node Exporter
Node Exporter เป็น exporter ที่ expose metrics ของ Linux system เช่น CPU, memory, disk:
# ดาวน์โหลด Node Exporter
cd /opt
wget https://github.com/prometheus/node_exporter/releases/download/v1.7.0/node_exporter-1.7.0.linux-amd64.tar.gz
tar xzf node_exporter-1.7.0.linux-amd64.tar.gz
mv node_exporter-1.7.0.linux-amd64 node_exporter
# สร้าง systemd service สำหรับ Node Exporter
cat > /etc/systemd/system/node_exporter.service << EOF
[Unit]
Description=Node Exporter
After=network.target
[Service]
Type=simple
User=prometheus
Group=prometheus
ExecStart=/opt/node_exporter/node_exporter
Restart=on-failure
RestartSec=10
[Install]
WantedBy=multi-user.target
EOF
# เปิด Node Exporter
sudo systemctl daemon-reload
sudo systemctl enable node_exporter
sudo systemctl start node_exporter
ขั้นที่ 5: ตรวจสอบว่า Prometheus ทำงาน
# ตรวจสอบ Prometheus web UI
curl http://localhost:9090
# ตรวจสอบ Node Exporter metrics
curl http://localhost:9100/metrics | head -20
เข้า browser ที่ http://YOUR_VPS_IP:9090 เพื่อดู Prometheus web UI
PromQL: Prometheus Query Language
PromQL เป็นภาษา query ของ Prometheus ตัวอย่างคำสั่ง:
# ดูค่า CPU usage ปัจจุบัน
node_cpu_seconds_total
# ดู CPU usage เป็นเปอร์เซ็นต์
100 - (avg by (instance) (irate(node_cpu_seconds_total{mode="idle"}[5m])) * 100)
# ดู Memory usage
node_memory_MemAvailable_bytes / node_memory_MemTotal_bytes * 100
# ดู Disk usage
node_filesystem_avail_bytes / node_filesystem_size_bytes * 100
# ดู rate ของ network traffic
irate(node_network_transmit_bytes_total[5m])
ตั้งค่า Alert Rules
สร้างไฟล์ /opt/prometheus/alert_rules.yml เพื่อตั้งค่า alert:
groups:
- name: system_alerts
interval: 30s
rules:
# Alert เมื่อ CPU usage สูงกว่า 80% นานกว่า 5 นาที
- alert: HighCPUUsage
expr: |
100 - (avg by (instance) (irate(node_cpu_seconds_total{mode="idle"}[5m])) * 100) > 80
for: 5m
annotations:
summary: "High CPU usage on {{ $labels.instance }}"
description: "CPU usage is {{ $value }}%"
# Alert เมื่อ Memory ใช้มากกว่า 85%
- alert: HighMemoryUsage
expr: |
(1 - node_memory_MemAvailable_bytes / node_memory_MemTotal_bytes) * 100 > 85
for: 5m
annotations:
summary: "High memory usage on {{ $labels.instance }}"
description: "Memory usage is {{ $value }}%"
# Alert เมื่อ Disk เหลือเพียง 10%
- alert: LowDiskSpace
expr: |
node_filesystem_avail_bytes / node_filesystem_size_bytes * 100 < 10
for: 5m
annotations:
summary: "Low disk space on {{ $labels.instance }}"
description: "Available disk space is {{ $value }}%"
เชื่อมต่อ Grafana กับ Prometheus
ถ้าต้องการสร้าง dashboard สวยงาม สามารถใช้ Grafana ร่วมกับ Prometheus ดู ติดตั้ง Grafana บน Cloud VPS และ สร้าง Dashboard สำหรับ Monitoring
Troubleshooting Prometheus
- Prometheus ไม่เก็บ metrics: ตรวจสอบว่า exporter ทำงาน curl http://localhost:9100/metrics
- Memory ใช้เยอะเกินไป: เพิ่มค่า retention ใน prometheus.yml --storage.tsdb.retention.time=15d
- Query ช้า: ลอง optimize PromQL query หรือ increase resources
- Targets down: เข้า Status > Targets ดู exporter ไหนที่ down
Prometheus บน Dot Enterprise Cloud VPS
ถ้าใช้ Dot Enterprise Cloud VPS เพื่อรัน Prometheus ให้หลีกเลี่ยงการใช้ shared hosting ให้ใช้ dedicated instance เพื่อให้ Prometheus มี resources เพียงพอ โดยทั่วไป 2 CPU 4GB RAM ก็เพียงพอสำหรับ monitoring เซอร์เวอร์หลายสิบตัว
Best Practices
- ตั้ง scrape_interval ให้เหมาะสม 15-30 วินาที ส่วนใหญ่
- ใช้ retention period สมควร 15-30 วันอาจพอสำหรับ monitoring ทั่วไป
- ใส่ labels ให้ metrics เพื่อง่ายต่อการ query
- ตั้ง alert rules ที่สมเหตุสมผล ไม่ห่วง alert เกินไป
- ให้ Prometheus มี dedicated storage ไม่ใช้ shared
- Backup Prometheus config file เป็นประจำ
บทความที่เกี่ยวข้อง
- สร้าง CI/CD Pipeline ด้วย GitHub Actions
- Docker Compose: Deploy หลาย Container
- Ansible: Automation Server Configuration
- Terraform: Infrastructure as Code
- Prometheus + Grafana: Monitoring