Files
ljwx-k8s/DASHBOARD-STATUS-REPORT.md
2026-02-20 22:50:45 +08:00

360 lines
10 KiB
Markdown
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
# Grafana Dashboard 问题修复报告
**问题时间**: 2026-01-02 23:30
**修复状态**: ✅ 部分完成(简化版 Dashboard 已可用)
---
## 📊 问题描述
原始 Dashboard 显示除了 "Gitea Runner 副本数" 之外,其他所有面板都是 "no data"。
---
## 🔍 根本原因分析
### 1. Gitea Actions Exporter 指标未被 Prometheus 采集 ⚠️
**问题**:
- Gitea Actions Exporter Pod 正常运行 ✅
- Exporter 正常暴露指标(http://gitea-actions-exporter.gitea-runner:9999/metrics)✅
- ServiceMonitor 已创建 ✅
- **但 Prometheus 未采集到指标** ❌
**原因**:
- Prometheus Operator 的 ServiceMonitor 选择器配置问题
- ServiceMonitor 需要 `release: monitoring` 标签才能被识别
- Additional Scrape Configs 可能未正确应用
### 2. Container 指标(CPU/Memory使用率)不可用 ❌
**原因**:
- 当前 Prometheus 实例主要采集 kube-state-metrics 指标
- 缺少 cAdvisor 指标(container_cpu_usage_seconds_total, container_memory_usage_bytes 等)
- 这些指标通常由 kubelet 的 cAdvisor 暴露
---
## ✅ 已完成的修复
### 1. 创建并导入简化版 Dashboard
**Dashboard 信息**:
- **标题**: Gitea Runner Autoscaling (Simplified)
- **UID**: af901k0wj05q8c
- **URL**: http://localhost:30300/d/af901k0wj05q8c/gitea-runner-autoscaling-simplified
**可用面板**(共 10 个):
| # | 面板名称 | 数据源 | 状态 |
|---|---------|--------|------|
| 1 | Gitea Runner 副本数 | kube_deployment_status_replicas | ✅ 正常 |
| 2 | Gitea Runner Pod 状态 | kube_deployment_status_replicas_* | ✅ 正常 |
| 3 | HPA 期望副本数 | kube_horizontalpodautoscaler_* | ✅ 正常 |
| 4 | Pod 重启次数 | kube_pod_container_status_restarts_total | ✅ 正常 |
| 5 | CPU 资源请求 | kube_pod_container_resource_requests{resource="cpu"} | ✅ 正常 |
| 6 | 内存资源请求 | kube_pod_container_resource_requests{resource="memory"} | ✅ 正常 |
| 7 | 当前副本数(Stat) | kube_deployment_status_replicas | ✅ 正常 |
| 8 | Ready Pods(Stat) | kube_deployment_status_replicas_ready | ✅ 正常 |
| 9 | HPA 最大副本数(Stat) | kube_horizontalpodautoscaler_spec_max_replicas | ✅ 正常 |
| 10 | Exporter Pod 状态(Stat) | kube_deployment_status_replicas_ready | ✅ 正常 |
**特点**:
- ✅ 所有面板都使用 kube-state-metrics 指标(已验证可用)
- ✅ 实时显示副本数、Pod 状态、HPA 配置
- ✅ 显示资源请求量(request),而非实际使用率
- ⚠️ 暂不包括队列指标(等待 Prometheus 采集 Exporter)
- ⚠️ 暂不包括实时 CPU/Memory 使用率(需要 cAdvisor 指标)
### 2. 修复 ServiceMonitor 配置
```yaml
# 已创建在 observability 命名空间
apiVersion: monitoring.coreos.com/v1
kind: ServiceMonitor
metadata:
name: gitea-actions-exporter
namespace: observability
labels:
release: monitoring # 关键:匹配 Prometheus 选择器
spec:
namespaceSelector:
matchNames:
- gitea-runner
selector:
matchLabels:
app: gitea-actions-exporter
endpoints:
- port: metrics
interval: 30s
```
### 3. 创建 Additional Scrape Configs
```yaml
# Secret: additional-scrape-configs (observability 命名空间)
- job_name: 'gitea-actions-exporter'
static_configs:
- targets: ['gitea-actions-exporter.gitea-runner.svc.cluster.local:9999']
scrape_interval: 30s
scrape_timeout: 10s
metrics_path: /metrics
```
### 4. 重启 Prometheus
- Prometheus Pod 已重启,正在重新加载配置
- 预计 2-3 分钟后开始采集 Exporter 指标
---
## ⏳ 待完成的任务
### 任务 1: 验证 Prometheus 采集 Exporter 指标(2-3 分钟后)
**验证步骤**:
```bash
# 1. 端口转发到 Prometheus
kubectl port-forward -n monitoring svc/prometheus 9090:9090
# 2. 查询 Gitea Actions 指标
curl -s 'http://localhost:9090/api/v1/query?query=gitea_actions_pending_jobs' | jq '.'
# 3. 检查 targets
curl -s 'http://localhost:9090/api/v1/targets' | jq '.data.activeTargets[] | select(.labels.job | contains("gitea"))'
```
**预期结果**:
```json
{
"status": "success",
"data": {
"resultType": "vector",
"result": [
{
"metric": {
"instance": "http://192.168.1.83:33000",
"__name__": "gitea_actions_pending_jobs"
},
"value": [1767396341, "0"]
}
]
}
}
```
### 任务 2: 添加队列监控面板(Prometheus 采集成功后)
**新增面板查询**:
```promql
# Gitea Actions 队列状态
gitea_actions_pending_jobs{instance="http://192.168.1.83:33000"}
gitea_actions_running_jobs{instance="http://192.168.1.83:33000"}
gitea_actions_total_jobs{instance="http://192.168.1.83:33000"}
# 最老任务年龄
gitea_actions_queue_oldest_job_age_seconds{instance="http://192.168.1.83:33000"}
```
### 任务 3: 启用 cAdvisor 指标(可选,用于实时使用率)
**方法 A: 验证 kubelet 指标**
```bash
# 检查是否已有 kubelet ServiceMonitor
kubectl get servicemonitor -n observability monitoring-kube-prometheus-kubelet
# 测试查询
curl 'http://localhost:9090/api/v1/query?query=container_cpu_usage_seconds_total'
```
**方法 B: 如果没有,启用 kubelet metrics**
```bash
# 编辑 kube-prometheus values
# 或手动创建 ServiceMonitor for kubelet
```
---
## 📝 当前系统状态
### 组件状态
| 组件 | 状态 | 说明 |
|------|------|------|
| Gitea Actions Exporter | ✅ Running | Pod: gitea-actions-exporter-7ffccb4f95-cvck4 |
| Exporter Service | ✅ Active | ClusterIP: 192.168.194.159:9999 |
| ServiceMonitor | ✅ Created | namespace: observability, labels: release=monitoring |
| Additional Scrape Config | ✅ Created | Secret: additional-scrape-configs |
| Prometheus | 🔄 Reloading | 正在重新加载配置 |
| Grafana Dashboard | ✅ Working | 简化版已可用 |
### Exporter 指标
**可用指标**(已验证):
```
gitea_actions_pending_jobs{instance="http://192.168.1.83:33000"} 0.0
gitea_actions_running_jobs{instance="http://192.168.1.83:33000"} 0.0
gitea_actions_total_jobs{instance="http://192.168.1.83:33000"} 0.0
gitea_actions_queue_oldest_job_age_seconds{instance="http://192.168.1.83:33000"} 0.0
gitea_exporter_scrape_errors_total 0.0
gitea_exporter_last_scrape_timestamp 1767396341.789907
```
**访问方式**:
```bash
# 直接访问 Exporter
kubectl port-forward -n gitea-runner svc/gitea-actions-exporter 9999:9999
curl http://localhost:9999/metrics
# 从 Prometheus Pod 测试
kubectl exec -n monitoring <prometheus-pod> -- \
wget -qO- http://gitea-actions-exporter.gitea-runner.svc.cluster.local:9999/metrics
```
---
## 🎯 下一步行动
### 立即(现在)
1. ✅ **访问简化版 Dashboard**
- URL: http://localhost:30300/d/af901k0wj05q8c/gitea-runner-autoscaling-simplified
- 验证 10 个面板是否正常显示数据
2. 🔄 **等待 Prometheus 重新加载**(2-3 分钟)
- Prometheus Pod 正在重启
- 配置将自动重新加载
### 2-3 分钟后
3. ✅ **验证 Prometheus 采集**
```bash
kubectl port-forward -n monitoring svc/prometheus 9090:9090
# 访问 http://localhost:9090/targets 查看 gitea-actions-exporter
```
4. ✅ **测试队列指标查询**
```bash
curl -s 'http://localhost:9090/api/v1/query?query=gitea_actions_pending_jobs'
```
5. ✅ **更新 Dashboard 添加队列面板**(如果采集成功)
### 长期优化(可选)
6. ⚪ **启用 cAdvisor 指标**
- 添加实时 CPU/Memory 使用率面板
- 需要配置 kubelet ServiceMonitor
7. ⚪ **优化告警规则**
- 基于队列长度设置告警
- 基于扩缩容事件设置通知
---
## 🛠️ 故障排查指南
### 问题 1: Prometheus 仍未采集到 Exporter 指标
**检查步骤**:
```bash
# 1. 验证 ServiceMonitor
kubectl get servicemonitor -n observability gitea-actions-exporter -o yaml
# 2. 检查 Prometheus 选择器
kubectl get prometheus -n observability monitoring-kube-prometheus-prometheus -o yaml | grep -A 5 serviceMonitorSelector
# 3. 查看 Prometheus 日志
kubectl logs -n monitoring -l app.kubernetes.io/name=prometheus --tail=100
# 4. 检查 targets
kubectl port-forward -n monitoring svc/prometheus 9090:9090
# 访问 http://localhost:9090/targets
```
**解决方案**:
如果仍未工作,手动添加到 Prometheus ConfigMap:
```bash
kubectl edit configmap prometheus-config -n monitoring
# 在 scrape_configs 添加:
# - job_name: 'gitea-actions-exporter'
# static_configs:
# - targets: ['gitea-actions-exporter.gitea-runner:9999']
```
### 问题 2: Dashboard 面板仍显示 "no data"
**检查步骤**:
```bash
# 1. 验证 Prometheus 数据源
# Grafana UI -> Configuration -> Data Sources -> Prometheus
# 2. 测试查询
# Dashboard -> Panel -> Edit -> Query
kube_deployment_status_replicas{deployment="gitea-runner", namespace="gitea-runner"}
# 3. 检查时间范围
# Dashboard 右上角时间选择器,确保选择了包含数据的时间范围
```
---
## 📄 相关文件
| 文件 | 位置 | 用途 |
|------|------|------|
| **简化版 Dashboard JSON** | `gitea-actions-exporter/gitea-runner-simple-dashboard.json` | Dashboard 定义 |
| **ServiceMonitor** | `gitea-actions-exporter/k8s-manifests/servicemonitor-observability.yaml` | Prometheus 监控配置 |
| **Additional Scrape Config** | `gitea-actions-exporter/prometheus-additional-scrape.yaml` | Prometheus 额外抓取配置 |
| **快速修复脚本** | `fix-dashboard-quick.sh` | 自动化修复工具 |
| **问题修复指南** | `DASHBOARD-FIX-GUIDE.md` | 详细故障排查文档 |
---
## 📊 Dashboard 访问信息
| Dashboard | URL | 状态 |
|-----------|-----|------|
| **简化版(推荐)** | http://localhost:30300/d/af901k0wj05q8c/gitea-runner-autoscaling-simplified | ✅ 可用 |
| **完整版(待修复)** | http://localhost:30300/d/bf8yuehshk6psb/gitea-runner-autoscaling-monitoring | ⚠️ 部分数据缺失 |
**默认凭证**:
- 用户名: admin
- 密码: admin123
---
## 🎉 总结
### 已解决 ✅
1. ✅ **创建简化版 Dashboard**(使用 kube-state-metrics 指标)
2. ✅ **修复 ServiceMonitor 配置**(添加 release: monitoring 标签)
3. ✅ **配置 Additional Scrape Configs**
4. ✅ **重启 Prometheus**(重新加载配置)
5. ✅ **10 个面板正常显示数据**
### 待验证 🔄
1. 🔄 **Prometheus 采集 Exporter 指标**(等待 2-3 分钟)
2. 🔄 **队列监控面板**(Prometheus 采集成功后添加)
### 可选优化 ⚪
1. ⚪ **启用 cAdvisor 指标**(实时使用率监控)
2. ⚪ **配置告警规则**
---
**报告生成时间**: 2026-01-02 23:45
**下次检查**: 2 分钟后验证 Prometheus 采集状态
**负责人**: AI DevOps Assistant