↓ 跳过正文
  1. 博客/
  2. Grafana/

用 Proxmox VE Exporter 采集集群级指标

··674 字·4 分钟·
小钰大主宰
作者
小钰大主宰
建站工程师
目录
Grafana - 这篇文章属于一个选集。
§ 16: 本文

用于 Prometheus 的 Proxmox VE Exporter 会从集群、节点及其 guest 采集指标。具体来说,它支持 guest(LXC 与 VM)的以下指标:

  • CPU 使用:pve_cpu_usage_ratio 与 pve_cpu_usage_limit。
  • 内存使用:pve_memory_usage_bytes 与 pve_memory_size_bytes(空闲/已用)。
  • 磁盘空间:pve_disk_usage_bytes 与 pve_disk_size_bytes(根镜像的已用/总量)。
  • 网络 I/O:pve_network_transmit_bytes 与 pve_network_receive_bytes(自启动以来的累计值)。
  • 磁盘 I/O:pve_disk_write_bytes 与 pve_disk_read_bytes(自启动以来的累计值)。
  • 状态:pve_up(guest 是否在运行)。
  • 运行时长:pve_uptime_seconds。
  • 元数据:pve_guest_info(名称、节点、类型、标签)。

PVE Exporter 通过 Proxmox VE API 采集数据,和往常一样由我们的 Prometheus 服务器来拉取。因此,这个 exporter 可以放在集群中的任何位置:

  1. 与现有的 Prometheus 服务器放在一起,前提是资源够用。
  2. 放在一个专用的小型 LXC 里。
  3. 放在集群的每个节点上。

为了获得最佳性能,exporter 应当(就网络跳数而言)尽可能地靠近节点。这是因为它为了拿到所需数据,会发出大量请求。

安装
#

我们打算在每个节点上都安装这个 exporter。这样 Prometheus 服务器会联系各节点上的 exporter,而 exporter 通过 localhost 向 PVE API 发请求获取所需指标。这能显著减少网络上的流量并加快速度。

顺带提一句:由于我们的 Prometheus 服务器只能访问 guest 的私有网络、且必须经过 HTTP 代理才能出公网,我们还会给它所在的 LXC 增加一个公网 IP。这样请求经过的网络跳数会进一步减少。

无论你决定把 PVE Exporter 装在哪里,都要确保 Python 版本足够新。写作时 PVE Exporter 要求 Python 3.9+,所以从 Bullseye 起的任何 Debian GNU/Linux 都可以。

我们要执行的步骤如下:

  1. 创建 prometheus 用户。
  2. 为应用创建虚拟环境。
  3. 在虚拟环境中安装 prometheus-pve-exporter。

先确保必要的软件包已安装:

apt-get update
apt-get install --yes ca-certificates curl python3 \
  python3-pip python3-venv ssl-cert

接着创建用户并加入 ssl-cert 组,这样它就能读取 exporter 与 Prometheus 之间加密通信所用的 TLS 证书:

adduser --system --disabled-login --comment "Prometheus daemon" \
        --home /var/lib/prometheus --groups ssl-cert prometheus

现在可以创建虚拟环境了:

python3 -m venv /opt/prometheus-pve-exporter
source /opt/prometheus-pve-exporter/bin/activate

最后,把 prometheus-pve-exporter 装进虚拟环境:

pip install prometheus-pve-exporter
deactivate

为了让 prometheus 用户能正常使用,/opt/prometheus-pve-exporter 目录及其下所有文件和子目录都必须对所有人可读。这通常已经满足,因为 /etc/login.defs 里默认的 UMASK 是 022。可以用下面的命令确认:

grep ^UMASK /etc/login.defs

也可以用 find 命令做进一步检查:

find /opt/prometheus-pve-exporter ! -perm -o=r

如果上面命令没有输出,就可以进入配置环节了。

API 令牌
#

我们需要生成一个用户和令牌,好让 Prometheus PVE Exporter 能请求生成指标所需的数据。最简单的做法是在任意节点的终端里执行:

pveum user add prometheus@pve --comment "Prometheus Monitoring"
pveum user token add prometheus@pve monitoring --privsep=0

