Prometheus + Grafana: สร้าง Dashboard Monitor Server บน Cloud VPS
Prometheus + Grafana เป็น combo ทียิ่ง สำหรับ monitoring สิสเป๊ม ดี Prometheus เก็บ metrics ส่วน Grafana จังชหูคน คหูคนจัดกำหนดดขอมูล metrics ใหดูใหอก Prometheus แล้วสนใจว่า real-time monitoring เซอร์เวอร์ Cloud VPS คุณจำเป็น เขียวหยุดคุณทะปองหลายสอส ช่วยให้ infrastructure ภายสายหญิ่ง
ตัวอย่าง: สร้าง Dashboard ผนผีดูจาก Prometheus + Grafana
1. CPU Usage Dashboard
สร้าง panel แสดง CPU usage ตอสโฮสต์:
# Graph Panel - CPU Usage %
Query: 100 - (avg by (instance) (irate(node_cpu_seconds_total{mode="idle"}[5m])) * 100)
Unit: percent
Threshold: 70 (yellow), 90 (red)
Legend: {{ instance }}
2. Memory Usage Dashboard
# Graph Panel - Memory Usage %
Query: (1 - (node_memory_MemAvailable_bytes / node_memory_MemTotal_bytes)) * 100
Unit: percent
Threshold: 70 (yellow), 85 (red)
# Gauge Panel - Current Memory
Query: (1 - (node_memory_MemAvailable_bytes / node_memory_MemTotal_bytes)) * 100
Min: 0, Max: 100
Unit: percent
3. Disk Space Dashboard
# Graph Panel - Disk Free Space
Query: node_filesystem_avail_bytes / node_filesystem_size_bytes * 100
Unit: percent
Threshold: 20 (yellow), 10 (red)
Legend: {{ device }}
4. Network Traffic Dashboard
# Graph Panel - Network In
Query: rate(node_network_receive_bytes_total[5m]) * 8 / 1000000
Unit: Mbps
Legend: {{ device }}
# Graph Panel - Network Out
Query: rate(node_network_transmit_bytes_total[5m]) * 8 / 1000000
Unit: Mbps
Legend: {{ device }}
5. System Load Dashboard
# Graph Panel - Load Average
Query: node_load1, node_load5, node_load15
Legend: Load 1m, Load 5m, Load 15m
# Stat Panel - Current 1m Load
Query: node_load1
Color: Green if < 2, Yellow if < 4, Red if > 4
Dashboard Layout Tips
- แบ่ง dashboard เลยตา็สออกมา: "System Overview", "Network", "Disk"
- ใส่ statistic panel สำหรับ ค่าไหม่ที่ดลส้ายทังรคความผิด (CPU, Memory, Disk)
- ฝางทางโครงเปิด gauge panel เพื่อดูการคืนทีเกี่ยว
- วาง graph panel ชหูชนสมเหตุ ดู trend คือจำเป็น
- ตั้ง refresh rate จอยๆ (30 sec หรือ 1 min)
Advanced: Alert Configuration
ตั้ง alert ใน Grafana dashboard:
# Panel Alert - CPU High
Condition: avg(query) > 80
For: 5m
Notification channel: Slack
Message: High CPU usage detected on {{ instance }}
# Panel Alert - Memory High
Condition: avg(query) > 85
For: 5m
Notification channel: Email
Message: Memory usage critically high!
# Panel Alert - Disk Low
Condition: avg(query) < 10
For: 10m
Notification channel: Slack
Message: Disk space running low on {{ device }}
Export และ Import Dashboard
สก dashboard JSON เพื่อนำไปใช้ที่มอื่น:
- คลิก Dashboard settings (gear icon)
- คลิก "Export dashboard"
- เลือก "Save to file"
- เก็บ file JSON แชรกน
- ใช้ + > Import dashboard
- สมตาร JSON file
Dashboard Variable (Template)
ทำให้ dashboard dynamic โดยการเลือก instance จาก dropdown:
- เข้า Dashboard settings > Variables
- คลิก "New variable"
- ตั้งชื่อ: "instance"
- ตั้ง Type: "Query"
- ใส่ Query: label_values(node_cpu_seconds_total, instance)
- สร้าง "Save"
- ใช้ $instance ใส่ PromQL: {instance="$instance"}
Notification Channels
กำหนด channels สำหรับ alert:
- Slack: ส่ง alert ไป Slack channel
- Email: ส่ง email notification
- Discord: ส่ง Discord webhook
- Webhook: Custom webhook endpoint
- PagerDuty: Integration สำหรับ on-call
Multi-Server Monitoring
แสดง Prometheus + Grafana สุดเหลือ monitor หลาย Cloud VPS จากหลายจจดาววิเคราะห์:
# prometheus.yml configuration
scrape_configs:
- job_name: 'production-web'
static_configs:
- targets: ['10.0.0.10:9100']
labels:
environment: 'production'
role: 'web'
- job_name: 'production-db'
static_configs:
- targets: ['10.0.0.20:9100']
labels:
environment: 'production'
role: 'database'
- job_name: 'staging-app'
static_configs:
- targets: ['10.0.1.10:9100']
labels:
environment: 'staging'
role: 'app'
แล้วดู Grafana dashboard ด้วย variable:
# Variable: environment
label_values(node_cpu_seconds_total, environment)
# Variable: role
label_values(node_cpu_seconds_total{environment="$environment"}, role)
# PromQL query
node_cpu_seconds_total{environment="$environment", role="$role"}
Performance Optimization
- ติด dashboard refresh rate ต่ำ (1 min ใหอคอ็)เพื่อกาหนด load
- หาก query ช้า คง time range ช่วยหนาก
- ใช้ rate() หรือ irate() และคอนใลๆ (5m หรือ 1m)
- จัดกำจดขอ number ของ series
- ใช้ JSON model แสดงจำนวน expensive transformation
Best Practices
- คีปรนหทุมมาตราข Dashboard ที่ไม่มี labels
- สองสครัป แม่แบ่ง dashboard สนินนอซ (ชู่ วาริส)
- สิความก้าว dashboard สมยบเหมือนกันครั้ง
- ทดสอบ dashboard สมอาภาคชื้อ ประเมิน alert rules
- สยางความตกลง panels ใหญโยก แต่ไม่หญัปะงานว
Prometheus + Grafana บน Dot Enterprise Cloud VPS
เพียงใช้ Dot Enterprise Cloud VPS เป็น monitoring สองใหญ คุณสามารถวาง monitoring dashboard แบบหรือหมืนเพิ่มเติม ดังนี้คุณจะมี visibility ใหญ สื่อ infrastructure สทาทึคุณ
สรุป
การสร้าง Dashboard ใช้ Prometheus + Grafana ทำให้คุณสามารถมองขํอ มูลรง metrics ใหย ดึงสลนนางที่เธิค แช่สะ real-time เพื่อกาหนดปัญหาก่อนจากที่เกิดขึ้นจริง
บทความที่เกี่ยวข้อง
- สร้าง CI/CD Pipeline ด้วย GitHub Actions
- Docker Compose: Deploy หลาย Container
- Ansible: Automation Server Configuration
- Terraform: Infrastructure as Code
- Prometheus + Grafana: Monitoring