health-check-endpoints

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

Health Check Endpoints

健康检查端点

Implement health checks for monitoring service availability and readiness.
实现健康检查以监控服务的可用性与就绪状态。

Probe Types

探针类型

ProbePurposeFailure Action
LivenessIs process alive?Restart container
ReadinessCan handle traffic?Remove from LB
StartupHas app started?Delay other probes
DeepAll deps healthy?Trigger alerts
探针类型用途失败触发动作
存活探针进程是否存活?重启容器
就绪探针是否可处理流量?从负载均衡器中移除
启动探针应用是否已启动?延迟其他探针执行
深度探针所有依赖项是否健康?触发告警

Implementation (Express)

基于Express的实现

javascript
class HealthChecker {
  async checkDatabase() {
    const start = Date.now();
    try {
      await db.query('SELECT 1');
      return { status: 'healthy', latency: Date.now() - start };
    } catch (err) {
      return { status: 'unhealthy', error: String(err?.message || err) };
    }
  }

  async checkRedis() {
    try {
      await redis.ping();
      return { status: 'healthy' };
    } catch (err) {
      return { status: 'unhealthy', error: err.message };
    }
  }

  async getReadiness() {
    const checks = await Promise.all([
      this.checkDatabase(),
      this.checkRedis()
    ]);
    const healthy = checks.every(c => c.status === 'healthy');
    return { healthy, checks };
  }
}

// Liveness - lightweight
app.get('/health/live', (req, res) => {
  res.json({ status: 'ok', timestamp: new Date().toISOString() });
});

// Readiness - check dependencies
app.get('/health/ready', async (req, res) => {
  const health = await healthChecker.getReadiness();
  res.status(health.healthy ? 200 : 503).json(health);
});
javascript
class HealthChecker {
  async checkDatabase() {
    const start = Date.now();
    try {
      await db.query('SELECT 1');
      return { status: 'healthy', latency: Date.now() - start };
    } catch (err) {
      return { status: 'unhealthy', error: String(err?.message || err) };
    }
  }

  async checkRedis() {
    try {
      await redis.ping();
      return { status: 'healthy' };
    } catch (err) {
      return { status: 'unhealthy', error: err.message };
    }
  }

  async getReadiness() {
    const checks = await Promise.all([
      this.checkDatabase(),
      this.checkRedis()
    ]);
    const healthy = checks.every(c => c.status === 'healthy');
    return { healthy, checks };
  }
}

// Liveness - lightweight
app.get('/health/live', (req, res) => {
  res.json({ status: 'ok', timestamp: new Date().toISOString() });
});

// Readiness - check dependencies
app.get('/health/ready', async (req, res) => {
  const health = await healthChecker.getReadiness();
  res.status(health.healthy ? 200 : 503).json(health);
});

Kubernetes Configuration

Kubernetes配置

yaml
livenessProbe:
  httpGet:
    path: /health/live
    port: 3000
  initialDelaySeconds: 15
  periodSeconds: 10
  failureThreshold: 3

readinessProbe:
  httpGet:
    path: /health/ready
    port: 3000
  initialDelaySeconds: 5
  periodSeconds: 10
yaml
livenessProbe:
  httpGet:
    path: /health/live
    port: 3000
  initialDelaySeconds: 15
  periodSeconds: 10
  failureThreshold: 3

readinessProbe:
  httpGet:
    path: /health/ready
    port: 3000
  initialDelaySeconds: 5
  periodSeconds: 10

Best Practices

最佳实践

  • Keep liveness checks minimal (no external deps)
  • Check only critical systems in readiness
  • Return 200 for healthy, 503 for unhealthy
  • Set reasonable timeouts to prevent cascading failures
  • Include response time metrics
  • 保持存活检查尽可能轻量化(不依赖外部服务)
  • 仅在就绪检查中验证关键系统
  • 健康状态返回200,不健康返回503
  • 设置合理的超时时间以防止级联故障
  • 包含响应时间指标

Additional Implementations

其他实现方案

See references/implementations.md for:
  • Python Flask complete health checker
  • Java Spring Boot Actuator
  • Full Kubernetes deployment config
查看references/implementations.md获取:
  • Python Flask完整健康检查器
  • Java Spring Boot Actuator
  • 完整Kubernetes部署配置

Never Do

禁忌操作

  • Make liveness depend on external services
  • Return 200 when dependencies are down
  • Skip dependency checks in readiness
  • 让存活检查依赖外部服务
  • 依赖项故障时仍返回200
  • 就绪检查中跳过依赖项检查