Interview Prep/Build Tools (Java, Node.js, Python, .NET)/Python builds (pip, venv, requirements.txt)

How would you deploy a Python API to Kubernetes in production?

AdvancedProductionPython builds (pip, venv, requirements.txt)
Answer

Short answer

Containerise it with a slim, non-root image running gunicorn/uvicorn; configure it through env vars, ConfigMaps and Secrets; add readiness/liveness probes, resource requests/limits and an HPA; run DB migrations as a pre-deploy Job; and ship it through a pipeline that tests, scans and promotes immutable image tags.

Key pieces

containers:
- name: orders-api
  image: registry.company.com/orders-api:1.3.0-9f2c1ab
  command: ["gunicorn", "-w", "2", "-b", "0.0.0.0:8000", "app:app"]
  ports: [{ containerPort: 8000 }]
  envFrom:
    - configMapRef: { name: orders-config }
    - secretRef: { name: orders-secrets }
  readinessProbe: { httpGet: { path: /health, port: 8000 }, periodSeconds: 5 }
  livenessProbe:  { httpGet: { path: /health, port: 8000 }, initialDelaySeconds: 20 }
  resources:
    requests: { cpu: 250m, memory: 256Mi }
    limits:   { memory: 512Mi }

Checklist

  • Workers per pod: 2–4; scale out with HPA on CPU or request rate.
  • Graceful shutdown: gunicorn handles SIGTERM; add a short preStop sleep so the pod leaves the Service before stopping.
  • Migrations: Alembic/Django migrate as a Helm hook or Job, backward-compatible.
  • Logs: JSON to stdout, PYTHONUNBUFFERED=1.
  • Security: non-root, read-only root filesystem, image scanned with Trivy.
  • Secrets: External Secrets from Vault/Secrets Manager/Key Vault.

What to say in the interview

"A slim non-root image running gunicorn with a few workers, config via ConfigMap and Secrets, probes, requests/limits and an HPA. Migrations run as a pre-upgrade Job, logs are unbuffered JSON on stdout, and images are tested, scanned and promoted by tag through GitOps."

All Build Tools (Java, Node.js, Python, .NET) questions Roadmap: Python apps: pip, venv & requirements.txt

More on Python builds (pip, venv, requirements.txt)