In this lab, you'll set up and deploy multiple instances of a Flask backend on Kubernetes, configure a Python-based load balancer to distribute traffic across the backend instances, and perform load testing using Apache Benchmark (AB). This lab will introduce you to managing Kubernetes deployments with multiple services, simulating load balancing, and observing request distribution. By the end, you'll have experience with Kubernetes’ traffic management, service discovery, and how to test a load balancer's performance.
- Deploy multiple services in Kubernetes for a distributed backend application.
- Set up a Python-based load balancer to route traffic across backend instances.
- Use
minikube servicefor accessing services if NodePort is unavailable. - Perform load testing with Apache Benchmark to evaluate traffic distribution.
- Show the TA the logs of one backend Pod in the MiniKube dashboard.
- Explain to the TA how the load balancer distributes requests to backend instances.
- Show a screenshot of Apache Benchmark (AB) test output with performance metrics.
- Explain how this setup could be applied in Milestone 3.
- Start Docker: Ensure Docker is running on your machine.
- Fork Repo: Fork this repo
- Install MiniKube: Follow instructions here.
- Start MiniKube: Start MiniKube on your local setup. Run
minikube start. - Verify MiniKube: Confirm that MiniKube is running by listing all pods:
kubectl get po -A
- Download the Backend Code: Obtain the files
backend.pyandDockerfile.backend. - Edit the Flask Backend:
- Customize the response message in
backend.pyto include a unique identifier for each backend instance. - TODO: Add a unique message or identifier to distinguish responses from different backend instances.
- Containerize the Backend:
- Edit
Dockerfile.backendto set up the Flask app, exposing port 5001. - TODO: Replace
<your-dockerhub-username>with your Docker Hub username when creating the image.
- Build and Push the Docker Image:
docker build -t <your-dockerhub-username>/<backend-image-name>:1.0.0 -f Dockerfile.backend .
docker push <your-dockerhub-username>/<backend-image-name>:1.0.0
- Configure Deployment and Service:
- Edit
backend-deployment.yamlandbackend-service.yaml. - TODO: In the YAML files, replace placeholders with your Docker Hub image and specify unique service names if needed.
- Deploy the Backend:
kubectl apply -f backend-deployment.yaml
kubectl apply -f backend-service.yaml
- Download Load Balancer Code: Obtain
load_balancer.pyandDockerfile.loadbalancer. - Edit the Load Balancer Application:
- Customize
load_balancer.pyto use round-robin distribution across backend instances. - TODO: Add backend URLs for round-robin distribution and retrieve
user_idfrom query parameters.
- Containerize the Load Balancer:
- Edit
Dockerfile.loadbalancer. - TODO: Replace
<your-dockerhub-username>with your Docker Hub username when creating the image.
- Build and Push the Docker Image:
docker build -t <your-dockerhub-username>/<load-balancer-image-name>:1.0.0 -f Dockerfile.loadbalancer .
docker push <your-dockerhub-username>/<load-balancer-image-name>:1.0.0
- Edit Load Balancer Deployment and Service Files:
- Edit
loadbalancer-deployment.yamlandloadbalancer-service.yaml. - TODO: Specify your Docker Hub image in the YAML files, and ensure unique service names if deploying multiple services.
- Deploy the Load Balancer:
kubectl apply -f loadbalancer-deployment.yaml
kubectl apply -f loadbalancer-service.yaml
- Access via NodePort:
- Get the MiniKube IP:
minikube ip - Access the load balancer using
curl(replace<minikube-ip>with the output fromminikube ip):curl "http://<minikube-ip>:30080/?user_id=Alice"
- Use
minikube service(If NodePort Does Not Work):
- Create a tunnel to the load balancer service:
minikube service flask-load-balancer-service - This command will provide a URL, typically in the format
http://127.0.0.1:<some-port>, which you can use to test the load balancer. - Send multiple requests and see how each request is being served by a different instance. Show this to the TA.
- Launch MiniKube Dashboard:
- Open the MiniKube dashboard to monitor the status of Pods, deployments, and services:
minikube dashboard
- Run Basic AB Test:
- Use Apache Benchmark to send a series of requests to the load balancer:
ab -n 100 -c 10 "http://127.0.0.1:<some-port>/?user_id=Alice" - TODO: Replace
<some-port>with the port provided byminikube service. - This test sends 100 requests (
-n 100) with 10 concurrent connections (-c 10).
- Perform High-Load AB Test (Optional):
- Increase the request count and concurrency level to simulate high traffic:
ab -n 1000 -c 50 "http://127.0.0.1:<some-port>/?user_id=Alice" - Observe load balancer performance under high load and check backend Pod distribution.
- Observe Load Balancer Logs (Optional):
-
Monitor the backend logs in real-time to verify that the load balancer distributes requests evenly:
kubectl logs -l app=flask-backend -f
- Minikube IP Issues: Use
minikube ipto verify the correct IP. - Service Not Accessible: If NodePort does not work, use
minikube serviceto create a tunnel. - Backend Logs: Use
kubectl logs -l app=flask-backend -fto monitor requests going to the backend. - Load Balancer Logs: Check the load balancer logs to see the request distribution:
kubectl logs -l app=flask-load-balancer -f - Image Pull Issues: Ensure your Docker images are pushed to Docker Hub with the correct tags. More Details