I want to host my fastAPI
application using gunicorn
and host it on a Kubernetes Service. My Kubernetes service runs a liveness probe (health check) using HTTP
call on a specified endpoint.
I also want the application to be served on HTTPS
because my Kubernetes service exposes it to be used by external components.
Now my HTTP endpoint can’t rely on redirection as the liveness probe expects a 200 Response
and redirection will hamper that.
I want to host my HTTPS endpoint on a pre-specified port as the organization has the best practices in place and the endpoint and port are specified.
Some similar problems on StackOverflow:
But both of these are okay with redirection, and we are not. And we cannot use the NGINX
server too, because that support is deprecated in my organization.
2
Answers
If we are trying this out in a Docker environment. The following will get it done:
Dockerfile:
Shell Script start.sh:
The
&
runs one in the background and then runs the other one. You can configure one to use HTTP and one to use HTTPS.We are using
gunicorn
forfastAPI
application so we are usinguvicorn
workers and you need to change it accordingly for your use case.For people landing here looking for fastapi/uvicorn help:
You should know, the background daemon will fail to close on
CTRL+C
. It’s best to use something like tmux, and run the:80
and:443
in different windows.