เมื่อ VPS รันงานจริง คุณต้องรู้ว่า CPU ใช้เท่าไหร่ RAM เหลือเท่าไหร่ Disk กำลังจะเต็มไหม — ก่อนที่ปัญหาจะเกิดขึ้น บทความนี้จะพาคุณติดตั้ง Netdata (ง่าย เร็ว) และ Prometheus + Grafana (ยืดหยุ่น ทรงพลัง) บน VPS Ubuntu ทีละขั้นตอน
VPS ที่ไม่มี monitoring เปรียบเหมือนรถที่ไม่มีแผงหน้าปัด คุณไม่รู้ว่าน้ำมันกำลังจะหมด หรือเครื่องกำลังจะพัง จนกว่าจะสายเกินไป
ปัญหาที่มักเกิดกับ VPS ที่ไม่มี monitoring:
| คุณสมบัติ | Netdata | Prometheus + Grafana |
|---|---|---|
| ความง่ายในการติดตั้ง | ⭐⭐⭐⭐⭐ ง่ายมาก (1 คำสั่ง) | ⭐⭐⭐ ต้องตั้งค่าหลายขั้นตอน |
| Real-time Dashboard | ✅ มี UI พร้อมใช้ทันที | ❌ ต้อง import dashboard |
| ความยืดหยุ่น | ปานกลาง | ⭐⭐⭐⭐⭐ สูงมาก |
| Long-term Storage | จำกัด (default 24h) | ✅ เก็บได้ตามต้องการ |
| Resource ที่ใช้ | RAM ~50-100 MB | RAM ~300-700 MB |
| Multi-server | Netdata Cloud (ฟรี) | ✅ รองรับ native |
| เหมาะกับ | VPS เดี่ยว, มือใหม่ | Cluster, Production ขนาดใหญ่ |
ฟรี Ubuntu 20.04/22.04/24.04
Netdata มี one-line installer ที่ตรวจสอบ OS และติดตั้งอัตโนมัติ:
# อัพเดต package list ก่อน
sudo apt update
# ติดตั้ง Netdata ด้วย one-line installer
wget -O /tmp/netdata-kickstart.sh https://get.netdata.cloud/kickstart.sh
sh /tmp/netdata-kickstart.sh --stable-channel --no-updates
หลังติดตั้งเสร็จ เช็คว่า service รันอยู่:
sudo systemctl status netdata
# ต้องเห็น: active (running)
เปิด Dashboard ที่ http://YOUR_VPS_IP:19999 จะเห็น real-time graphs ทันที
# เพิ่ม Netdata repo
curl -s https://packagecloud.io/install/repositories/netdata/netdata/script.deb.sh | sudo bash
# ติดตั้ง
sudo apt install netdata -y
# เปิดใช้งาน
sudo systemctl enable netdata
sudo systemctl start netdata
แก้ไขไฟล์ /etc/netdata/netdata.conf:
[web]
# อนุญาตเฉพาะ localhost (แล้วใช้ SSH Tunnel เข้าถึง)
bind to = 127.0.0.1
# หรือระบุ IP ที่อนุญาต
allow connections from = localhost 192.168.1.*
sudo systemctl restart netdata
# รันบน local machine
ssh -L 19999:127.0.0.1:19999 user@YOUR_VPS_IP
# แล้วเปิด browser ที่
# http://localhost:19999
sudo nano /etc/netdata/health_alarm_notify.conf
# Email
SEND_EMAIL="YES"
DEFAULT_RECIPIENT_EMAIL="[email protected]"
SENDMAIL="$(which sendmail)"
Prometheus ต้องติดตั้ง 2 ส่วน: ตัว Prometheus (เก็บ metric) และ Node Exporter (ดึง metric จาก OS)
# ดาวน์โหลด Node Exporter (เช็ค version ล่าสุดที่ github.com/prometheus/node_exporter)
cd /tmp
wget https://github.com/prometheus/node_exporter/releases/download/v1.8.2/node_exporter-1.8.2.linux-amd64.tar.gz
tar xvf node_exporter-1.8.2.linux-amd64.tar.gz
sudo cp node_exporter-1.8.2.linux-amd64/node_exporter /usr/local/bin/
# สร้าง service user
sudo useradd -rs /bin/false node_exporter
สร้าง systemd service:
sudo nano /etc/systemd/system/node_exporter.service
[Unit]
Description=Node Exporter
After=network.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
# เช็ค: curl http://localhost:9100/metrics | head -20
cd /tmp
wget https://github.com/prometheus/prometheus/releases/download/v2.54.1/prometheus-2.54.1.linux-amd64.tar.gz
tar xvf prometheus-2.54.1.linux-amd64.tar.gz
sudo useradd -rs /bin/false prometheus
sudo mkdir /etc/prometheus /var/lib/prometheus
sudo cp prometheus-2.54.1.linux-amd64/prometheus /usr/local/bin/
sudo cp prometheus-2.54.1.linux-amd64/promtool /usr/local/bin/
sudo cp -r prometheus-2.54.1.linux-amd64/consoles /etc/prometheus
sudo cp -r prometheus-2.54.1.linux-amd64/console_libraries /etc/prometheus
ตั้งค่า Prometheus:
sudo nano /etc/prometheus/prometheus.yml
global:
scrape_interval: 15s
scrape_configs:
- job_name: 'prometheus'
static_configs:
- targets: ['localhost:9090']
- job_name: 'node_exporter'
static_configs:
- targets: ['localhost:9100']
sudo chown -R prometheus:prometheus /etc/prometheus /var/lib/prometheus
สร้าง systemd service:
sudo nano /etc/systemd/system/prometheus.service
[Unit]
Description=Prometheus
After=network.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/ \
--storage.tsdb.retention.time=30d \
--web.listen-address=127.0.0.1:9090
[Install]
WantedBy=multi-user.target
sudo systemctl daemon-reload
sudo systemctl enable prometheus
sudo systemctl start prometheus
sudo systemctl status prometheus
sudo apt install -y apt-transport-https software-properties-common wget
sudo wget -q -O /usr/share/keyrings/grafana.key https://apt.grafana.com/gpg.key
echo "deb [signed-by=/usr/share/keyrings/grafana.key] 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 grafana-server
sudo systemctl start grafana-server
เข้าถึงผ่าน SSH Tunnel: ssh -L 3000:127.0.0.1:3000 user@VPS_IP แล้วเปิด http://localhost:3000
admin Password: admin (เปลี่ยนทันที)http://localhost:90901860 (Node Exporter Full — dashboard ยอดนิยม)คุณจะได้ dashboard สวยงามแสดง CPU, RAM, Disk, Network ทันที
| Metric | เกณฑ์ปกติ | ต้องระวัง |
|---|---|---|
| CPU Usage | < 70% | > 90% ต่อเนื่อง 5 นาที |
| RAM Usage | < 80% | > 90% (เสี่ยง OOM) |
| Disk Usage | < 70% | > 85% (วางแผน clean up) |
| Disk I/O | ขึ้นอยู่กับ workload | Wait > 20% ต่อเนื่อง |
| Network In/Out | ตามปกติใช้งาน | spike ผิดปกติ |
| Load Average | < จำนวน CPU | > 2× CPU cores |
| Swap Usage | 0-20% | > 50% (RAM ไม่พอ) |
Netdata มี alert พร้อมใช้จำนวนมากใน /etc/netdata/health.d/ แก้ threshold ได้ เช่น:
# /etc/netdata/health.d/cpu.conf
alarm: cpu_usage
on: system.cpu
lookup: average -1m percentage of user,system
units: %
warn: $this > 80
crit: $this > 95
info: CPU usage is high
Dashboard ไม่ควรเปิดสาธารณะ เพราะมีข้อมูล internal ของเซิร์ฟเวอร์ วิธีป้องกัน:
# เข้าถึง Netdata
ssh -L 19999:127.0.0.1:19999 user@VPS_IP &
# เข้าถึง Grafana
ssh -L 3000:127.0.0.1:3000 user@VPS_IP &
sudo apt install -y nginx apache2-utils
# สร้าง password file
sudo htpasswd -c /etc/nginx/.htpasswd admin
# /etc/nginx/sites-available/netdata
server {
listen 80;
server_name monitor.yourdomain.com;
auth_basic "Monitoring";
auth_basic_user_file /etc/nginx/.htpasswd;
location / {
proxy_pass http://127.0.0.1:19999;
proxy_set_header Host $host;
}
}
# บล็อก port monitoring จาก internet
sudo ufw deny 19999
sudo ufw deny 9090
sudo ufw deny 3000
sudo ufw allow from YOUR_IP_ADDRESS to any port 19999
บทความที่เกี่ยวข้อง: ตั้งค่า UFW Firewall บน VPS • สร้าง systemd Service • Grafana + Prometheus Monitor VPS