Skip to main content

Accessing the FastAPI App

Objective

This lab aims to guide you through the process of accessing your microservices deployed onto a minikube cluster. By using minikube's 'port forwarding' feature and enabling a network tunnel, we'll expose the FastAPI service, allowing you to interact with it from your local machine's web browser. This process is especially crucial for LoadBalancer service types, as they require an additional network route from the host to the service's cluster.

Prerequisites

Initial Setup

Navigate to the root directory of the python-fastapi-demo-docker project where your environment variables are sourced:

cd ~/environment/python-fastapi-demo-docker

1. Checking the Status of Pods

Before we try to access our application, we need to ensure that all of our pods are running correctly. To check the status of all pods, run the following command:

kubectl get pods -n my-cool-app

All your pods should be in the "Running" state. If they're not, you will need to troubleshoot the deployment before proceeding.

2. Accessing the FastAPI Service

Use the tabs below to see the steps for the specific environment where you are running this lab.

Use the minikube service command to create a tunnel to the cluster and connect to FastAPI service:

minikube service fastapi-service --namespace=my-cool-app

The expected output should look like this:

|-------------|-----------------|-------------|---------------------------|
| NAMESPACE | NAME | TARGET PORT | URL |
|-------------|-----------------|-------------|---------------------------|
| my-cool-app | fastapi-service | 80 | http://192.168.49.2:30639 |
|-------------|-----------------|-------------|---------------------------|
🎉 Opening service my-cool-app/fastapi-service in default browser...
👉 http://192.168.49.2:30639

Minikube exposes service port 80 using localhost address. Additionally, you need to expose the service on the EC2 instance public IP address, to be able to access it from your local browser.

Expose the service port 80 using host port 8000 by running

kubectl -n my-cool-app port-forward --address 0.0.0.0 service/fastapi-service 8000:80

Keep this command running while accessing the service with the steps below.

Execute the command below in a new VScode terminal to show the URL to connect to Node Port service fastapi-service:

echo "http://$PUBLIC_IP:8000"

Access this URL using your web browser.

3. Verifying the Setup by Adding a Book

To confirm that everything is functioning as expected, attempt to add a book by selecting the Create a book option.

Image

Now you can press Ctrl+C to stop kubectl command.

Conclusion

This lab has walked you through the steps necessary to access your microservices, specifically the FastAPI service, deployed on a minikube cluster from your local machine. We've shown how to check the status of your pods, enable a minikube tunnel for access, and verify your setup by interacting with the FastAPI service. The minikube service command is a convenient way to expose your Kubernetes services to your local machine and interact with them as if they were locally deployed.