## 新增K8s配置文件 **Prometheus Operator集成**: - servicemonitor.yml: 自动发现ljwx-boot服务,30秒采集间隔 - prometheusrule.yml: 18条告警规则,覆盖P0-3/P0-4所有场景 - grafana-dashboard-configmap.yml: Dashboard导入配置 **应用配置**: - configmap.yml: 添加P0-3 Redis去重和P0-4缓存失效配置 - application.yml: 更新P0功能配置和Actuator暴露设置 **部署工具**: - deploy-p0.sh: 一键部署脚本,支持dry-run和健康检查 - P0-K8S-DEPLOYMENT.md: 完整K8s部署指南(18KB) ## Docker Compose开发环境 **混合模式配置**: - docker-compose-p0-dev.yml: 基础设施Docker化(MySQL/Redis/Prometheus/Grafana) - prometheus-p0-dev.yml: 使用host.docker.internal访问本地ljwx-boot - .env.p0: 环境变量配置模板 - verify-p0-dev.sh: 开发环境验证脚本 **完整模式配置**: - docker-compose-p0.yml: 包含ljwx-boot在内的完整Docker部署 - prometheus-p0.yml: Docker网络内部服务发现 ## 文档 **快速开始指南**: - K8S-P0-QUICK-START.md: K8s部署5分钟快速指南 - P0-DEV-QUICK-START.md: 本地开发环境快速启动指南 **监控Dashboard**: - grafana-dashboard-p0.json: 修复Dashboard JSON格式问题 ## 核心功能 ✅ **P0-3 Redis去重**: 分布式去重,24小时TTL ✅ **P0-4 缓存失效**: 事件驱动自动失效,1秒延迟 ✅ **完整监控**: 14个Grafana面板,18条告警规则 ✅ **多环境支持**: 本地开发、Docker Compose、K8s生产环境 ## 告警规则(6组18条) 1. 服务可用性 (2条): 服务不可用、高错误率 2. P0-3 Redis去重 (3条): Redis连接失败、内存告警、Key激增 3. P0-4 缓存失效 (3条): 功能禁用、失败率高、响应慢 4. JVM资源 (2条): 内存不足、GC时间长 5. 数据库连接池 (2条): 连接池耗尽、无空闲连接 6. 应用性能 (2条): API响应慢、并发数高 7. Pod资源 (3条): CPU/内存高、频繁重启 ## 部署方式 **K8s生产环境**: ```bash cd ljwx-boot/k8s ./deploy-p0.sh ``` **本地开发环境**: ```bash docker-compose -f docker-compose-p0-dev.yml up -d ./ljwx-boot/run-local.sh ``` 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
9.2 KiB
9.2 KiB
K8s P0监控 - 快速开始指南
目标: 将ljwx-health业务系统部署到K8s的ljwx-health namespace,集成现有monitoring组件
适用场景: 生产环境、测试环境
完成时间: 约10-15分钟
快速部署
方式1: 使用自动化脚本(推荐)
cd /Users/brunogao/work/codes/ljwx/ljwx-health/ljwx-boot/k8s
# 1. 预览部署(DRY-RUN)
./deploy-p0.sh --dry-run
# 2. 实际部署
./deploy-p0.sh
# 3. 部署到其他namespace
./deploy-p0.sh --namespace=production
方式2: 手动部署
cd /Users/brunogao/work/codes/ljwx/ljwx-health/ljwx-boot/k8s
# Step 1: 创建基础资源
kubectl apply -f secret.yml -n ljwx-health
kubectl apply -f configmap.yml -n ljwx-health
kubectl apply -f service.yml -n ljwx-health
# Step 2: 部署应用
kubectl apply -f deployment.yml -n ljwx-health
# Step 3: 部署监控组件
kubectl apply -f servicemonitor.yml -n ljwx-health
kubectl apply -f prometheusrule.yml -n ljwx-health
kubectl apply -f grafana-dashboard-configmap.yml -n ljwx-health
# Step 4: 等待Pod就绪
kubectl wait --for=condition=ready pod -l app=ljwx-boot -n ljwx-health --timeout=300s
验证部署
1. 检查应用状态
# 查看所有资源
kubectl get all,servicemonitor,prometheusrule -n ljwx-health -l app=ljwx-boot
# 预期输出:
# pod/ljwx-boot-xxxxxxxxx-xxxxx 1/1 Running 0 2m
# pod/ljwx-boot-xxxxxxxxx-yyyyy 1/1 Running 0 2m
# service/ljwx-boot ClusterIP 10.96.xxx.xxx <none> 9998/TCP
# deployment.apps/ljwx-boot 2/2 2 2 5m
# servicemonitor.../ljwx-boot-p0-monitor 30s
# prometheusrule.../ljwx-boot-p0-alerts 18个告警规则
2. 验证健康检查
# 获取Pod名称
POD=$(kubectl get pod -n ljwx-health -l app=ljwx-boot -o jsonpath='{.items[0].metadata.name}')
# 执行健康检查
kubectl exec -n ljwx-health $POD -- curl -sf http://localhost:9998/actuator/health
# 预期返回: {"status":"UP"}
3. 验证P0 Metrics暴露
# 检查P0指标
kubectl exec -n ljwx-health $POD -- \
curl -s http://localhost:9998/actuator/prometheus | \
grep -E "health_cache|health_dedup"
# 预期看到:
# health_cache_invalidation_enabled{...} 1.0
# health_cache_invalidation_total{...} 0.0
# health_dedup_mode{...} 1.0
4. 验证Prometheus采集
# 端口转发到Prometheus
kubectl port-forward -n monitoring svc/prometheus 9090:9090 &
# 访问 http://localhost:9090/targets
# 查找 ljwx-boot target,状态应为 UP
# 查询P0指标
# 在Prometheus UI执行: health_cache_invalidation_enabled
Grafana Dashboard配置
快速导入Dashboard
# 1. 端口转发到Grafana
kubectl port-forward -n monitoring svc/grafana 3000:3000 &
# 2. 访问 http://localhost:3000
# 登录: admin / <your-password>
# 3. 导入Dashboard
# - Dashboards → + Import
# - Upload JSON file
# - 选择: ljwx-boot/monitoring/grafana-dashboard-p0.json
# - 数据源: Prometheus
# - 点击 Import
# 4. 查看Dashboard
# 应该看到14个面板显示数据:
# ✓ 缓存失效监控 (6个面板)
# ✓ Redis去重监控 (4个面板)
# ✓ JVM和数据库监控 (3个面板)
# ✓ 系统告警 (1个面板)
文件清单
| 文件名 | 大小 | 用途 |
|---|---|---|
| deployment.yml | 5KB | 应用Deployment配置(2副本,滚动更新) |
| service.yml | 1KB | Service配置(ClusterIP + Headless) |
| configmap.yml | 2KB | 应用配置(含P0-3/P0-4配置) |
| secret.yml.template | 1KB | Secret模板(数据库/Redis密码) |
| servicemonitor.yml | 1KB | Prometheus ServiceMonitor配置 |
| prometheusrule.yml | 13KB | Prometheus告警规则(18条规则) |
| grafana-dashboard-configmap.yml | 1KB | Grafana Dashboard导入说明 |
| ingress.yml | 2KB | Ingress配置(可选) |
| deploy-p0.sh | 7KB | 一键部署脚本 |
| P0-K8S-DEPLOYMENT.md | 40KB | 完整部署文档 |
关键配置说明
P0-3 Redis去重配置
在configmap.yml中配置:
health:
dedup:
use-redis: true # 使用Redis去重
ttl-hours: 24 # Key TTL 24小时
use-content-hash: true # 启用内容哈希
P0-4 缓存失效配置
在configmap.yml中配置:
health:
cache:
auto-invalidation: true # 启用自动失效
invalidation-delay-ms: 1000 # 延迟1秒
Prometheus配置
servicemonitor.yml中的关键配置:
spec:
endpoints:
- port: http
path: /actuator/prometheus
interval: 30s # 30秒采集一次
scrapeTimeout: 10s # 10秒超时
告警规则
已配置18条告警规则,分为6个组:
- 服务可用性 (2条): 服务不可用、高错误率
- P0-3 Redis去重 (3条): Redis连接失败、内存告警、Key激增
- P0-4 缓存失效 (3条): 功能禁用、失败率高、响应慢
- JVM资源 (2条): 内存不足、GC时间长
- 数据库连接池 (2条): 连接池耗尽、无空闲连接
- 应用性能 (2条): API响应慢、并发数高
- Pod资源 (3条): CPU/内存高、频繁重启
常见问题
Q1: Pod启动失败 (CrashLoopBackOff)
解决方案:
# 查看日志
kubectl logs -n ljwx-health -l app=ljwx-boot --tail=100
# 检查ConfigMap配置
kubectl get configmap ljwx-boot-config -n ljwx-health -o yaml
# 验证依赖服务
kubectl get svc -n ljwx-health | grep -E "mysql|redis"
Q2: Prometheus无法采集Metrics
解决方案:
# 检查ServiceMonitor
kubectl describe servicemonitor ljwx-boot-p0-monitor -n ljwx-health
# 检查Prometheus Targets
kubectl port-forward -n monitoring svc/prometheus 9090:9090
# 访问: http://localhost:9090/targets
# 检查metrics端点
kubectl exec -n ljwx-health $POD -- curl http://localhost:9998/actuator/prometheus
Q3: Dashboard显示"No Data"
解决方案:
# 1. 确认Prometheus在采集数据
# Prometheus UI执行查询: health_cache_invalidation_total
# 2. 检查Grafana数据源
# Grafana → Configuration → Data Sources → Prometheus
# 3. 验证P0代码激活
kubectl exec -n ljwx-health $POD -- \
curl -s http://localhost:9998/actuator/prometheus | grep health_cache
运维命令
查看日志
# 所有Pod日志
kubectl logs -f -n ljwx-health -l app=ljwx-boot
# 特定Pod日志
kubectl logs -f -n ljwx-health ljwx-boot-xxx-yyy
扩缩容
# 扩容到3副本
kubectl scale deployment ljwx-boot -n ljwx-health --replicas=3
# 验证
kubectl get pods -n ljwx-health -l app=ljwx-boot
滚动更新
# 更新镜像
kubectl set image deployment/ljwx-boot \
ljwx-boot=harbor.omniverseai.net/ljwx-health/ljwx-boot:v2.0 \
-n ljwx-health
# 查看更新状态
kubectl rollout status deployment/ljwx-boot -n ljwx-health
# 回滚
kubectl rollout undo deployment/ljwx-boot -n ljwx-health
更新配置
# 编辑ConfigMap
kubectl edit configmap ljwx-boot-config -n ljwx-health
# 重启Pod使配置生效
kubectl rollout restart deployment ljwx-boot -n ljwx-health
删除部署
# 只删除监控组件
kubectl delete servicemonitor,prometheusrule -n ljwx-health -l app=ljwx-boot
# 删除整个应用
kubectl delete deployment,service,configmap,secret -n ljwx-health -l app=ljwx-boot
监控指标速查
P0核心指标
| 指标 | 说明 | 告警阈值 |
|---|---|---|
health_dedup_mode |
Redis去重模式(1=Redis, 0=内存) | = 0 (critical) |
health_cache_invalidation_enabled |
缓存失效启用状态 | = 0 (warning) |
redis_db_keys{db="database1"} |
Redis去重Key数量 | 增速>1000/min (warning) |
health_cache_invalidation_total |
缓存失效次数 | 失败率>5% (warning) |
性能指标
| 指标 | 说明 | SLA目标 |
|---|---|---|
| P50响应时间 | 50%请求响应时间 | < 50ms |
| P95响应时间 | 95%请求响应时间 | < 100ms |
| P99响应时间 | 99%请求响应时间 | < 200ms |
| QPS | 每秒请求数 | ≥ 1000 |
| 成功率 | 请求成功率 | ≥ 99% |
相关文档
- P0-K8S-DEPLOYMENT.md - 完整K8s部署指南(40KB)
- P0-DEV-QUICK-START.md - 本地开发环境快速启动
- P0-3实施报告 - Redis去重详细设计
- P0-4实施报告 - 缓存失效详细设计
架构对比
Docker Compose环境 vs K8s环境
| 维度 | Docker Compose | Kubernetes |
|---|---|---|
| 用途 | 本地开发、快速验证 | 生产环境、高可用 |
| 服务发现 | host.docker.internal | Service DNS |
| 高可用 | 单实例 | 多副本(2+) |
| 自动扩缩容 | ❌ | ✅ HPA |
| 滚动更新 | ❌ | ✅ RollingUpdate |
| 监控集成 | 手动配置 | ServiceMonitor自动发现 |
| 配置管理 | .env文件 | ConfigMap + Secret |
| 日志收集 | docker logs | EFK/Loki |
最后更新: 2025-12-15 文档版本: v1.0
下一步: 运行 ./ljwx-boot/k8s/deploy-p0.sh 开始部署!