Blog2026-03-102 min read
Monitor container project
Có một vài cách phổ biến để thu thập metrics của container lên Grafana. Stack phổ biến nhất là cAdvisor + Prometheus + Grafana. Stack: cAdvisor → Prometheus → G
Nguyen Pham
•Xin chào. Mình thích lập trình và công nghệ. Đây là blog mình chia sẻ lại các kiến thức về lập trình, công nghệ.
Có một vài cách phổ biến để thu thập metrics của container lên Grafana. Stack phổ biến nhất là cAdvisor + Prometheus + Grafana.
Stack: cAdvisor → Prometheus → Grafana#
1. Tổng quan luồng dữ liệu#
codeContainer (Docker) ↓ cAdvisor (thu thập metrics) ↓ Prometheus (lưu trữ time-series) ↓ Grafana (visualize)
2. Docker Compose đầy đủ#
codeversion: '3.8' services: cadvisor: image: gcr.io/cadvisor/cadvisor:latest container_name: cadvisor ports: - "8080:8080" volumes: - /:/rootfs:ro - /var/run:/var/run:ro - /sys:/sys:ro - /var/lib/docker/:/var/lib/docker:ro restart: unless-stopped prometheus: image: prom/prometheus:latest container_name: prometheus ports: - "9090:9090" volumes: - ./prometheus.yml:/etc/prometheus/prometheus.yml restart: unless-stopped grafana: image: grafana/grafana:latest container_name: grafana ports: - "3000:3000" environment: - GF_SECURITY_ADMIN_PASSWORD=admin volumes: - grafana-data:/var/lib/grafana restart: unless-stopped volumes: grafana-data:
3. Cấu hình Prometheus (prometheus.yml)#
codeglobal: scrape_interval: 15s scrape_configs: - job_name: 'cadvisor' static_configs: - targets: ['cadvisor:8080'] - job_name: 'prometheus' static_configs: - targets: ['localhost:9090']
4. Kết nối Grafana với Prometheus#
- Truy cập
http://localhost:3000(user/pass:admin/admin) - Vào Connections → Data Sources → Add data source
- Chọn Prometheus
- URL:
http://prometheus:9090 - Nhấn Save & Test
5. Import Dashboard có sẵn#
Thay vì tự tạo panel, import dashboard có sẵn từ Grafana Labs:
- Vào Dashboards → Import
- Nhập ID:
14282(Docker & Container Monitoring) - Hoặc ID:
893(Docker Dashboard) - Chọn Prometheus datasource → Import
6. Một số PromQL query hữu ích#
| Metric | Query |
|---|---|
| CPU usage | rate(container_cpu_usage_seconds_total[1m]) |
| RAM usage | container_memory_usage_bytes |
| Network in | rate(container_network_receive_bytes_total[1m]) |
| Network out | rate(container_network_transmit_bytes_total[1m]) |
| Disk I/O | rate(container_fs_reads_bytes_total[1m]) |
Các stack thay thế khác#
| Stack | Mô tả |
|---|---|
| Node Exporter + Prometheus + Grafana | Thu thập metrics máy chủ (CPU, RAM, disk của host) |
| Grafana Agent | All-in-one agent của Grafana, thay thế cho Prometheus + cAdvisor |
| Telegraf + InfluxDB + Grafana | Stack TIG, phù hợp với InfluxDB |
| Datadog / New Relic | SaaS, không cần tự quản lý |
Bạn đang dùng Docker hay Kubernetes? Và muốn monitor ở mức container hay cả host machine? Mình có thể hướng dẫn chi tiết hơn theo use case cụ thể.
Tags:#Blog