SuperAPI AI 工具集

ln-774-healthcheck-setup

部署与运维

查看项目

更新于 10-15 12:53

技能介绍

用途

ln-774-healthcheck-setup 是一个 L3 Worker 技能,属于 7XX Project Bootstrap 类别,用于为 Kubernetes 探针和监控配置健康检查端点。当需要部署到 Kubernetes 时使用。

主要能力

该技能接收来自 ln-770 的 Context Store,扫描项目依赖并生成健康检查端点与 Kubernetes 探针配置。

  • 输入:来自 ln-770 的 Context Store
  • 输出:健康检查端点与 Kubernetes 探针配置
  • 支持技术栈:.NET(AspNetCore.Diagnostics.HealthChecks)、Python(FastAPI 路由)

阶段一:接收上下文并识别依赖

接受 Context Store 并扫描需要监控的依赖。

必需上下文:

  • STACK:.NET 或 Python
  • PROJECT_ROOT:项目目录路径

幂等性检查:

  • .NET:Grep 查找 AddHealthChecks 或 MapHealthChecks
  • Python:Grep 查找 /health 路由
  • 若找到:返回 { "status": "skipped" }

依赖检测:

  • PostgreSQL:.NET 检测 csproj 中的 Npgsql;Python 检测 requirements 中的 psycopg2 或 asyncpg
  • MySQL:.NET 检测 csproj 中的 MySql.Data;Python 检测 requirements 中的 mysql-connector-python
  • Redis:.NET 检测 csproj 中的 StackExchange.Redis;Python 检测 requirements 中的 redis
  • RabbitMQ:.NET 检测 csproj 中的 RabbitMQ.Client;Python 检测 requirements 中的 pika 或 aio-pika
  • MongoDB:.NET 检测 csproj 中的 MongoDB.Driver;Python 检测 requirements 中的 pymongo

阶段二:设计健康检查策略

按 Kubernetes 最佳实践定义三类健康端点。

端点类型:

  • /health/live:存活探针,判断应用是否存活,仅检查应用响应(不检查依赖)
  • /health/ready:就绪探针,判断应用能否提供流量,检查所有依赖是否健康
  • /health/startup:启动探针(K8s 1.16+),判断应用是否初始化完成,检查初始预热是否完成

各探针失败时的行为:

  • 存活探针失败:容器重启,kubelet 重启容器
  • 就绪探针失败:从服务中移除,停止流量,不重启
  • 启动探针失败:延迟其他探针,存活/就绪探针暂停

阶段三:研究健康检查模式

使用 MCP 工具获取最新文档。

.NET:

  • MCP ref:"ASP.NET Core health checks Kubernetes probes"
  • Context7:/dotnet/aspnetcore

Python:

  • MCP ref:"FastAPI health check endpoint Kubernetes"
  • Context7:/tiangolo/fastapi

需研究的关键模式:

  • 数据库健康检查(连接池)
  • Redis 连通性检查
  • 自定义健康检查实现
  • 健康检查响应写入器定制

阶段四:配置 Kubernetes 探针

根据应用特征确定探针时序。

探针配置参数:

  • initialDelaySeconds:存活 10,就绪 5,启动 0
  • periodSeconds:存活 10,就绪 5,启动 5
  • timeoutSeconds:存活 5,就绪 3,启动 3
  • failureThreshold:存活 3,就绪 3,启动 30
  • successThreshold:存活 1,就绪 1,启动 1

启动探针计算:

  • 最大启动时间 = initialDelaySeconds + (periodSeconds × failureThreshold)
  • 默认:0 + (5 × 30) = 150 秒

阶段五:生成实现

.NET 输出文件:

  • Extensions/HealthCheckExtensions.cs:健康检查注册
  • HealthChecks/StartupHealthCheck.cs:自定义启动检查

生成过程:

  • 使用 MCP ref 获取当前 ASP.NET Core 健康检查 API
  • 生成 HealthCheckExtensions,包含:AddHealthChecks 注册、数据库健康检查(若检测到)、Redis 健康检查(若检测到)、自定义 StartupHealthCheck
  • 配置三个端点并设置正确的标签

