ln-774-healthcheck-setup
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
Chineseln-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
概述
| Aspect | Details |
|---|---|
| Input | Context Store from ln-770 |
| Output | Health 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:
- : .NET or Python
STACK - : Project directory path
PROJECT_ROOT
Idempotency Check:
- .NET: Grep for or
AddHealthChecksMapHealthChecks - Python: Grep for route
/health - If found: Return
{ "status": "skipped" }
Dependency Detection:
| Dependency | .NET Detection | Python Detection |
|---|---|---|
| PostgreSQL | | |
| MySQL | | |
| Redis | | |
| RabbitMQ | | |
| MongoDB | | |
接收上下文存储并扫描需要监控的依赖项。
必要上下文:
- : .NET 或 Python
STACK - : 项目目录路径
PROJECT_ROOT
幂等性检查:
- .NET: 搜索 或
AddHealthChecksMapHealthChecks - Python: 搜索 路由
/health - 如果已存在:返回
{ "status": "skipped" }
依赖项检测:
| 依赖项 | .NET 检测方式 | Python 检测方式 |
|---|---|---|
| PostgreSQL | csproj 中存在 | requirements 中存在 |
| MySQL | csproj 中存在 | requirements 中存在 |
| Redis | csproj 中存在 | requirements 中存在 |
| RabbitMQ | csproj 中存在 | requirements 中存在 |
| MongoDB | csproj 中存在 | requirements 中存在 |
Phase 2: Design Health Check Strategy
阶段2:设计健康检查策略
Define three types of health endpoints per Kubernetes best practices.
根据Kubernetes最佳实践定义三种类型的健康检查端点。
Endpoint Types
端点类型
| Endpoint | Probe Type | Purpose | Checks |
|---|---|---|---|
| Liveness | Is app alive? | App responds (no dependency checks) |
| Readiness | Can app serve traffic? | All dependencies healthy |
| Startup (K8s 1.16+) | Is app initialized? | Initial warmup complete |
| 端点 | 探针类型 | 用途 | 检查内容 |
|---|---|---|---|
| 存活探针 | 应用是否存活? | 应用可响应(不检查依赖项) |
| 就绪探针 | 应用能否处理流量? | 所有依赖项状态正常 |
| 启动探针(K8s 1.16+) | 应用是否完成初始化? | 初始预热完成 |
When Each Probe Fails
探针失败后的处理
| Probe | Failure Action | Kubernetes Behavior |
|---|---|---|
| Liveness | Container restart | kubelet restarts container |
| Readiness | Remove from service | Traffic stopped, no restart |
| Startup | Delay other probes | Liveness/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/aspnetcoreFor Python:
MCP ref: "FastAPI health check endpoint Kubernetes"
Context7: /tiangolo/fastapiKey Patterns to Research:
- Database health checks (connection pool)
- Redis connectivity check
- Custom health check implementation
- 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需研究的核心模式:
- 数据库健康检查(连接池)
- Redis连通性检查
- 自定义健康检查实现
- 健康检查响应写入器定制
Phase 4: Configure Kubernetes Probes
阶段4:配置Kubernetes探针
Determine probe timing based on application characteristics.
根据应用特性确定探针时序参数。
Probe Configuration
探针配置
| Parameter | Liveness | Readiness | Startup |
|---|---|---|---|
| 10 | 5 | 0 |
| 10 | 5 | 5 |
| 5 | 3 | 3 |
| 3 | 3 | 30 |
| 1 | 1 | 1 |
Startup Probe Calculation:
Max startup time = initialDelaySeconds + (periodSeconds × failureThreshold)
Default: 0 + (5 × 30) = 150 seconds| 参数 | 存活探针 | 就绪探针 | 启动探针 |
|---|---|---|---|
| 10 | 5 | 0 |
| 10 | 5 | 5 |
| 5 | 3 | 3 |
| 3 | 3 | 30 |
| 1 | 1 | 1 |
启动探针最大时长计算:
最大启动时间 = initialDelaySeconds + (periodSeconds × failureThreshold)
默认值: 0 + (5 × 30) = 150 秒Phase 5: Generate Implementation
阶段5:生成实现代码
.NET Output Files
.NET 输出文件
| File | Purpose |
|---|---|
| Health check registration |
| Custom startup check |
Generation Process:
- Use MCP ref for current ASP.NET Core health checks API
- Generate HealthCheckExtensions with:
- AddHealthChecks registration
- Database health check (if detected)
- Redis health check (if detected)
- Custom StartupHealthCheck
- Configure three endpoints with proper tags
Packages to Add:
- (if PostgreSQL)
AspNetCore.HealthChecks.NpgSql - (if Redis)
AspNetCore.HealthChecks.Redis - (if MySQL)
AspNetCore.HealthChecks.MySql
Registration Code:
csharp
builder.Services.AddHealthCheckServices(builder.Configuration);
// ...
app.MapHealthCheckEndpoints();| 文件 | 用途 |
|---|---|
| 健康检查注册逻辑 |
| 自定义启动检查逻辑 |
生成流程:
- 基于MCP参考获取最新ASP.NET Core健康检查API
- 生成HealthCheckExtensions,包含:
- AddHealthChecks 注册
- 数据库健康检查(若检测到对应依赖)
- Redis健康检查(若检测到对应依赖)
- 自定义StartupHealthCheck
- 配置三个带正确标签的端点
需添加的包:
- (若使用PostgreSQL)
AspNetCore.HealthChecks.NpgSql - (若使用Redis)
AspNetCore.HealthChecks.Redis - (若使用MySQL)
AspNetCore.HealthChecks.MySql
注册代码:
csharp
builder.Services.AddHealthCheckServices(builder.Configuration);
// ...
app.MapHealthCheckEndpoints();Python Output Files
Python 输出文件
| File | Purpose |
|---|---|
| Health check router |
| Dependency health checks |
Generation Process:
- Use MCP ref for FastAPI health patterns
- Generate health router with:
- /health/live endpoint (simple)
- /health/ready endpoint (with dependency checks)
- /health/startup endpoint
- Generate health_checker service for dependency verification
Registration Code:
python
from routes.health import health_router
app.include_router(health_router)| 文件 | 用途 |
|---|---|
| 健康检查路由 |
| 依赖项健康检查服务 |
生成流程:
- 基于MCP参考获取FastAPI健康检查最佳实践
- 生成健康路由,包含:
- /health/live 端点(基础存活检查)
- /health/ready 端点(含依赖项检查)
- /health/startup 端点
- 生成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: 30Phase 6: Validate
阶段6:验证
Validation Steps:
-
Syntax check:
- .NET:
dotnet build --no-restore - Python:
python -m py_compile routes/health.py
- .NET:
-
Endpoint test:bash
curl http://localhost:5000/health/live curl http://localhost:5000/health/ready curl http://localhost:5000/health/startup -
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" } -
Dependency failure test:
- Stop database
- Verify returns 503
/health/ready - Verify still returns 200
/health/live
验证步骤:
-
语法检查:
- .NET:
dotnet build --no-restore - Python:
python -m py_compile routes/health.py
- .NET:
-
端点测试:bash
curl http://localhost:5000/health/live curl http://localhost:5000/health/ready curl http://localhost:5000/health/startup -
验证响应格式: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" } -
依赖项失败测试:
- 停止数据库服务
- 验证 返回503状态码
/health/ready - 验证 仍返回200状态码
/health/live
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
参考链接
Version: 2.0.0
Last Updated: 2026-01-10