ln-774-healthcheck-setup

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

ln-774-healthcheck-setup

ln-774-healthcheck-setup

Type: L3 Worker Category: 7XX Project Bootstrap Parent: ln-770-crosscutting-setup
Configures health check endpoints for Kubernetes probes and monitoring.

类型: L3 执行角色 分类: 7XX 项目初始化 父任务: ln-770-crosscutting-setup
为Kubernetes探针和监控配置健康检查端点。

Overview

概述

AspectDetails
InputContext Store from ln-770
OutputHealth check endpoints and Kubernetes probe configuration
Stacks.NET (AspNetCore.Diagnostics.HealthChecks), Python (FastAPI routes)

方面详情
输入来自ln-770的上下文存储
输出健康检查端点和Kubernetes探针配置
技术栈.NET (AspNetCore.Diagnostics.HealthChecks), Python (FastAPI路由)

Phase 1: Receive Context + Identify Dependencies

阶段1:接收上下文 + 识别依赖项

Accept Context Store and scan for dependencies to monitor.
Required Context:
  • STACK
    : .NET or Python
  • PROJECT_ROOT
    : Project directory path
Idempotency Check:
  • .NET: Grep for
    AddHealthChecks
    or
    MapHealthChecks
  • Python: Grep for
    /health
    route
  • If found: Return
    { "status": "skipped" }
Dependency Detection:
Dependency.NET DetectionPython Detection
PostgreSQL
Npgsql
in csproj
psycopg2
or
asyncpg
in requirements
MySQL
MySql.Data
in csproj
mysql-connector-python
in requirements
Redis
StackExchange.Redis
in csproj
redis
in requirements
RabbitMQ
RabbitMQ.Client
in csproj
pika
or
aio-pika
in requirements
MongoDB
MongoDB.Driver
in csproj
pymongo
in requirements

接收上下文存储并扫描需要监控的依赖项。
必要上下文:
  • STACK
    : .NET 或 Python
  • PROJECT_ROOT
    : 项目目录路径
幂等性检查:
  • .NET: 搜索
    AddHealthChecks
    MapHealthChecks
  • Python: 搜索
    /health
    路由
  • 如果已存在:返回
    { "status": "skipped" }
依赖项检测:
依赖项.NET 检测方式Python 检测方式
PostgreSQLcsproj 中存在
Npgsql
requirements 中存在
psycopg2
asyncpg
MySQLcsproj 中存在
MySql.Data
requirements 中存在
mysql-connector-python
Rediscsproj 中存在
StackExchange.Redis
requirements 中存在
redis
RabbitMQcsproj 中存在
RabbitMQ.Client
requirements 中存在
pika
aio-pika
MongoDBcsproj 中存在
MongoDB.Driver
requirements 中存在
pymongo

Phase 2: Design Health Check Strategy

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

Define three types of health endpoints per Kubernetes best practices.
根据Kubernetes最佳实践定义三种类型的健康检查端点。

Endpoint Types

端点类型

EndpointProbe TypePurposeChecks
/health/live
LivenessIs app alive?App responds (no dependency checks)
/health/ready
ReadinessCan app serve traffic?All dependencies healthy
/health/startup
Startup (K8s 1.16+)Is app initialized?Initial warmup complete
端点探针类型用途检查内容
/health/live
存活探针应用是否存活?应用可响应(不检查依赖项)
/health/ready
就绪探针应用能否处理流量?所有依赖项状态正常
/health/startup
启动探针(K8s 1.16+)应用是否完成初始化?初始预热完成

When Each Probe Fails

探针失败后的处理

ProbeFailure ActionKubernetes Behavior
LivenessContainer restartkubelet restarts container
ReadinessRemove from serviceTraffic stopped, no restart
StartupDelay other probesLiveness/Readiness paused

探针失败操作Kubernetes 行为
存活探针重启容器kubelet 重启容器
就绪探针从服务中移除停止流量转发,不重启容器
启动探针延迟其他探针存活/就绪探针暂停执行

Phase 3: Research Health Check Patterns

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

Use MCP tools for current documentation.
For .NET:
MCP ref: "ASP.NET Core health checks Kubernetes probes"
Context7: /dotnet/aspnetcore
For Python:
MCP ref: "FastAPI health check endpoint Kubernetes"
Context7: /tiangolo/fastapi
Key Patterns to Research:
  1. Database health checks (connection pool)
  2. Redis connectivity check
  3. Custom health check implementation
  4. Health check response writer customization

使用MCP工具获取最新文档。
针对.NET:
MCP 参考: "ASP.NET Core health checks Kubernetes probes"
Context7: /dotnet/aspnetcore
针对Python:
MCP 参考: "FastAPI health check endpoint Kubernetes"
Context7: /tiangolo/fastapi
需研究的核心模式:
  1. 数据库健康检查(连接池)
  2. Redis连通性检查
  3. 自定义健康检查实现
  4. 健康检查响应写入器定制