需添加的包:

  • AspNetCore.HealthChecks.NpgSql(若使用 PostgreSQL)
  • AspNetCore.HealthChecks.Redis(若使用 Redis)
  • AspNetCore.HealthChecks.MySql(若使用 MySQL)

注册代码:

builder.Services.AddHealthCheckServices(builder.Configuration);
// ...
app.MapHealthCheckEndpoints();

Python 输出文件:

  • routes/health.py:健康检查路由
  • services/health_checker.py:依赖健康检查

生成过程:

  • 使用 MCP ref 获取 FastAPI 健康模式
  • 生成健康路由,包含:/health/live 端点(简单)、/health/ready 端点(含依赖检查)、/health/startup 端点
  • 生成 health_checker 服务用于依赖验证

注册代码:

from routes.health import health_router
app.include_router(health_router)

Kubernetes 清单片段(用于包含在 deployment.yaml 中):

livenessProbe:
  httpGet:
    path: /health/live
    port: 5000
  initialDelaySeconds: 10
  periodSeconds: 10
  timeoutSeconds: 5
  failureThreshold: 3

readinessProbe:
  httpGet:
    path: /health/ready
    port: 5000
  initialDelaySeconds: 5
  periodSeconds: 5
  timeoutSeconds: 3
  failureThreshold: 3

startupProbe:
  httpGet:
    path: /health/startup
    port: 5000
  periodSeconds: 5
  failureThreshold: 30

阶段六:验证

验证步骤:

  • 语法检查:.NET 使用 dotnet build --no-restore;Python 使用 python -m py_compile routes/health.py
  • 端点测试:
curl http://localhost:5000/health/live
curl http://localhost:5000/health/ready
curl http://localhost:5000/health/startup
  • 验证响应格式:
{
  "status": "Healthy",
  "checks": {
    "database": { "status": "Healthy", "duration": "00:00:00.0234" },
    "redis": { "status": "Healthy", "duration": "00:00:00.0012" }
  },
  "totalDuration": "00:00:00.0250"
}
  • 依赖失败测试:停止数据库,验证 /health/ready 返回 503,验证 /health/live 仍返回 200

返回给协调器

{
  "status": "success",
  "files_created": [
    "Extensions/HealthCheckExtensions.cs",
    "HealthChecks/StartupHealthCheck.cs"
  ],
  "packages_added": [
    "AspNetCore.HealthChecks.NpgSql"
  ],
  "registration_code": "builder.Services.AddHealthCheckServices(configuration);",
  "message": "Configured health checks with liveness, readiness, and startup probes"
}

关键规则

  • 三个独立端点:/health/live、/health/ready、/health/startup,遵循 Kubernetes 最佳实践
  • 存活探针不得检查依赖:仅确认应用存活(避免级联重启)
  • 就绪探针检查所有依赖:验证 DB、Redis、RabbitMQ 连通性
  • 从项目文件自动检测依赖:扫描 csproj/requirements 中的已知包
  • 幂等:若 AddHealthChecks/MapHealthChecks 或 /health 路由已存在,返回 status: "skipped"

完成定义

  • 已接收 Context Store(stack、project root)
  • 已检测依赖(PostgreSQL、MySQL、Redis、RabbitMQ、MongoDB)
  • 已为检测到的技术栈生成健康检查端点(live、ready、startup)
  • 已生成带正确时序参数的 Kubernetes 探针清单片段
  • 已通过语法验证(dotnet build 或 py_compile)
  • 已向 ln-770 协调器返回结构化 JSON 响应

适用场景

适用于需要部署到 Kubernetes 的 .NET 或 Python 项目,尤其是使用 PostgreSQL、MySQL、Redis、RabbitMQ 或 MongoDB 等依赖服务的应用。

限制

  • 仅支持 .NET 和 Python(FastAPI)两种技术栈
  • 依赖检测仅覆盖 PostgreSQL、MySQL、Redis、RabbitMQ、MongoDB
  • 若项目中已存在健康检查配置,则跳过执行
  • 版本:2.0.0,最后更新:2026-01-10