---
title: "Monitor container project"
description: "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"
date: "2026-03-10"
author:
  name: "Nguyen Pham"
  role: "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ệ."
  avatar: "http://0.gravatar.com/avatar/6fb61398e24046b34e13870dd6c357b9?s=128&d=mm&r=g"
category: "Blog"
tags: ["Blog"]
coverImage: "/blog/covers/monitor-container-project.png"
featured: false
readingTime: "2 min read"
---

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

```
Container (Docker)
    ↓
cAdvisor (thu thập metrics)
    ↓
Prometheus (lưu trữ time-series)
    ↓
Grafana (visualize)
```

---

### 2. Docker Compose đầy đủ

```
version: '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`)

```
global:
  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

1. Truy cập `http://localhost:3000` (user/pass: `admin/admin`)
2. Vào **Connections → Data Sources → Add data source**
3. Chọn **Prometheus**
4. URL: `http://prometheus:9090`
5. 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ể.
