如何监控指标¶
Incus 收集所有正在运行的实例以及一些内部指标的指标。这些指标涵盖 CPU、内存、网络、磁盘和进程使用情况。它们旨在由 Prometheus 使用,您可以使用 Grafana 将指标显示为图形。有关可用指标的列表,请参阅 提供的指标。
在集群环境中,Incus 仅返回在正在访问的服务器上运行的实例的值。因此,您必须分别抓取每个集群成员。
调用 /1.0/metrics 端点时,会更新实例指标。为了处理多个抓取程序,它们会被缓存 8 秒。获取指标对于 Incus 来说是一个相对昂贵的操作,因此如果影响过大,请考虑以高于默认间隔的时间抓取。
查询原始数据¶
要查看 Incus 收集的原始数据,请使用 incus query 命令查询 /1.0/metrics 端点
user@host:~$ incus query /1.0/metrics# HELP incus_cpu_seconds_total The total number of CPU time used in seconds.# TYPE incus_cpu_seconds_total counterincus_cpu_seconds_total{cpu="0",mode="system",name="u1",project="default",type="container"} 60.304517incus_cpu_seconds_total{cpu="0",mode="user",name="u1",project="default",type="container"} 145.647502incus_cpu_seconds_total{cpu="0",mode="iowait",name="vm",project="default",type="virtual-machine"} 4614.78incus_cpu_seconds_total{cpu="0",mode="irq",name="vm",project="default",type="virtual-machine"} 0incus_cpu_seconds_total{cpu="0",mode="idle",name="vm",project="default",type="virtual-machine"} 412762incus_cpu_seconds_total{cpu="0",mode="nice",name="vm",project="default",type="virtual-machine"} 35.06incus_cpu_seconds_total{cpu="0",mode="softirq",name="vm",project="default",type="virtual-machine"} 2.41incus_cpu_seconds_total{cpu="0",mode="steal",name="vm",project="default",type="virtual-machine"} 9.84incus_cpu_seconds_total{cpu="0",mode="system",name="vm",project="default",type="virtual-machine"} 340.84incus_cpu_seconds_total{cpu="0",mode="user",name="vm",project="default",type="virtual-machine"} 261.25# HELP incus_cpu_effective_total The total number of effective CPUs.# TYPE incus_cpu_effective_total gaugeincus_cpu_effective_total{name="u1",project="default",type="container"} 4incus_cpu_effective_total{name="vm",project="default",type="virtual-machine"} 0# HELP incus_disk_read_bytes_total The total number of bytes read.# TYPE incus_disk_read_bytes_total counterincus_disk_read_bytes_total{device="loop5",name="u1",project="default",type="container"} 2048incus_disk_read_bytes_total{device="loop3",name="vm",project="default",type="virtual-machine"} 353280...设置 Prometheus¶
为了收集和存储原始指标,您应该设置 Prometheus。然后,您可以将其配置为通过指标 API 端点抓取指标。
公开指标端点¶
要公开 /1.0/metrics API 端点,您必须设置其应可用的地址。
为此,您可以设置 core.metrics_address 服务器配置选项或 core.https_address 服务器配置选项。 core.metrics_address 选项仅用于指标,而 core.https_address 选项公开了完整的 API。因此,如果您希望为指标 API 使用与完整 API 不同的地址,或者如果您只想公开指标端点而不公开完整 API,则应设置 core.metrics_address 选项。
例如,要公开 8443 端口上的完整 API,请输入以下命令
incus config set core.https_address ":8443"
要仅公开 8444 端口上的指标 API 端点,请输入以下命令
incus config set core.metrics_address ":8444"
要仅在特定 IP 地址和端口上公开指标 API 端点,请输入类似于以下命令的命令
incus config set core.metrics_address "192.0.2.101:8444"
为 Incus 添加指标证书¶
对/1.0/metrics API 端点的身份验证是通过指标证书完成的。指标证书(类型为metrics)与客户端证书(类型为client)不同,因为它仅用于指标,不能用于与实例或任何其他 Incus 实体进行交互。
要创建证书,请输入以下命令
openssl req -x509 -newkey ec -pkeyopt ec_paramgen_curve:secp384r1 -sha384 -keyout metrics.key -nodes -out metrics.crt -days 3650 -subj "/CN=metrics.local"
注意
该命令需要 OpenSSL 1.1.0 或更高版本。
然后将此证书添加到受信任客户端列表中,并将类型指定为metrics
incus config trust add-certificate metrics.crt --type=metrics
如果您的环境中无法强制执行 TLS 客户端身份验证,则可以使/1.0/metrics API 端点可供未经身份验证的客户端访问。虽然不建议这样做,但如果您已实施其他控制措施来限制可以访问该 API 端点的用户,则这可能是可以接受的。要禁用指标 API 上的身份验证
# Disable authentication (NOT RECOMMENDED)
incus config set core.metrics_authentication false
使指标证书可用于 Prometheus¶
如果您在与 Incus 服务器不同的机器上运行 Prometheus,则必须将所需的证书复制到 Prometheus 机器上
您创建的指标证书(
metrics.crt)和密钥(metrics.key)位于
/var/lib/incus/中的 Incus 服务器证书(server.crt)
将这些文件复制到 Prometheus 可访问的tls目录中,例如/etc/prometheus/tls。请参阅以下示例命令
# Create tls directory
mkdir /etc/prometheus/tls/
# Copy newly created certificate and key to tls directory
cp metrics.crt metrics.key /etc/prometheus/tls/
# Copy Incus server certificate to tls directory
cp /var/lib/incus/server.crt /etc/prometheus/tls/
# Make the files accessible by prometheus
chown -R prometheus:prometheus /etc/prometheus/tls
配置 Prometheus 从 Incus 提取指标¶
最后,您必须将 Incus 添加为 Prometheus 配置的目标。
为此,请编辑/etc/prometheus/prometheus.yaml并为 Incus 添加一个作业。
配置需要如下所示
global:
# How frequently to scrape targets by default. The Prometheus default value is 1m.
scrape_interval: 15s
scrape_configs:
- job_name: incus
metrics_path: '/1.0/metrics'
scheme: 'https'
static_configs:
- targets: ['foo.example.com:8443']
tls_config:
ca_file: 'tls/server.crt'
cert_file: 'tls/metrics.crt'
key_file: 'tls/metrics.key'
# XXX: server_name is required if the target name
# is not covered by the certificate (not in the SAN list)
server_name: 'foo'
注意
默认情况下,Grafana Prometheus 数据源假设
scrape_interval为 15 秒。如果您决定使用不同的scrape_interval值,则必须在 Prometheus 配置和 Grafana Prometheus 数据源配置中都进行更改。否则,Grafana 的$__rate_interval值将计算不正确,并可能导致使用它的查询出现no data响应。如果 Incus 服务器证书不包含与
targets列表中使用的主机名相同的名称,则必须指定server_name。要验证这一点,请打开server.crt并检查“主题备用名称 (SAN)”部分。
例如,假设server.crt具有以下内容
user@host:~$ openssl x509 -noout -text -in /etc/prometheus/tls/server.crt... X509v3 Subject Alternative Name: DNS:foo, IP Address:127.0.0.1, IP Address:0:0:0:0:0:0:0:1...由于“主题备用名称 (SAN)”列表不包含targets列表(foo.example.com)中提供的主机名,因此您必须使用server_name指令覆盖用于比较的名称。
以下是一个prometheus.yml配置示例,其中使用多个作业来提取多个 Incus 服务器的指标
global:
# How frequently to scrape targets by default. The Prometheus default value is 1m.
scrape_interval: 15s
scrape_configs:
# abydos, langara and orilla are part of a single cluster (called `hdc` here)
# initially bootstrapped by abydos which is why all 3 targets
# share the same `ca_file` and `server_name`. That `ca_file` corresponds
# to the `/var/lib/incus/cluster.crt` file found on every member of
# the Incus cluster.
#
# Note: the `project` param is are provided when not using the `default` project
# or when multiple projects are used.
#
# Note: each member of the cluster only provide metrics for instances it runs locally
# this is why the `incus-hdc` cluster lists 3 targets
- job_name: "incus-hdc"
metrics_path: '/1.0/metrics'
params:
project: ['jdoe']
scheme: 'https'
static_configs:
- targets:
- 'abydos.hosts.example.net:8444'
- 'langara.hosts.example.net:8444'
- 'orilla.hosts.example.net:8444'
tls_config:
ca_file: 'tls/abydos.crt'
cert_file: 'tls/metrics.crt'
key_file: 'tls/metrics.key'
server_name: 'abydos'
# jupiter, mars and saturn are 3 standalone Incus servers.
# Note: only the `default` project is used on them, so it is not specified.
- job_name: "incus-jupiter"
metrics_path: '/1.0/metrics'
scheme: 'https'
static_configs:
- targets: ['jupiter.example.com:9101']
tls_config:
ca_file: 'tls/jupiter.crt'
cert_file: 'tls/metrics.crt'
key_file: 'tls/metrics.key'
server_name: 'jupiter'
- job_name: "incus-mars"
metrics_path: '/1.0/metrics'
scheme: 'https'
static_configs:
- targets: ['mars.example.com:9101']
tls_config:
ca_file: 'tls/mars.crt'
cert_file: 'tls/metrics.crt'
key_file: 'tls/metrics.key'
server_name: 'mars'
- job_name: "incus-saturn"
metrics_path: '/1.0/metrics'
scheme: 'https'
static_configs:
- targets: ['saturn.example.com:9101']
tls_config:
ca_file: 'tls/saturn.crt'
cert_file: 'tls/metrics.crt'
key_file: 'tls/metrics.key'
server_name: 'saturn'
编辑完配置后,重新启动 Prometheus(例如,systemctl restart prometheus)以开始提取指标。
设置 Grafana 仪表盘¶
要可视化指标数据,请设置Grafana。Incus 提供了一个Grafana 仪表盘,该仪表盘配置为显示 Prometheus 提取的 Incus 指标和 Loki 的日志条目。
注意
仪表盘需要 Grafana 8.4 或更高版本。
有关安装和登录的说明,请参阅 Grafana 文档
完成以下步骤以导入Incus 仪表盘
配置 Prometheus 作为数据源
转到配置 > 数据源。
单击添加数据源。

选择Prometheus。

如果在本地运行 Prometheus,则在URL字段中输入
https://:9090/。
保留其他字段的默认配置,然后单击保存并测试。
配置 Loki 作为数据源
转到配置 > 数据源。
单击添加数据源。
选择Loki。
如果在本地运行 Loki,则在URL字段中输入
https://:3100/。保留其他字段的默认配置,然后单击保存并测试。
导入 Incus 仪表盘
转到仪表盘 > 浏览。
单击新建并选择导入。

在通过 grafana.com 导入字段中,输入仪表盘 ID
19727。
单击加载。
在Incus下拉菜单中,选择您配置的 Prometheus 和 Loki 数据源。

单击导入。
您现在应该可以看到 Incus 仪表盘。您可以选择项目并按实例进行筛选。

在页面底部,您可以看到每个实例的数据。

注意
为了使仪表盘的 Loki 部分正常运行,您需要确保instance字段与 Prometheus 作业名称匹配。您可以通过loki.instance配置键更改instance字段。