添加 /health 就绪检查
告知编排器您的服务何时准备好提供服务
添加 /health 就绪检查 是 CoddyKit 上的免费 MLOps Academy 课时。 这是第 4 节课,共 4 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 AI 导师进行实践。 这是 MLOps Academy 学习路径的一部分,你的进度在网页和 CoddyKit 应用中同步。 MLOps Academy 课程共包含 4 节课。
本课时的部分内容尚未翻译,以英文显示。
How Does the Cluster Know?
Orchestrators like Kubernetes need a way to ask your service: are you alive, and are you ready to take traffic? A health endpoint answers that. 🩺
Liveness vs Readiness
Liveness asks if the process is running. Readiness asks if it can actually serve requests yet. They are two different questions.
A Trivial Health Route
The simplest check is a GET /health that returns ok. If the process can reply at all, it is at least alive.
@app.get("/health")
def health():
return {"status": "ok"}Readiness Needs the Model
For ML, alive is not enough. A real readiness check confirms the model finished loading before you accept prediction traffic.
Check the Loaded Model
You inspect app.state to see if the model is present. If it is still None, the service is not ready yet.
ready = app.state.model is not NoneSignal Not-Ready with 503
When the model is not loaded, return a 503 status so the orchestrator holds traffic back until you are ready.
from fastapi import HTTPException
raise HTTPException(status_code=503)Put It Together
A /ready route checks the model and returns ready or raises 503. The check stays fast and free of heavy work.
@app.get("/ready")
def ready(req: Request):
if req.app.state.model is None:
raise HTTPException(503)
return {"ready": True}Wire Up the Probes
In Kubernetes you point a readinessProbe at /ready and a livenessProbe at /health. The platform polls them on a schedule.
Keep Checks Lightweight
Probes run often, so a health check must be cheap. Never run a real model inference or hit a slow database inside it.
Smooth Zero-Downtime Deploys
Readiness checks enable rolling updates: new pods only get traffic once ready, so users never hit a half-started instance. 🚦
Why It Matters
Health and readiness endpoints make your model service a good citizen in any cluster, enabling safe scaling, restarts, and deploys. ✅
Quick Check
Your model is still loading and a readiness probe hits /ready. What should the endpoint return?
Recap
You split liveness from readiness, returned 503 until the model loads, and wired probes for safe deploys. Your service is cluster-ready! 🙌
常见问题解答
「添加 /health 就绪检查」课时是免费的吗?
是的 — 「添加 /health 就绪检查」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 AI 导师)并解锁 MLOps Academy 课程的其余内容,请升级到 CoddyKit PRO。 MLOps Academy 课程共包含 4 节课。
「添加 /health 就绪检查」这节课中我会学到什么?
告知编排器您的服务何时准备好提供服务 你通过在浏览器中直接运行的动手代码来练习 MLOps Academy,全天候 AI 导师会在你学习这节课的过程中回答你的问题。
学习 MLOps Academy 需要有经验吗?
无需任何先前经验。CoddyKit 上的 MLOps Academy 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 4 节课,共 4 节。
「添加 /health 就绪检查」课时需要多长时间?
大多数 CoddyKit 课程大约需要 5–10 分钟。每节课都很精短且互动,所以你能稳步进步,并在网页和应用中从离开的地方继续。
我能在这节 MLOps Academy 课中编写并运行代码吗?
能。每节 MLOps Academy 课都包含内置代码编辑器,你可以在浏览器中直接编写并运行真实代码,并获得即时 AI 反馈 — 无需本地设置。
此课程中的所有课时
- 您的第一个 /predict 端点
- 使用 Pydantic 验证请求
- 在启动时只加载一次模型
- 添加 /health 就绪检查