The fastapi-serve
library is a versatile tool that not only helps you deploy FastAPI applications to the cloud but also ensures zero downtime during application upgrades. This tutorial walks you through the process of testing zero downtime upgrades for your FastAPI applications.
Here's the directory structure of the FastAPI app:
.
โโโ main.py # The FastAPI app
โโโ README.md # This README file
โโโ requirements.txt # The requirements file for the FastAPI app
โโโ zero_downtime.py # Python script to check service availability and version updates
This template application provides a simple /health
endpoint and an /endpoint
that responds with a Hello: World
JSON object. We'll first deploy the application, then run a script that continually checks these endpoints. We'll then update the application, redeploy it, and verify that the version of the app was updated without any downtime.
# main.py
import os
from fastapi import FastAPI
from pydantic import BaseModel, Field
__version__ = "0.0.1"
app = FastAPI()
class Health(BaseModel):
status: str
version: str = __version__
revision: str = Field(default_factory=lambda: os.environ.get("K_REVISION", "0.0.0"))
@app.get("/health")
def health():
return Health(status="ok")
@app.get("/endpoint")
def endpoint():
return {"Hello": "World"}
Let's start with deploying the app.
fastapi-serve deploy jcloud main:app
โญโโโโโโโโโโโโโโโโโโโโโโโโโโฌโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโฎ
โ App ID โ fastapi-3a8d2d474f โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโผโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโค
โ Phase โ Serving โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโผโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโค
โ Endpoint โ https://fastapi-3a8d2d474f.wolf.jina.ai โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโผโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโค
โ App logs โ https://cloud.jina.ai/ โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโผโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโค
โ Base credits (per hour) โ 10.104 (Read about pricing here) โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโผโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโค
โ Swagger UI โ https://fastapi-3a8d2d474f.wolf.jina.ai/docs โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโผโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโค
โ OpenAPI JSON โ https://fastapi-3a8d2d474f.wolf.jina.ai/openapi.json โ
โฐโโโโโโโโโโโโโโโโโโโโโโโโโโดโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโฏ
The zero_downtime.py
script continuously checks the /health
endpoint and reports the status and version of the service. Run the script in your terminal:
python zero_downtime.py https://fastapi-3a8d2d474f.wolf.jina.ai
Service status: ok, Version: 0.0.1, Revision: gateway-00001
Service status: ok, Version: 0.0.1, Revision: gateway-00001
Service status: ok, Version: 0.0.1, Revision: gateway-00001
As you can see, the service is up and running, and the version is 0.0.1
.
Let's modify the FastAPI app. We'll change the version number __version__
and the response of the /endpoint
endpoint to Hello: Universe
.
# main.py
import os
from fastapi import FastAPI
from pydantic import BaseModel, Field
__version__ = "0.0.2"
app = FastAPI()
class Health(BaseModel):
status: str
version: str = __version__
revision: str = Field(default_factory=lambda: os.environ.get("K_REVISION", "0.0.0"))
@app.get("/health")
def health():
return Health(status="ok")
@app.get("/endpoint")
def endpoint():
return {"Hello": "Universe"}
Then, redeploy your app with the new changes. Don't forget to use the same app ID as before.
fastapi-serve deploy jcloud main:app --app-id fastapi-3a8d2d474f
While the update is going on, let's start another terminal and run the zero_downtime.py
script again:
python zero_downtime.py https://fastapi-3a8d2d474f.wolf.jina.ai
...
Service status: ok, Version: 0.0.1, Revision: gateway-00001
Service status: ok, Version: 0.0.1, Revision: gateway-00001
Service status: ok, Version: 0.0.1, Revision: gateway-00001
Version updated from 0.0.1 to 0.0.2
Service status: ok, Version: 0.0.2, Revision: gateway-00002
Service status: ok, Version: 0.0.2, Revision: gateway-00002
Service status: ok, Version: 0.0.2, Revision: gateway-00002
...
Eventually, you'll see that the version has been updated to 0.0.2
without any downtime. The zero_downtime.py
script also reports the revision number, which is incremented every time the app is updated.
check
With fastapi-serve
, you gain the ability to perform zero-downtime upgrades, eliminating service disruptions and enhancing your application's reliability by providing your users with constant access even during updates. Users don't need to deal with application downtime or manually check for updates.