GithubHelp home page GithubHelp logo

danielchudc / kubernetes-fastapi Goto Github PK

View Code? Open in Web Editor NEW

This project forked from 4oh4/kubernetes-fastapi

0.0 1.0 0.0 256 KB

Barebones Python FastAPI in Kubernetes

License: MIT License

Dockerfile 11.08% Python 88.92%

kubernetes-fastapi's Introduction

License: MIT

kubernetes-fastapi

Template for a Python FastAPI with Dockerfile and configuration for Kubernetes

Development setup

To run (in isolation), either:

Run from active Python environment using uvicorn:

pip install -r requirements.txt
uvicorn service.main:app --host 0.0.0.0 --port 8080 --reload

Or build and run the Docker container:

docker build -t 4oh4/kubernetes-fastapi:1.0.0 .
docker run -p 8080:8080 --name kubernetes-fastapi 4oh4/kubernetes-fastapi:1.0.0

Navigate to http://localhost:8080/docs to test the API.

Test drive the API

The API responds with a greeting, and the result of a long-running calculation of the largest prime factor of a random integer. You should see a response body similar to:

{
  "message1": "Hello, world!",
  "message2": "The largest prime factor of 1462370954730 is 398311. Calculation took 0.006 seconds.",
  "n": 1462370954730,
  "largest_prime_factor": 398311,
  "elapsed_time": 0.0057561397552490234
}

Push the container image to Docker Hub

If desired, push the container to Docker Hub yourself, and change all references to the image accordingly. Replace "4oh4" with your Docker Hub ID):

docker push 4oh4/kubernetes-fastapi:1.0.0

You may also need to make the image public as well.

Google Cloud GKE initial setup

Follow the steps in this section if deploying to Google Cloud GKE, or skip if deploying to a ready-configured Kubernetes cluster. From command line, with Google Cloud SDK installed:

gcloud components install kubectl

gcloud config set project my-project-id
gcloud config set compute/zone europe-west2-a

Create a cluster and get credentials for kubectl:

gcloud container clusters create my-cluster-name --num-nodes=3
gcloud container clusters get-credentials my-cluster-name

Kubernetes deployment

kubectl apply -f api.yaml

If working locally, e.g. using minikube, use port forwarding to expose the service:

kubectl port-forward service/kf-api-svc 8080

To scale the deployment, apply a HorizontalPodAutoscaler. Either:

kubectl apply -f autoscale.yaml

or:

kubectl autoscale deployment kf-api --cpu-percent=50 --min=1 --max=10

Load testing

Use locust to simulate a high load on the API

pip install locust
locust

Load testing with Locust

Teardown

kubectl delete deployment kf-api
kubectl delete svc kf-api-svc
kubectl delete hpa kf-api-hpa

Google Cloud clean-up

gcloud container clusters delete my-cluster-name

** Check all resources have been deleted in the console - if in doubt, delete the project as well **

Acknowledgements

Inspiration and code for FastAPI setup: How to continuously deploy a fastAPI to AWS Lambda with AWS SAM.

kubernetes-fastapi's People

Contributors

4oh4 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.