Prometheus 实战
  • 前言
  • 修订记录
  • 如何贡献
  • Prometheus 简介
    • Prometheus 是什么
    • 为什么选择 Prometheus
  • Prometheus 安装
    • 二进制包安装
    • Docker 安装
  • 基础概念
    • 数据模型
    • 指标类型
    • 作业与实例
  • PromQL
    • PromQL 基本使用
    • 与 SQL 对比
  • 数据可视化
    • Web Console
    • Grafana
    • Promlens
  • Prometheus 配置
    • 全局配置
    • 告警配置
    • 规则配置
    • 数据拉取配置
    • 远程可写存储
    • 远程可读存储
    • 服务发现
    • 配置样例
  • 服务发现
    • 静态服务发现
    • 文件服务发现
    • HTTP服务发现
    • Consul服务发现
    • moby服务发现
    • kubernetes服务发现
  • Exporter
    • 文本格式
    • Sample Exporter
    • Node Exporter 安装使用
    • Node Exporter 常用查询
    • 其他 Exporter 介绍
  • Pushgateway
    • Pushgateway 是什么
    • 如何使用 Pushgateway
  • 数据存储
    • Local Store
    • Remote Store
  • 告警/记录规则
    • 如何配置
    • 触发逻辑
  • Alertmanager
    • Alertmanager 是什么
    • 配置详情
    • 通过 Email 接收告警
    • 通过企业微信接收告警
    • 通过 Slack 接收告警
    • 通过 Webhook 接收告警
    • 其他告警接收方案
  • Prometheus 工具
    • Promtool 介绍和使用
    • Client SDK
  • Prometheus 性能调优
    • Metrics 仪表盘
    • 启动参数优化
    • 日志查询
  • Prometheus 与容器
    • Docker
    • Kubernetes
  • 高可用方案探讨
    • Prometheus Server 的高可靠
    • AlertManager 的高可靠
  • 实战练习
    • NodeExporter
    • 配置告警规则
    • Grafana 集成
    • Alertmanager 告警
  • 常见问题收录
    • 如何热加载新配置
    • 如何通过认证后拉取数据
Powered by GitBook
On this page
  • docker stats 对 cadvisor
  • cadvisor 的安装
  • cadvisor 深入了解
  • cadvisor 与 Prometheus 集成
  • Prometheus 中查看容器的 CPU,内存,网络流量等数据

Was this helpful?

  1. Prometheus 与容器

Docker

想必大家在生产环境中已大量使用到了容器,那对于容器的监控(CPU, 内存,网络请求)是如何处理的呢?

docker stats 对 cadvisor

众所周知 dokcer stats 可以查看运行的 Docker 镜像的运行状态,例如:

CONTAINER ID        NAME                CPU %               MEM USAGE / LIMIT    MEM %               NET I/O             BLOCK I/O           PIDS
a25dd77a5237        cadvisor            0.91%               14.8MiB / 1.952GiB   0.74%               749kB / 11.5MB      18.9MB / 0B         11

这种方式比较原始,因为你无法通过 http 的方式来获取数据,而且没有界面,数据可视化还需要做大量的工作。

由于 dokcer stats 有这些问题,所以 cadvisor 诞生了。cadvisor 不仅可以搜集一台机器上所有运行的容器信息还提供基础查询界面和 http 接口,方便 Prometheus 进行数据抓取。

正是因为 cadvisor 与 Prometheus 的完美结合,所以它成为了容器监控的第一选择。

cadvisor 的安装

Step1: 使用 docker pull 下载最新版本的 cadvisor

$ docker pull google/cadvisor:latest

Step2: 使用 docker images 查看下载的版本

$ docker images  

google/cadvisor      latest              75f88e3ec333        4 months ago        62.2MB

Step3: 使用 docker run 启动

sudo docker run \
  --volume=/:/rootfs:ro \
  --volume=/var/run:/var/run:rw \
  --volume=/sys:/sys:ro \
  --volume=/var/lib/docker/:/var/lib/docker:ro \
  --volume=/dev/disk/:/dev/disk:ro \
  --publish=8080:8080 \
  --detach=true \
  --name=cadvisor \
  google/cadvisor:latest

当启动成功后,使用 docker ps 你会看到

CONTAINER ID        IMAGE                    COMMAND                  CREATED             STATUS              PORTS                    NAMES
742a464fa631        google/cadvisor:latest   "/usr/bin/cadvisor -…"   1 second ago        Up 1 second         0.0.0.0:8080->8080/tcp   cadvisor

Step4: 访问 http://localhost:8080 你将看到:

这说明 cadvisor 已运行成功。

cadvisor 深入了解

Tips1: 访问 http://localhost:8080/docker 可以查看到所有运行的 dokcer 镜像:

Tips2: 选择任意一个镜像,你将看到其运行状态的详细信息:

Tips3: 访问 http://localhost:8080/metrics 可以查看其暴露给 Prometheus 的所有数据:

cadvisor 与 Prometheus 集成

Step1: 修改 Prometheus 配置信息,添加 cadvisor 访问地址:

#prometheus.yml

scrape_configs:
  - job_name: 'node'
    static_configs:
      - targets: ['127.0.0.1:9100']

  - job_name: 'container'
    static_configs:
      - targets: ['127.0.0.1:8080']  # 本地 cadvisor 访问地址

Step2: 重新加载配置,访问 http://localhost:9090/targets 你将看到新加的 cadvisor 已经生效。

Step3: 此时访问 Prometheus 的 graph 页面 http://localhost:9090/graph,搜索 container 你将看到容器相关数据。

Prometheus 中查看容器的 CPU,内存,网络流量等数据

CPU 使用率查询:

sum by (name) (rate(container_cpu_usage_seconds_total{image!=""}[1m])) / scalar(count(node_cpu{mode="user"})) * 100

内存使用量:

sum by (name)(container_memory_usage_bytes{image!=""})

网络入口流量

sum by (name) (rate(container_network_receive_bytes_total{image!=""}[1m]))

网络出口流量

sum by (name) (rate(container_network_transmit_bytes_total{image!=""}[1m]))

磁盘使用量:

sum by (name) (container_fs_usage_bytes{image!=""})
PreviousPrometheus 与容器NextKubernetes

Last updated 4 years ago

Was this helpful?

/images/cadvisor/cadvisor-01.png
/images/cadvisor/cadvisor-02.png
/images/cadvisor/cadvisor-03.png
/images/cadvisor/cadvisor-04.png
/images/cadvisor/cadvisor-05.png
/images/cadvisor/prometheus01.png
/images/cadvisor/prometheus02.png
/images/cadvisor/prometheus03.png
/images/cadvisor/prometheus04.png
/images/cadvisor/prometheus06.png
/images/cadvisor/prometheus05.png
/images/cadvisor/prometheus07.png