mcpField
L
ln-774-healthcheck-setup

ln-774-healthcheck-setup

github.com/levnikolaevich云与 DevOps584385 次安装2 天前更新
代码仓库

关于 ln-774-healthcheck-setup

用于项目初始化的技能,可为 .NET 或 Python 项目添加健康检查端点和 Kubernetes 探针配置。它会检测已知依赖,并跳过已有的健康检查设置。

主要功能

  1. 01配置 .NET 和 Python 健康检查
  2. 02提供存活、就绪和启动端点
  3. 03从项目文件检测依赖
  4. 04配置 Kubernetes 探针
  5. 05验证语法和端点

适用场景

  1. 01为新的 .NET 服务添加探针
  2. 02设置 FastAPI 健康检查路由
  3. 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 或 Python
  • PROJECT_ROOT:项目目录路径

幂等性检查:

  • .NET:搜索 AddHealthChecks 或 MapHealthChecks
  • Python:搜索 /health 路由
  • 如果已找到:返回 { "status": "skipped" }

依赖项检测:

依赖项.NET 检测方式Python 检测方式
PostgreSQLcsproj 中的 Npgsqlrequirements 中的 psycopg2 或 asyncpg
MySQLcsproj 中的 MySql.Datarequirements 中的 mysql-connector-python
Rediscsproj 中的 StackExchange.Redisrequirements 中的 redis
RabbitMQcsproj 中的 RabbitMQ.Clientrequirements 中的 pika 或 aio-pika
MongoDBcsproj 中的 MongoDB.Driverrequirements 中的 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

需要研究的关键模式:

  1. 数据库健康检查(连接池)
  2. Redis 连通性检查
  3. 自定义健康检查实现
  4. 自定义健康检查响应编写器

阶段 4:配置 Kubernetes 探针

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

探针配置
参数存活就绪启动
initialDelaySeconds1050
periodSeconds1055
timeoutSeconds533
failureThreshold3330
successThreshold111

启动探针计算:

Max startup time = initialDelaySeconds + (periodSeconds × failureThreshold)
Default: 0 + (5 × 30) = 150 seconds

阶段 5:生成实现

.NET 输出文件
文件用途
Extensions/HealthCheckExtensions.cs健康检查注册
HealthChecks/StartupHealthCheck.cs自定义启动检查

生成流程:

  1. 使用 MCP ref 查询最新的 ASP.NET Core 健康检查 API
  2. 生成 HealthCheckExtensions,包含:
    • AddHealthChecks 注册
    • 数据库健康检查(如果检测到)
    • Redis 健康检查(如果检测到)
    • 自定义 StartupHealthCheck
  3. 使用恰当的标签配置三个端点

要添加的包:

  • 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依赖项健康检查

生成流程:

  1. 使用 MCP ref 参考 FastAPI 健康检查模式
  2. 生成健康检查路由器,包含:
    • /health/live 端点(简单检查)
    • /health/ready 端点(检查依赖项)
    • /health/startup 端点
  3. 生成用于验证依赖项的 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 阶段:验证

验证步骤:

  1. 语法检查:

    • .NET:dotnet build --no-restore
    • Python:python -m py_compile routes/health.py
  2. 端点测试:

    curl http://localhost:5000/health/live
    curl http://localhost:5000/health/ready
    curl http://localhost:5000/health/startup
    
  3. 验证响应格式:

    {
      "status": "Healthy",
      "checks": {
        "database": { "status": "Healthy", "duration": "00:00:00.0234" },
        "redis": { "status": "Healthy", "duration": "00:00:00.0012" }
      },
      "totalDuration": "00:00:00.0250"
    }
    
  4. 依赖项故障测试:

    • 停止数据库
    • 验证 /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