Skip to content

tsadimas/fastapi-postgres-example

Repository files navigation

Run the project locally

docker-compose up --build

Deploy the project to a kubernetes cluster

  • connect to cluster

persistent volumes

kubectl apply -f k8s/db/postgres-pvc.yml

configmaps

create fastapi configmap

kubectl create configmap fastapi-config --from-env-file=k8s/fastapi/fastapi.env

deployments

kubectl apply -f k8s/db/postgres-deployment.yml
kubectl apply -f k8s/fastapi/fastapi-deployment.yml

services

kubectl apply -f k8s/db/postgres-clip.yml
kubectl apply -f k8s/fastapi/fastapi-clip.yml

secrets

  • pg secret
kubectl create secret generic pg-user \
--from-literal=PGUSER=<put user name here> \
--from-literal=PGPASSWORD=<put password here>
  • tls secret
cd nginx/certs
kubectl create secret generic tls-secret \ 
 --from-file=tls.crt=server.crt \             
 --from-file=tls.key=server.key\             
 --from-file=ca.crt=ca_bundle.crt

ingress

  • allow routing. Get the name of your network interface, e.g. eth0 and run
sudo ufw allow in on eth0 && sudo ufw allow out on eth0
sudo ufw default allow routed
  • apply ingress yml file(s)
kaf k8s/ingress/fastapi-ingress-.yml # for http only
kaf k8s/ingress/fastapi-ingress-ssl.yml # for https

docker registry

Github Packages

  • enable improved container support
  • create personal access token
  • tag an image
docker build -t ghcr.io/tsadimas/myfastapi:latest -f fastapi.Dockerfile .
  • login to docker registry
cat ~/github-image-repo.txt | docker login ghcr.io -u tsadimas --password-stdin
  • push image
docker push ghcr.io/tsadimas/myfastapi:latest

create docker login secret

  • create from the command
echo <USER>:<TOKEN> | base64
  • create kubernetes secret
echo '{"auths":{"ghcr.io":{"auth":"<AUTH>"}}}' | kubectl create secret generic dockerconfigjson-github-com --type=kubernetes.io/dockerconfigjson --from-file=.dockerconfigjson=/dev/stdin

Testing

a test.db file is generated inside app directory

cp .env.example .env
cd app
pytest

Application overview

Image of Yaktocat

Links

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published