关于 ln-774-healthcheck-setup
用于项目初始化的技能,可为 .NET 或 Python 项目添加健康检查端点和 Kubernetes 探针配置。它会检测已知依赖,并跳过已有的健康检查设置。
主要功能
- 01配置 .NET 和 Python 健康检查
- 02提供存活、就绪和启动端点
- 03从项目文件检测依赖
- 04配置 Kubernetes 探针
- 05验证语法和端点
适用场景
- 01为新的 .NET 服务添加探针
- 02设置 FastAPI 健康检查路由
- 03分别检查依赖就绪状态和应用存活状态
MCP 相关
该技能使用 MCP 工具查询最新的 ASP.NET Core 和 FastAPI 健康检查文档。
安装
复制下面的命令,在终端中运行。
$
npx skills add https:/ / github.com/ levnikolaevich/ claude-code-skills --skill ln-774-healthcheck-setup安装后在 Claude Code 中输入/ln-774-healthcheck-setup确认已加载。
详细介绍
ln-774-healthcheck-setup
**类型:**L3 Worker **类别:**7XX 项目引导
为 Kubernetes 探针和监控配置健康检查端点。
概述
| 方面 | 详情 |
|---|---|
| 输入 | 来自 ln-770 的 Context Store |
| 输出 | 健康检查端点和 Kubernetes 探针配置 |
| 技术栈 | .NET(AspNetCore.Diagnostics.HealthChecks)、Python(FastAPI 路由) |
阶段 1:接收上下文并识别依赖项
接收 Context Store,并扫描需要监控的依赖项。
必需上下文:
STACK:.NET 或 PythonPROJECT_ROOT:项目目录路径
幂等性检查:
- .NET:搜索
AddHealthChecks或MapHealthChecks - Python:搜索
/health路由 - 如果已找到:返回
{ "status": "skipped" }
依赖项检测:
| 依赖项 | .NET 检测方式 | Python 检测方式 |
|---|---|---|
| PostgreSQL | csproj 中的 Npgsql | requirements 中的 psycopg2 或 asyncpg |
| MySQL | csproj 中的 MySql.Data | requirements 中的 mysql-connector-python |
| Redis | csproj 中的 StackExchange.Redis | requirements 中的 redis |
| RabbitMQ | csproj 中的 RabbitMQ.Client | requirements 中的 pika 或 aio-pika |
| MongoDB | csproj 中的 MongoDB.Driver | requirements 中的 pymongo |
阶段 2:设计健康检查策略
根据 Kubernetes 最佳实践定义三种健康端点。
端点类型
| 端点 | 探针类型 | 用途 | 检查项 |
|---|---|---|---|
/health/live | 存活 | 应用是否存活? | 应用是否响应(不检查依赖项) |
/health/ready | 就绪 | 应用能否处理流量? | 所有依赖项是否健康 |
/health/startup | 启动(K8s 1.16+) | 应用是否已初始化? | 初始预热是否完成 |
各探针失败时的处理
| 探针 | 失败时的操作 | Kubernetes 行为 |
|---|---|---|
| 存活 | 重启容器 | kubelet 重启容器 |
| 就绪 | 从服务中移除 | 停止流量,不重启 |
| 启动 | 延迟其他探针 | 暂停存活/就绪探针 |
阶段 3:研究健康检查模式
使用 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 连通性检查
- 自定义健康检查实现
- 自定义健康检查响应编写器
阶段 4:配置 Kubernetes 探针
根据应用特性确定探针时序。
探针配置
| 参数 | 存活 | 就绪 | 启动 |
|---|---|---|---|
initialDelaySeconds | 10 | 5 | 0 |
periodSeconds | 10 | 5 | 5 |
timeoutSeconds | 5 | 3 | 3 |
failureThreshold | 3 | 3 | 30 |
successThreshold | 1 | 1 | 1 |
启动探针计算:
Max startup time = initialDelaySeconds + (periodSeconds × failureThreshold)
Default: 0 + (5 × 30) = 150 seconds
阶段 5:生成实现
.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
第 6 阶段:验证
验证步骤:
-
语法检查:
- .NET:
dotnet build --no-restore - Python:
python -m py_compile routes/health.py
- .NET:
-
端点测试:
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"
}
参考链接
关键规则
- 三个独立端点 — 按照 Kubernetes 最佳实践设置
/health/live、/health/ready、/health/startup - 存活检查不得检查依赖项 — 仅确认应用是否存活(避免级联重启)
- 就绪检查所有依赖项 — 验证 DB、Redis、RabbitMQ 的连接
- 从项目文件自动检测依赖项 — 扫描 csproj/requirements,查找已知软件包
- 幂等 — 如果已存在
AddHealthChecks/MapHealthChecks或/health路由,则返回status: "skipped"
完成定义
- 已接收上下文存储(技术栈、项目根目录)
- 已检测依赖项(PostgreSQL、MySQL、Redis、RabbitMQ、MongoDB)
- 已为检测到的技术栈生成健康检查端点(live、ready、startup)
- 已生成包含适当时间参数的 Kubernetes 探针清单片段
- 已验证语法(
dotnet build或py_compile) - 已向 ln-770 协调器返回结构化 JSON 响应
版本: 2.0.0 最后更新: 2026-01-10
评论
支持 Markdown