这会输出一个令牌。请把它存进密码库(例如 Proton Pass 或 Vaultwarden),后面配置 /etc/prometheus/pve_exporter.yml 时会用到。

键值
full-tokenidprometheus@pve!monitoring
info{"privsep":"0"}
valuexxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx

最后,给该用户授予 PVEAuditor 角色(只读):

pveum acl modify / --user prometheus@pve --role PVEAuditor

配置
#

为了在整个集群中以一致的方式部署 Prometheus exporter,我们仿照它们在 Debian 里的打包方式:在 /etc/default/ 放一个命令行参数文件,在 /etc/prometheus/ 放主配置文件。

命令行
#

先从 /etc/default/prometheus-pve-exporter 这个命令行参数文件开始:

# Environment variables for prometheus-pve-exporter
# See: https://github.com/prometheus-pve/prometheus-pve-exporter
#
# Bind to all interfaces.
# Disable the config collector for our large deployment (100+ guests),
# as it makes one API call per guest.
#
ARGS="--config.file='/etc/prometheus/pve_exporter.yml' \
      --web.listen-address='[::]:9221' \
      --collector.status \
      --collector.version \
      --collector.node \
      --collector.cluster \
      --collector.resources \
      --collector.replication \
      --no-collector.config \
      --server.keyfile='/etc/ssl/private/localdomain.com.key' \
      --server.certfile='/etc/ssl/certs/localdomain.com.crt'"

示例中的 localdomain.com 证书是集群本地域名的通配证书,通过 PowerDNS 在内部管理、由 Let’s Encrypt 签发。请按你的场景调整。

主要选项
#

考虑到这很可能是该节点上第一个与 Prometheus 相关的软件包,需要先创建配置目录:

mkdir --parents --mode=0755 /etc/prometheus

然后创建主配置文件 /etc/prometheus/pve_exporter.yml,内容如下:

# Prometheus PVE Exporter configuration
# See: https://github.com/prometheus-pve/prometheus-pve-exporter
default:
  user: prometheus@pve
  token_name: "monitoring"
  token_value: "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"
  verify_ssl: false

由于 exporter 通过 localhost 访问 API,校验证书的 TLS 链没有意义。

别忘了收紧 /etc/prometheus/pve_exporter.yml 的权限——它包含敏感信息:

chown prometheus:prometheus /etc/prometheus/pve_exporter.yml
chmod 600 /etc/prometheus/pve_exporter.yml

Systemd
#

最后是 systemd 服务文件 /etc/systemd/system/prometheus-pve-exporter.service:

[Unit]
Description=Prometheus exporter for Proxmox VE
Documentation=https://github.com/prometheus-pve/prometheus-pve-exporter
After=network.target

[Service]
Type=simple
User=prometheus
Group=prometheus
EnvironmentFile=/etc/default/prometheus-pve-exporter
ExecStart=/opt/prometheus-pve-exporter/bin/pve_exporter $ARGS

# Security hardening
NoNewPrivileges=true
PrivateTmp=true
ProtectSystem=strict
ProtectHome=true
ReadWritePaths=/var/log

# Restart policy
Restart=on-failure
RestartSec=5s

# Resource limits
LimitNOFILE=8192

[Install]
WantedBy=multi-user.target

启动
#

见证结果的时刻到了,先启用并启动服务:

systemctl daemon-reload
systemctl enable --now prometheus-pve-exporter.service

用下面的命令查看服务状态:

systemctl status prometheus-pve-exporter.service

再用这个命令跟踪服务输出的日志:

journalctl --follow --unit=prometheus-pve-exporter.service

别忘了调整防火墙规则,好让承载 Prometheus 服务器的 LXC 能访问节点上 9221 端口的 PVE Exporter。注意数据中心层面的防火墙规则对所有节点生效。

现在可以在节点终端里测试 exporter:

curl "https://proxmox1.localdomain.com:9221/pve?module=default&cluster=1&node=1"

可用指标列表在配置 Grafana 告警规则时会很有用,建议把它保存下来:

curl "https://proxmox1.localdomain.com:9221/pve?module=default&cluster=1&node=1" > ~/pve_exporter_metrics.txt

查询字符串里 module 参数的值对应主配置文件中使用的配置键,即 default。

网络流量
#

