健康检查接口看起来最简单,却有几个常见的坑:存活和就绪该分开,检查依赖要设超时,探针和业务请求共用 worker,探针的访问日志会把日志淹没。这篇用真实的 gunicorn 一项项量出来(数据库连接的健康检查见第 188 篇,容器信号见第 189 篇)。
依赖正常 /healthz → 200 ok (43 ms) /readyz → 200 {"checks":{"dep":"ok"},"status":"ready"} (1 ms)
依赖被杀掉 /healthz → 200 ok (1 ms) /readyz → 503 {"checks":{"dep":"ConnectionRefusedError"},"status":"not ready"} (1 ms)
依赖重新起来 /healthz → 200 ok (5 ms) /readyz → 200 {"checks":{"dep":"ok"},"status":"ready"} (6 ms)
依赖被杀掉时,进程本身没问题(不该被重启),只是暂时不该接流量
HEAD /healthz → 200,正文 0 字节,Content-Length 2上一篇:Docker 部署 Flask;下一篇:优雅关闭。
依赖是一个自己起的 TCP 小服务(PING/PONG),不是真实数据库。
sync ×1 /slow 处理中时 /healthz → 200,耗时 2615 ms sync ×2 /slow 处理中时 /healthz → 200,耗时 9 ms gthread 1 进程 ×4 线程 /slow 处理中时 /healthz → 200,耗时 6 ms
依赖调用不设超时 → 探针客户端 8 秒后放弃:TimeoutError
(同时)/healthz → 200 (8 ms)
依赖调用超时 0.5 秒 → 503 {"checks":{"dep":"TimeoutError"},"status":"not ready"} (532 ms)默认 accesslog 访问日志共 22 行,其中探针 20 行 logger-class qlog.QuietHealth 访问日志共 2 行,其中探针 0 行