To upgrade a Python application on Kubernetes, build and push a new container image with a new tag, then change the image in the Deployment. Kubernetes performs a rolling update by default, replacing pods gradually while readiness probes keep traffic away from pods that are not ready. If the new version misbehaves, kubectl rollout undo returns to the previous revision.
For more control you can use a canary release or a blue-green switch. This article covers the full flow: image, manifest, rollout, verification, rollback and the two advanced strategies.
Build the image with an immutable version tag, such as a release number or git commit, and never rely on latest for production. A typical Python image uses a slim base and a production server such as gunicorn or uvicorn.
Python servers shut down cleanly on SIGTERM, which Kubernetes sends before stopping a pod. Make sure your server handles it so in-flight requests finish during the upgrade.
A Deployment with readiness and liveness probes and an explicit rolling strategy gives zero-downtime upgrades for stateless services.
With maxUnavailable: 0 and maxSurge: 1, Kubernetes starts one new pod, waits until it is ready, then removes one old pod, and repeats. The readiness probe is what makes this safe.
Change the image from the command line or edit the manifest and re-apply it.
If the new pods fail their probes, the rollout stalls and the old pods keep serving. Revert with:
In a GitOps setup, change the tag in the manifest in Git and let the controller apply it, so the cluster always matches the repository. For a quick restart without a version change, use kubectl rollout restart deployment/python-app.
A canary sends a small share of traffic to the new version first. The simplest approach needs no service mesh: run a stable and a canary Deployment that share one label, and point one Service at that label. Traffic splits roughly in proportion to the number of pods.
The stable Deployment must also carry app: python-app (plus track: stable) and the Service selects only app: python-app. With nine stable pods and one canary pod, about 10 percent of requests reach v2.
For exact percentages, header-based routing and automatic analysis, use the Gateway API HTTPRoute weights, an ingress controller with canary support, Istio, or a progressive delivery tool such as Argo Rollouts or Flagger. Watch error rate and latency, then scale the canary up or delete it.
Blue-green runs the new version (green) beside the old one (blue), tests it, and then moves all traffic at once by changing the Service selector.
Deploy the green Deployment with the label version: green, run smoke tests against it through a separate test Service, then apply the Service change. To roll back, set the selector back to version: blue. Delete the old Deployment only after the new version has proven stable.
| Strategy | Extra capacity | Rollback speed | Best for |
|---|---|---|---|
| Rolling update | Small surge | Fast, rollout undo | Most stateless services |
| Canary | One or a few pods | Fast, remove canary | Risky changes needing real traffic |
| Blue-green | Full second copy | Instant, switch selector | Releases needing full pre-testing |
Make schema migrations backward compatible, so both the old and new application versions can run against the same database during the rollout. Run migrations as a separate Job or init step rather than inside every pod. Roll out configuration changes by updating the ConfigMap and restarting the Deployment, because most apps read environment variables only at startup.
Use a Deployment with a rolling update, maxUnavailable: 0, and a working readiness probe. Kubernetes only sends traffic to new pods once they are ready.
Run kubectl rollout undo deployment/<name>. Use kubectl rollout history to see revisions and --to-revision to pick a specific one.
No. Two Deployments behind one Service give a rough replica-based split. A mesh, Gateway API or Argo Rollouts gives exact weights and automated analysis.
latest tag?No. Use unique tags or image digests so each rollout is reproducible and rollbacks are meaningful.
Push a uniquely tagged image, update the Deployment and let the rolling update replace pods behind readiness probes. Use kubectl rollout status to watch and kubectl rollout undo to revert. Choose a canary for gradual exposure or blue-green for an instant switch, and keep database changes backward compatible.