Health 检查
一句话解释
health 检查是 periodic 测试那个验证 service 是运行 and ready 到 serve 请求.
用大白话说
health 检查是 简单测试那个 asks "是这个 service healthy?" 它是 通常 HTTP 端点 (如 /health or /healthz) that returns a 200 status code if the service is fine and a non-200 code (or no response) if something is wrong.
Health checks are used by load balancers, orchestrators (Kubernetes), and monitoring systems to decide whether to send traffic to a service. If the health check fails, the service is removed from the load balancer's rotation until it recovers.
There are typically two types: liveness (is the service running?) and readiness (is the service ready to handle requests?). A service might be running but not ready — for example, it might still be loading data or waiting for a database connection.
示例
// Express.js health check endpoint
app.get("/health", (req, res) => {
if (db.isConnected()) {
res.status(200).json({ status: "healthy" });
} else {
res.status(503).json({ status: "unhealthy" });
}
});这个端点返回 200 如果数据库是 已连接 (healthy) and 503 如果不 (unhealthy).
工作原理
- The service exposes a health check endpoint (e.g., /health).
- A load balancer or orchestrator calls the endpoint periodically.
- If the response is healthy (200), traffic is sent to the service.
- If unhealthy (non-200), traffic is stopped until the service recovers.
常见混淆
容易混淆的对象: Liveness Probe
关键区别: health 检查是 通用概念; liveness probe 是 Kubernetes' 特定 implementation 的 health 检查那个 determines 如果容器应该是 restarted.
相关术语
解读错误信息
用大白话解释常见错误——什么意思、为什么发生、如何修复。