由于 Proxmox VE API 的设计方式,PVE Exporter 为了拿到全部所需信息,需要向它发出相当多的调用。以下是供参考的统计数据。

假设我们有 10 个节点、200 个 guest,抓取间隔设为 30 秒,使用 exporter 默认的 29 秒缓存,且不启用 config 采集器。

任务类型端点 GET /api2/json调用次数采集到的指标
Cluster/cluster/resources1节点、VM、LXC、存储,以及 guest 的指标
Cluster/nodes1节点列表与 HA 状态信息
Node/nodes/{node}/status10节点状态
Node/nodes/{node}/storage10每个节点的存储信息
Node/nodes/{node}/disks/list10物理磁盘信息与 SMART 状态
Node/nodes/{node}/certificates/info10TLS 证书过期信息
Node/nodes/{node}/version10每个节点的 PVE 版本信息
Node/nodes/{node}/replication10VM/LXC 复制状态

合计 62 次调用。

如果启用 config 采集器,假设有 180 个 LXC 和 20 个 VM,则会额外增加:

任务类型端点 GET /api2/json调用次数说明
Guest/nodes/{node}/qemu10列出每个节点上的所有 VM
Guest/nodes/{node}/lxc10列出每个节点上的所有 LXC
Guest/nodes/{node}/qemu/{vmid}/config20单个 VM 的配置
Guest/nodes/{node}/lxc/{ctid}/config180单个 LXC 的配置

合计 220 次调用。

config 采集器会额外提供一个指标:pve_onboot_status,表示每个 VM/LXC 是否被配置为随节点启动自动启动。你需要权衡这份信息对你的场景是否足够重要,值不值得为此向 PVE API 多发 220 个 HTTP 请求。

Prometheus
#

我们会在 Prometheus 服务器的 /etc/prometheus/prometheus.yml 中新增两个抓取配置。第一个采集集群级指标:

scrape_configs:
  - job_name: 'pve-cluster'
    static_configs:
      - targets:
          - proxmox1.localdomain.com
    metrics_path: /pve
    params:
      module: [default]
      cluster: ['1']
      node: ['0']

第二个采集节点级指标:

scrape_configs:
  - job_name: 'pve-nodes'
    file_sd_configs:
      - files:
        - file_sd_configs/pve_exporter.yml
    metrics_path: /pve
    params:
      module: [default]
      cluster: ['0']
      node: ['1']

而 /etc/prometheus/file_sd_configs/pve_exporter.yml 的内容如下:

- targets:
  - 'proxmox1.localdomain.com:9221'
  - 'proxmox2.localdomain.com:9221'
  - 'proxmox3.localdomain.com:9221'
  - 'proxmox4.localdomain.com:9221'
  - 'proxmox5.localdomain.com:9221'
  - 'proxmox6.localdomain.com:9221'
  - 'proxmox7.localdomain.com:9221'
  - 'proxmox8.localdomain.com:9221'
  - 'proxmox9.localdomain.com:9221'
  - 'proxmox10.localdomain.com:9221'
  labels:
    group: 'pve'

如果你不使用复制(replication),可以跳过节点级指标。

需要让 Prometheus 重新加载配置文件才能生效:

systemctl reload prometheus

这套配置会高效地:集群与 guest 指标各抓取一次,节点专属指标每个节点抓取一次——这是大型集群推荐的做法。所有 guest(VM/LXC)的指标都会自动包含在集群级的抓取结果里,因此无需逐个抓取就能拿到全部容器的指标。

Grafana 仪表盘
#

Prometheus PVE Exporter 仓库给出了一个 Proxmox via Prometheus Grafana 仪表盘的链接,可以作为起点。按以下步骤操作:

  1. 访问 Grafana Dashboard 网站。
  2. 用 Download JSON 按钮下载 JSON 格式的仪表盘。
  3. 在你的 Grafana 中进入 Dashboards 菜单。
  4. 用 New > Import 按钮选择刚下载的 JSON 文件。
  5. 按需修改默认名称,指定文件夹并选择 Prometheus 数据源,点 Import。
  6. 加载完成后,可选地点击 Settings 给它加上一些标签。

大功告成。

Grafana - 这篇文章属于一个选集。
§ 16: 本文

相关文章