CodeGym /课程 /Docker SELF /使用 Prometheus 设置容器监控

使用 Prometheus 设置容器监控

Docker SELF
第 21 级 , 课程 3
可用

4.1 安装 Prometheus

Prometheus 是一个强大的开源监控和告警系统,专为从各种来源(包括 Docker 容器)收集和存储指标而设计。在这个课程中,我们将详细讲解如何使用 Prometheus 配置容器的监控,包括安装、指标收集的配置以及与 Docker 的集成。

1. 下载并安装 Prometheus

首先,从官方网站下载 Prometheus 的最新版本:

Terminal

wget https://github.com/prometheus/prometheus/releases/download/v2.30.3/prometheus-2.30.3.linux-amd64.tar.gz

解压文件:

Terminal

tar xvfz prometheus-*.tar.gz
cd prometheus-*      

将二进制文件移动到 /usr/local/bin:

Terminal

sudo mv prometheus /usr/local/bin/
sudo mv promtool /usr/local/bin/

4.2 配置 Prometheus 的配置文件

Prometheus 使用配置文件 prometheus.yml 来定义指标来源和数据收集设置。创建或编辑文件 prometheus.yml

Yaml

global:
  scrape_interval: 15s  # 指标收集间隔
      
scrape_configs:
  - job_name: 'prometheus'
    static_configs:
      - targets: ['localhost:9090']
      
  - job_name: 'cadvisor'
    static_configs:
      - targets: ['localhost:8080']

这个配置文件包含两个任务 (job):

  1. prometheus: 用于从 Prometheus 本身收集指标。
  2. cadvisor: 用于从 cAdvisor 收集指标,cAdvisor 将被用于监控 Docker 容器。

5.3 启动 Prometheus

使用已创建的配置文件启动 Prometheus:

Terminal

prometheus --config.file=prometheus.yml

Prometheus 可通过地址 http://localhost:9090 访问。

5.4 安装和配置 cAdvisor

cAdvisor (Container Advisor) 是一个用来监控容器的工具,它会收集并提供 Docker 容器的指标数据。使用它请完成以下步骤:

在 Docker 容器中运行 cAdvisor

Terminal

docker run -d \
  --name=cadvisor \
  --volume=/:/rootfs:ro \
  --volume=/var/run:/var/run:ro \
  --volume=/sys:/sys:ro \
  --volume=/var/lib/docker/:/var/lib/docker:ro \
  --publish=8080:8080 \
  --privileged \
  gcr.io/cadvisor/cadvisor:latest

cAdvisor 可以通过地址访问 http://localhost:8080.

5.5 为cAdvisor更新Prometheus配置

确保Prometheus的配置中包含对cAdvisor的任务配置。示例配置如上所示。

重新启动Prometheus以应用更改:

Terminal

prometheus --config.file=prometheus.yml

5.6 检查指标收集

在浏览器中打开 Prometheus 的 web 界面,地址为 http://localhost:9090,确认 Prometheus 正在从 cAdvisor 收集指标。为此,在 Prometheus 中执行以下查询:

Terminal

container_cpu_usage_seconds_total

你应该会看到从 Docker 容器收集的数据。

5.7 添加额外的指标

你可以在 Prometheus 的配置文件里添加额外的指标以便收集更多关于容器的数据。以下是一些额外指标的例子:

Yaml

  - job_name: 'node'
    static_configs:
      - targets: ['localhost:9100']

为此,你需要安装 node_exporter,它提供关于主机系统状态的指标。

下载并安装 node_exporter:

Terminal

wget https://github.com/prometheus/node_exporter/releases/download/v1.2.2/node_exporter-1.2.2.linux-amd64.tar.gz
tar xvfz node_exporter-*.tar.gz
sudo mv node_exporter-1.2.2.linux-amd64/node_exporter /usr/local/bin/

启动 node_exporter:

Terminal

node_exporter &

现在 node_exporter 可以通过 http://localhost:9100 访问。

5.8 创建警报规则

Prometheus 支持创建警报规则以通知管理员问题。将警报规则添加到配置文件 prometheus.yml:

Yaml

rule_files:
  - "alert.rules"
      
alerting:
  alertmanagers:
  - static_configs:
    - targets:
      - "localhost:9093"

创建文件 alert.rules,并添加示例规则:

Yaml

groups:
- name: example
  rules:
  - alert: HighCPUUsage
    expr: container_cpu_usage_seconds_total{job="cadvisor"} > 0.85
    for: 1m
    labels:
      severity: warning
    annotations:
      summary: "检测到高 CPU 使用率"
      description: "容器 CPU 使用率超过 85%,持续时间超过 1 分钟。"
评论
TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULL VERSION