Phase 4: Configure Kubernetes Probes

阶段4:配置Kubernetes探针

Determine probe timing based on application characteristics.
根据应用特性确定探针时序参数。

Probe Configuration

探针配置

ParameterLivenessReadinessStartup
initialDelaySeconds
1050
periodSeconds
1055
timeoutSeconds
533
failureThreshold
3330
successThreshold
111
Startup Probe Calculation:
Max startup time = initialDelaySeconds + (periodSeconds × failureThreshold)
Default: 0 + (5 × 30) = 150 seconds

参数存活探针就绪探针启动探针
initialDelaySeconds
1050
periodSeconds
1055
timeoutSeconds
533
failureThreshold
3330
successThreshold
111
启动探针最大时长计算:
最大启动时间 = initialDelaySeconds + (periodSeconds × failureThreshold)
默认值: 0 + (5 × 30) = 150 秒

Phase 5: Generate Implementation

阶段5:生成实现代码

.NET Output Files

.NET 输出文件

FilePurpose
Extensions/HealthCheckExtensions.cs
Health check registration
HealthChecks/StartupHealthCheck.cs
Custom startup check
Generation Process:
  1. Use MCP ref for current ASP.NET Core health checks API
  2. Generate HealthCheckExtensions with:
    • AddHealthChecks registration
    • Database health check (if detected)
    • Redis health check (if detected)
    • Custom StartupHealthCheck
  3. Configure three endpoints with proper tags
Packages to Add:
  • AspNetCore.HealthChecks.NpgSql
    (if PostgreSQL)
  • AspNetCore.HealthChecks.Redis
    (if Redis)
  • AspNetCore.HealthChecks.MySql
    (if MySQL)
Registration Code:
csharp
builder.Services.AddHealthCheckServices(builder.Configuration);
// ...
app.MapHealthCheckEndpoints();
文件用途
Extensions/HealthCheckExtensions.cs
健康检查注册逻辑
HealthChecks/StartupHealthCheck.cs
自定义启动检查逻辑
生成流程:
  1. 基于MCP参考获取最新ASP.NET Core健康检查API
  2. 生成HealthCheckExtensions,包含:
    • AddHealthChecks 注册
    • 数据库健康检查(若检测到对应依赖)
    • Redis健康检查(若检测到对应依赖)
    • 自定义StartupHealthCheck
  3. 配置三个带正确标签的端点
需添加的包:
  • AspNetCore.HealthChecks.NpgSql
    (若使用PostgreSQL)
  • AspNetCore.HealthChecks.Redis
    (若使用Redis)
  • AspNetCore.HealthChecks.MySql
    (若使用MySQL)
注册代码:
csharp
builder.Services.AddHealthCheckServices(builder.Configuration);
// ...
app.MapHealthCheckEndpoints();

Python Output Files

Python 输出文件

FilePurpose
routes/health.py
Health check router
services/health_checker.py
Dependency health checks
Generation Process:
  1. Use MCP ref for FastAPI health patterns
  2. Generate health router with:
    • /health/live endpoint (simple)
    • /health/ready endpoint (with dependency checks)
    • /health/startup endpoint
  3. Generate health_checker service for dependency verification
Registration Code:
python
from routes.health import health_router
app.include_router(health_router)
文件用途
routes/health.py
健康检查路由
services/health_checker.py
依赖项健康检查服务
生成流程:
  1. 基于MCP参考获取FastAPI健康检查最佳实践
  2. 生成健康路由,包含:
    • /health/live 端点(基础存活检查)
    • /health/ready 端点(含依赖项检查)
    • /health/startup 端点
  3. 生成health_checker服务用于验证依赖项状态
注册代码:
python
from routes.health import health_router
app.include_router(health_router)

Kubernetes Manifest Snippet

Kubernetes 清单片段

Generate for inclusion in deployment.yaml:
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

生成可嵌入deployment.yaml的配置:
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

Phase 6: Validate

阶段6:验证

Validation Steps:
  1. Syntax check:
    • .NET:
      dotnet build --no-restore
    • Python:
      python -m py_compile routes/health.py
  2. Endpoint test:
    bash
    curl http://localhost:5000/health/live
    curl http://localhost:5000/health/ready
    curl http://localhost:5000/health/startup
  3. Verify response format:
    json
    {
      "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. Dependency failure test:
    • Stop database
    • Verify
      /health/ready
      returns 503
    • Verify
      /health/live
      still returns 200

验证步骤:
  1. 语法检查:
    • .NET:
      dotnet build --no-restore
    • Python:
      python -m py_compile routes/health.py
  2. 端点测试:
    bash
    curl http://localhost:5000/health/live
    curl http://localhost:5000/health/ready
    curl http://localhost:5000/health/startup
  3. 验证响应格式:
    json
    {
      "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状态码

Return to Coordinator

返回至协调器

json
{
  "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"
}

json
{
  "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"
}

Reference Links

参考链接