My repository for an app created using Python with FastAPI, connected to a simple React UI, and containerized with Docker, deployed through Kubernetes through helm (tested on Microk8s).
-
Create basic Python app that gathers data from an online API and prints that data to confirm access works.
- Using python
requests
library
- Using python
-
Create Python
FastAPI
app that utilizes that data and has a basic GET endpoint that sends the data from the external API through a FastAPI endpoint.- This endpoint will just send this data as a JSON response.
-
Create a
Dockerfile
that will containerize thisFastAPI
app into aDocker
image that can be ran as a Docker container.- Confirm that this Docker container runs successfully and that the endpoint returns the data properly.
-
Create a basic
React
app usingcreate-react-app
and make sure we can access the data from the endpoint that is running on the previously madeFastAPI Docker Container
.- First just make sure that the data is accessible using a basic console.log() statement.
-
Once the data is accessible through the
React
app, then work on creating a component that will display the data in a list and then use that component in the App.js file.- After this is done some basic styling can be added.
-
Create a
Dockerfile
that will containerize thisReact
app into aDocker
image that can be ran as a Docker container.- Confirm that this Docker container runs properly, and is able to properly access our
FastAPI
Docker container and display the data correctly.
- Confirm that this Docker container runs properly, and is able to properly access our
-
Once both
Docker
images have been created and are able to be used together as separateDocker
images, create adeployment.yaml
file that will allow both of ourDocker
images to be deployed into a localKubernetes
cluster.- In this case will be using
Microk8s
to deploy thedeployment.yaml
chart inKubernetes
, however it should be able to be deployed throughMiniKube
or any otherKubernetes
environment.
- In this case will be using
- Go to
python_code
directory
cd python_code
- Create a Python virtual environment and activate it. Then install requirements from
python_code/requirements.txt
.
python3 -m venv venv
source venv/bin/activate
pip3 install -r requirements.txt
- Run this command in the terminal within the
python_code
directory:
uvicorn app.main:app --host 0.0.0.0 --port 5000 --reload
- Usage:
- Go to this URL to see the available endpoints!
http://localhost:5000/docs
- Go to
react_code
directory
cd react_code
- Have
Node.js
andnpm
installed and then runnpm install
withinreact_code
directory
npm install
- Start the React server using
npm start
npm start
- Usage:
- Go to this URL to see the React App running!
http://localhost:3000
- First set up the Docker Network so the containers can speak with each other:
docker network create fastapi_react_network
- Then run the below Docker container commands using that network. The Docker images will be pulled automatically from Docker Hub.
docker run \
-d \
--rm \
--name fastapi-dog-facts-server \
-p 5000:5000 \
--network fastapi_react_network \
kevinajili/fastapi-dog-facts
- Usage:
- Go to this URL to see the available endpoints!
http://localhost:5000/docs
docker run \
-itd \
--rm \
--name react-app-dog-facts \
-v /app/node_modules \
-p 3000:3000 \
-e CHOKIDAR_USEPOLLING=true \
--network fastapi_react_network \
kevinajili/react-app-dog-facts
- Usage:
- Go to this URL to see the React App running!
http://localhost:3000
- Teardown both Docker containers:
docker kill fastapi-dog-facts-server react-app-dog-facts
- Run this command in the root folder to deploy the deployments and services:
kubectl apply -f deployment.yaml
- Access the services by loading the IP addresses after the services are deployed from the above command.
kubectl get all
- Launch
FastAPI Server
from Kubernetes- Find and copy the
CLUSTER-IP
Address of thefastapi-service
and go to that URL + port 5000
- Find and copy the
http://<CLUSTER-IP_of_service/fastapi-service>:5000/docs
- Launch
React UI
from Kubernetes- Find and copy the
CLUSTER-IP
Address of theui
service and go to that URL + port 3000
- Find and copy the
http://<CLUSTER-IP_of_service/ui>:3000
- Teardown the cluster using this command:
kubectl delete -f deployment.yaml
-
One of the main difficulities of this assignment is actually getting the Docker containers to communicate with each other because the React docker container is trying to access
localhost:5000/dogs
to get information from the FastAPI server, but they are on different networks within the Docker Containers.- I will solve this by using a Docker Network.
- The Docker image was created using a different proxy to connect to the FastAPI docker container's DNS name internally, and for local deployment the proxy in package.json was kept as
localhost
so that it can be run directly.
-
Another difficulty is getting the two Docker containers to run together within Kubernetes
- I will be working through solving this using research and testing to orchestrate the containers into Kubernetes.
- I had to create a separate Docker image for the kubernetes deployment of the react UI, because it needed to point to the internal network
fastapi-service
. But this worked as a solution.- This was similar to what I did to get the Docker containers to connect to each other.
-
Overall I had a lot of fun working on this project, and I feel a huge sense of accomplishment getting each part of it working, and even deployed through Docker directly, and Kubernetes as well.