FastAPI Production Deployment on Sealos
Launch FastAPI on Sealos with an immutable image, same-image Alembic migration, two hardened replicas, HTTPS, release logs, backup planning, and A-B-A-B recovery.
This production guide completes the FastAPI series. It assumes you have already deployed the Tasks API and proved PostgreSQL migration and restart persistence. Use the protected stage-3-production tag as the source for the image, Job, Deployment, security, and rollback contracts.
Who this checklist is for
Use it when your FastAPI release needs:
- A source-linked immutable container digest
- Alembic migration before traffic reaches new code
- Two horizontally safe application replicas backed by PostgreSQL
- Non-root runtime controls and one clear worker model
- Public HTTPS, generated docs, release identity logs, and monitoring
- A documented update, rollback, recovery, backup, and incident path
Plugin setup
Install the same Sealos plugin used in the earlier stages:
For Claude Code:
Use $sealos in Codex CLI, select Sealos from the Codex App plugin picker,
or use /sealos in Claude Code. A production request should preserve the
existing PostgreSQL-backed Tasks API and its verified DATABASE_URL source.
Production launch model
The accepted practice used the current KubeBlocks PostgreSQL 16.4 runtime and the protected Stage 3 application image. The Job and Deployment consumed the same final digest.
Production readiness scorecard
Aim for this state before switching traffic:
Step 1: Clone and validate Stage 3
Clone the protected tag and reproduce the lock:
The protected tag peels to source commit:
Step 2: Resolve immutable production state
The publisher exposes a lookup tag for each complete source SHA. Resolve it once and use the digest reference for every production workload:
Verify architecture, operating system, revision, source label, and manifest:
The accepted final reference is:
Keep database values in the generated Secret and render the tracked
deploy/migration-job.yaml and deploy/application.yaml placeholders into
mode-0600 scratch files. Bind both manifests to IMAGE_REFERENCE and the same
source release.
Protected Stage 3 source, immutable final digest, redacted database state, and same-image migration relationStep 3: Migrate before rolling out replicas
Validate the rendered manifests against the active API, then apply the migration first:
The accepted migration Job ran as UID/GID 10001, used the exact final image
digest, and reached Complete=True. An independent same-image verification Job
returned:
Step 4: Roll out two hardened replicas
Apply the application only after migration completion:
Require 2/2 Ready. The protected runtime contract gives each Pod:
- UID/GID
10001 - One PID-1 Uvicorn application command
- One Uvicorn worker bound to
0.0.0.0:8000 - A read-only root filesystem
- Dropped Linux capabilities
- Disabled privilege escalation
RuntimeDefaultseccomp- A memory-backed writable
/tmp
The fresh practice verified the exact final image ID on both Pods, two Ready
replicas, and restart counts of 0.
Same-image Alembic migration completed before a 2 of 2 Ready rollout at UID and GID 10001 with health 200Step 5: Verify public HTTPS, docs, tasks, and logs
Use the generated domain as the public acceptance surface:
The current practice verified TLS without bypass flags, exact health 200,
Swagger 200, public task create 201, and task read 200.
Correlate release identity in both Pod logs:
Both Pods emitted the credential-free event=service_start release marker.
The final 61-second Runtime Truth window reported zero active failures, zero
findings, zero restart delta, zero Pod replacement, and stable Ready state.
Public Sealos HTTPS health and Swagger surface beside credential-free FastAPI release logs with zero runtime findingsMonitoring and incident signals
Monitor the same surfaces used for acceptance:
- Public HTTPS
/healthstatus and latency - Deployment Ready and Available replica counts
- Pod restarts and replacements
- Service endpoint inventory
- Migration Job completion and revision
event=service_startrelease identity on each new Pod- Database connection failures after task create and read smoke checks
- Resource saturation for the two-replica workload and PostgreSQL Cluster
Route alerts to a named owner and record the first diagnostic commands in the runbook. Keep health checks lightweight and use a real task smoke when you need database-path evidence.
Backup and migration safety
Before a schema-changing release:
- Create or verify a PostgreSQL recovery point
- Record the current source SHA and image digest
- Review whether the Alembic change is backward-compatible with the previous app
- Test restore or forward repair in a separate environment
- Keep the previous digest available through the recovery window
- Assign one operator to the application rollback and one to database recovery
Image rollback preserves application code state. Database recovery follows the schema and data decision recorded for that release.
UPDATE workflow
Use UPDATE when .sealos/state.json and live Sealos resource truth agree on
the existing application. Resolve the new full-source-SHA image to a digest,
run its same-image migration, apply the retained final application manifest,
wait for rollout, and repeat Runtime Truth Pass. Use DEPLOY when recorded
state cannot identify a verifiable live application.
Keep these values in the release record:
Step 6: Roll back and explicitly recover
The accepted historical authority exercised four states against one PostgreSQL
Task 1:
For immediate application rollback:
Recovery explicitly reapplies the retained final manifest:
The checksum-sealed A-B-A-B evidence kept Task 1 readable through all four
controller revisions, passed rollout undo, and passed explicit final recovery.
Checksum-sealed A-B-A-B FastAPI controller revisions preserving Task 1 through rollout undo and explicit final recoveryProduction runbook
Record this sequence for every release:
- Confirm the protected source and clean lock reproduction.
- Resolve the complete source-SHA image tag to its immutable digest.
- Verify image architecture, source, and revision labels.
- Create a database recovery point for a schema-changing release.
- Render Job and Deployment from the same digest and Secret reference.
- Run the migration Job and verify
Complete=Trueplus Alembic head. - Roll out two replicas and verify image IDs, UID/GID, process model, and restarts.
- Run public health, Swagger, task create/read, endpoint, and log checks.
- Observe a stable Runtime Truth window and record accepted state.
- Keep rollout undo and explicit final manifest recovery ready.
Failure guide
Migration completes but the app remains unready
Verify the application and Job consume the same Secret-backed DATABASE_URL,
then read Alembic current from the same image. Require 0001 (head) for this
source stage.
One replica has a different image ID
Stop acceptance. Compare the Deployment digest, both Pod image IDs, and the source release label. Resume after both replicas use the intended digest.
Logs lack event=service_start
Inspect the PID-1 command and release environment. Each newly started Pod from this protected image emits one credential-free marker.
Rollback restores code but task readback fails
Check PostgreSQL reachability, migration compatibility, and the retained Task ID. Use the release's database recovery decision when schema or data state has changed.
.sealos/state.json points at stale resources
Use live resource truth to choose repair or a fresh DEPLOY. Record new state only after Runtime Truth Pass accepts the replacement.
Final checklist
- The checkout points at protected
stage-3-production - The image resolves from the complete source SHA to an immutable digest
- Image architecture and source/revision labels pass
- Migration and application manifests use the same digest and Secret source
- The migration Job reaches
Complete=Trueat0001 (head) - The Deployment reaches
2/2 Ready - Both Pods run as UID/GID 10001 with one Uvicorn worker
- Both Pod image IDs match the intended digest and restart count is zero
- Public HTTPS
/health,/docs, and/taskspass - Both Pods emit
event=service_start - Monitoring, backup, restore, and incident ownership are recorded
- UPDATE or DEPLOY mode is selected from live resource truth
- Rollout undo and explicit final recovery preserve task continuity
- The production runbook is stored with the release
The FastAPI Framework Tutorial Series is now complete: deploy, PostgreSQL, and production all use one continuous Tasks API and one protected source lifecycle.
FAQ
Previous
Deploy FastAPI with PostgreSQL on Sealos
Keep building
Continue this Sealos path
Unify Your Entire Workflow.
Code in a ready-to-use cloud environment, deploy with a click. Sealos combines the entire dev-to-prod lifecycle into one seamless platform. No more context switching.
Explore with AI
Get AI insights on this article