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.
More Related questions...