Python / Uvicorn Fundamentals Interview Questions
What are best practices for deploying Uvicorn in a Kubernetes environment?
Kubernetes brings its own deployment patterns that intersect with Uvicorn's configuration. Getting the combination right ensures zero-downtime deployments, proper health checking, and correct signal handling.
| Concern | Recommendation |
|---|---|
| Host binding | Always --host 0.0.0.0 so Kubernetes can route traffic to pods |
| Workers | Set workers based on pod CPU limits - usually 1-2 per vCPU for I/O-bound apps |
| Health checks | Implement /health and /ready endpoints; Uvicorn has no built-in health endpoint |
| Graceful shutdown | SIGTERM triggers Uvicorn graceful shutdown; set terminationGracePeriodSeconds > timeout-graceful-shutdown |
| Preemptive SIGKILL | If Uvicorn doesn't finish within terminationGracePeriodSeconds, Kubernetes force-kills |
| Environment config | All Uvicorn settings via UVICORN_* env vars in ConfigMap/Secret |
| Liveness vs readiness | Liveness: is the server running? Readiness: is it ready to serve traffic? |
# Kubernetes Deployment snippet spec: containers: - name: api image: myapp:latest command: ["uvicorn", "main:app", "--host", "0.0.0.0", "--port", "8000", "--workers", "2", "--timeout-graceful-shutdown", "25"] ports: - containerPort: 8000 env: - name: UVICORN_LOG_LEVEL value: "warning" livenessProbe: httpGet: path: /health port: 8000 initialDelaySeconds: 5 periodSeconds: 10 readinessProbe: httpGet: path: /ready port: 8000 initialDelaySeconds: 2 periodSeconds: 5 lifecycle: preStop: exec: command: ["sleep", "5"] # allow load balancer to de-register
Pre-stop hook: adding a preStop sleep of 5 seconds gives Kubernetes time to remove the pod from the service endpoint list before SIGTERM is sent. This prevents new requests from being routed to a pod that is already shutting down.
Invest now in Acorns!!! 🚀
Join Acorns and get your $5 bonus!
Acorns is a micro-investing app that automatically invests your "spare change" from daily purchases into diversified, expert-built portfolios of ETFs. It is designed for beginners, allowing you to start investing with as little as $5. The service automates saving and investing. Disclosure: I may receive a referral bonus.
Invest now!!! Get Free equity stock (US, UK only)!
Use Robinhood app to invest in stocks. It is safe and secure. Use the Referral link to claim your free stock when you sign up!.
The Robinhood app makes it easy to trade stocks, crypto and more.
Webull! Receive free stock by signing up using the link: Webull signup.
More Related questions...
