健康监控 cAdvisor
什么是 cAdvisor
cAdvisor(Container Advisor)是 Google 开源的容器监控工具,自动发现和监控运行中的容器,采集 CPU、内存、网络、磁盘等资源使用指标。它提供了 Web UI 和 REST API,是容器监控的基础组件。
cAdvisor 的工作原理
cAdvisor 启动 → 监听 /var/run/docker.sock
↓
发现所有容器 → 采集 cgroups 指标
↓
汇总指标 → Web UI (8080) + REST API
↓
导出到 Prometheus / InfluxDB / ...
启动 cAdvisor
最简单的启动
docker run \
-d \
--name=cadvisor \
--restart=always \
-p 8080:8080 \
-v /:/rootfs:ro \
-v /var/run:/var/run:ro \
-v /sys:/sys:ro \
-v /var/lib/docker/:/var/lib/docker:ro \
google/cadvisor:latest
# 访问 Web UI
# http://localhost:8080
Docker Compose 部署
version: "3.8"
services:
cadvisor:
image: google/cadvisor:latest
container_name: cadvisor
restart: always
ports:
- "8080:8080"
volumes:
- /:/rootfs:ro
- /var/run:/var/run:ro
- /sys:/sys:ro
- /var/lib/docker/:/var/lib/docker:ro
privileged: true
devices:
- /dev/kmsg
cAdvisor 监控指标
CPU 指标
# Web UI 路径: /containers/
# 图表展示:
# - CPU 使用率(总计和每核)
# - CPU 负载
# - CPU 节流时间
# API 获取
curl http://localhost:8080/api/v2.0/stats/docker/container_name
内存指标
# 监控指标
# - 内存使用量
# - 内存限制
# - 缓存/缓冲区
# - Swap 使用
# API
curl http://localhost:8080/api/v2.0/spec/docker/container_name
网络指标
# 监控指标
# - 网络接收/发送字节
# - 网络接收/发送包数
# - 错误数
# - 丢弃数
磁盘指标
# 监控指标
# - 镜像层大小
# - 可写层大小
# - 文件系统使用
cAdvisor API
主要端点
| 端点 | 功能 |
|---|---|
/api/v1.3/containers/ |
容器列表和摘要 |
/api/v2.0/stats/{container} |
容器实时统计 |
/api/v2.0/spec/{container} |
容器资源配置 |
/api/v2.0/version/ |
cAdvisor 版本 |
/metrics |
Prometheus 格式指标 |
/docker/{container} |
Docker 容器详情 |
使用 API
# 列出所有容器
curl http://localhost:8080/api/v1.3/containers/
# 获取特定容器的统计
curl http://localhost:8080/api/v2.0/stats/docker/abc123...
# 获取 Prometheus 指标
curl http://localhost:8080/metrics
# 获取容器资源规格
curl http://localhost:8080/api/v2.0/spec/docker/abc123...
与 Prometheus 集成
cAdvisor 天然支持 Prometheus 指标格式:
# prometheus.yml
scrape_configs:
- job_name: 'cadvisor'
scrape_interval: 15s
static_configs:
- targets: ['cadvisor:8080']
关键 Prometheus 指标
# CPU 使用率
rate(container_cpu_usage_seconds_total[1m])
# 内存使用率
container_memory_usage_bytes / container_spec_memory_limit_bytes
# 网络 I/O
rate(container_network_receive_bytes_total[1m])
rate(container_network_transmit_bytes_total[1m])
# 磁盘 I/O
rate(container_fs_io_current[1m])
# 容器重启次数
container_restarts_total
Grafana Dashboard
{
"dashboard": {
"title": "Docker Container Monitoring",
"panels": [
{
"title": "CPU Usage",
"type": "graph",
"targets": [{
"expr": "sum(rate(container_cpu_usage_seconds_total{name!=\"\"}[1m])) by (name)"
}]
},
{
"title": "Memory Usage",
"type": "graph",
"targets": [{
"expr": "sum(container_memory_usage_bytes{name!=\"\"}) by (name)"
}]
}
]
}
}
完整监控栈示例
version: "3.8"
services:
cadvisor:
image: google/cadvisor:latest
volumes:
- /:/rootfs:ro
- /var/run:/var/run:ro
- /sys:/sys:ro
- /var/lib/docker/:/var/lib/docker:ro
ports:
- "8080:8080"
restart: always
prometheus:
image: prom/prometheus:latest
volumes:
- ./prometheus.yml:/etc/prometheus/prometheus.yml
- prometheus-data:/prometheus
ports:
- "9090:9090"
restart: always
grafana:
image: grafana/grafana:latest
volumes:
- grafana-data:/var/lib/grafana
ports:
- "3000:3000"
environment:
- GF_SECURITY_ADMIN_PASSWORD=admin
restart: always
volumes:
prometheus-data:
grafana-data:
告警规则
# prometheus-rules.yml
groups:
- name: container-alerts
rules:
- alert: HighCPUUsage
expr: >
sum(rate(container_cpu_usage_seconds_total[5m])) by (name) /
sum(machine_cpu_cores) * 100 > 80
for: 5m
labels:
severity: warning
annotations:
summary: "Container {{ $labels.name }} CPU usage > 80%"
- alert: HighMemoryUsage
expr: >
container_memory_usage_bytes / container_spec_memory_limit_bytes * 100 > 85
for: 5m
labels:
severity: warning
annotations:
summary: "Container {{ $labels.name }} memory usage > 85%"
- alert: ContainerRestart
expr: rate(container_restarts_total[10m]) > 0.1
for: 2m
labels:
severity: critical
annotations:
summary: "Container {{ $labels.name }} restarting frequently"
cAdvisor 的局限性和替代方案
| 工具 | 特点 | 适用场景 |
|---|---|---|
| cAdvisor | 轻量、实时 | 单机监控基础 |
| Prometheus + cAdvisor | 持久化+告警 | 多机集群监控 |
| Datadog Agent | 商业支持 | 云端监控 |
| Sysdig | 系统级追踪 | 深入诊断 |
| Netdata | 美观、实时 | 可视化监控 |
最佳实践
- 始终运行 cAdvisor:所有 Docker 主机都应该部署
- 搭配 Prometheus:持久化和告警
- 设置合理的保留期:Prometheus 默认 15 天
- 配置告警:资源使用超限通知
- 监控 cAdvisor 本身:确保监控工具本身可用
- 使用 Grafana 可视化:直观的 Dashboard
# 快速检查所有容器状态
curl -s http://localhost:8080/api/v1.3/containers/ | jq '. | keys'
# 查看资源使用 Top 5
curl -s http://localhost:8080/api/v2.0/summary/ | jq '. | to_entries | sort_by(.value.stats[0].cpu.usage.total) | reverse | .[0:5]'
cAdvisor 是容器监控的基石,结合 Prometheus 和 Grafana 可以构建完整的容器可观测性体系。
© 版权声明
文章版权归作者所有,未经允许请勿转载。
THE END


暂无评论内容