Prometheus 和 Grafana
一份用 Prometheus 和 Grafana 监控 Kept 的分步指南。指标本身、健康检查、日志和链路追踪见 监控。
1. 开启 /metrics
Section titled “1. 开启 /metrics”在终端中生成一个令牌:
openssl rand -hex 32把这个值粘贴到 compose.yaml 旁边的 .env 中:
KEPT_METRICS_TOKEN=<the value>.env 按原样读取,所以在里面写 $(openssl …) 会被当作字面文本。然后应用设置:
docker compose up -dCompose 会把这个变量传给 Kept。没有它,/metrics 就不存在。检查它是否有响应:
curl -H "Authorization: Bearer <the value>" http://localhost:8080/metrics在 Kubernetes 上,把 KEPT_METRICS_TOKEN 放进一个 Secret,并在 chart 的
metrics.tokenSecret 中指定它(Kubernetes)。
2. 抓取指标
Section titled “2. 抓取指标”把令牌保存到 Prometheus 能读取的文件中,例如 /etc/prometheus/kept-token,然后添加一个任务:
scrape_configs: - job_name: kept metrics_path: /metrics authorization: type: Bearer credentials_file: /etc/prometheus/kept-token static_configs: - targets: ['kept:8080']kept:8080 是 Compose 网络中 Kept 的服务;从其他地方访问时,请使用 Prometheus 访问 Kept 的地址和端口。重新加载 Prometheus:它的 targets 页面应显示 kept 为 up。
3. 要关注什么
Section titled “3. 要关注什么”| 指标 | 关注 |
|---|---|
kept_process_uptime_seconds |
进程重启时会回落到一个很小的数 |
kept_process_resident_memory_bytes |
逐渐逼近容器的内存上限 |
kept_db_pool_connections{state="waiting"} |
持续一分钟以上大于 0:有请求在等待数据库 |
kept_backup_last_success_timestamp_seconds |
超过 36 小时(0 表示还没有成功的备份) |
kept_backup_last_run_status{status="failed"} |
为 1:上一次备份失败 |
kept_disk_used_ratio{volume="data"} |
高于 0.85 |
kept_update_available |
为 1:有更新的版本。除非开启了更新检查,否则始终为 0(升级) |
备份和磁盘的数值来自每小时一次的检查,所以最多可能滞后一小时。
4. Grafana
Section titled “4. Grafana”一个仪表板,每行一个面板:
- Stat:
kept_build_info,按其version标签显示。 - Time series:
kept_process_uptime_seconds和kept_process_resident_memory_bytes。 - Time series:
kept_db_pool_connections,按pool和state分组。 - Stat:
time() - kept_backup_last_success_timestamp_seconds,即备份的年龄(秒)。 - Stat:
kept_backup_last_run_status,按status分组。 - Gauge:
kept_disk_used_ratio{volume="data"},范围 0 到 1。
groups: - name: kept rules: - alert: KeptDown expr: up{job="kept"} == 0 for: 2m labels: severity: critical annotations: summary: Kept is down
- alert: KeptBackupStale expr: >- kept_backup_last_success_timestamp_seconds > 0 and time() - kept_backup_last_success_timestamp_seconds > 36 * 3600 for: 5m labels: severity: warning annotations: summary: Kept's last good backup is more than 36 hours old
- alert: KeptBackupFailed expr: kept_backup_last_run_status{status="failed"} == 1 labels: severity: critical annotations: summary: Kept's last backup failed
- alert: KeptDiskFilling expr: kept_disk_used_ratio{volume="data"} > 0.85 for: 5m labels: severity: warning annotations: summary: Kept's data disk is over 85% full
- alert: KeptUpdateAvailable expr: kept_update_available == 1 for: 1h labels: severity: info annotations: summary: A new Kept release is available36 小时和 85% 与 Kept 自己的管理员告警一致(监视备份)。> 0 让备份过期规则在还没做过第一次备份的 Kept 上保持安静:在那之前该指标为 0。