This project demonstrates how to deploy a simple FastAPI application that displays "Hello, World!" using Kubernetes. It covers the process of creating a FastAPI app, containerizing it with Docker, and deploying it to a Kubernetes cluster.
- Docker installed and configured
- Kubernetes cluster (local or cloud-based)
- kubectl command-line tool installed and configured
- Python 3.9 or later
kubernetes-fastapi-hello-world/
├── app.py
├── Dockerfile
├── requirements.txt
└── kubernetes-deployment.yaml
-
Clone the repository:
git clone <repository-url> cd kubernetes-fastapi-hello-world
-
Build the Docker image:
docker build -t fastapi-k8s-hello-world .
This command builds a Docker image from your Dockerfile. The
-t
flag tags the image with the namefastapi-k8s-hello-world
. The.
at the end specifies that the Dockerfile is in the current directory. -
Tag the Docker image:
docker tag fastapi-k8s-hello-world your-docker-username/fastapi-k8s-hello-world
This step tags your image with your Docker Hub username, preparing it for pushing to Docker Hub. Replace
your-docker-username
with your actual Docker Hub username. -
Push the image to Docker Hub:
docker push your-docker-username/fastapi-k8s-hello-world
This command uploads your Docker image to Docker Hub, making it accessible for your Kubernetes cluster to pull. Ensure you're logged in to Docker Hub (
docker login
) before running this command. -
Deploy to Kubernetes:
kubectl apply -f kubernetes-deployment.yaml
This command creates or updates the resources defined in your
kubernetes-deployment.yaml
file in your Kubernetes cluster.Note: If already deployed, you can delete the existing deployment and redeploy: Check the status of the pods:
kubectl get pods
This shows the current running pods in your cluster. Delete the deployment:
kubectl delete -f kubernetes-deployment.yaml
This removes the existing deployment and associated resources.
Redeploy the application:
kubectl apply -f kubernetes-deployment.yaml
This creates a fresh deployment with your updated configuration or image.
-
Access the application:
kubectl port-forward service/fastapi-hello-world 8080:80
This command forwards traffic from your local machine's port 8080 to port 80 of the
fastapi-hello-world
service in your Kubernetes cluster.Open a web browser and navigate to
http://localhost:8080
You should now see your FastAPI application's "Hello, World!" message in your web browser.