Skip to content

vivadata/Kube-Demo

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Kubernetes Load Balancer Demo

A simple FastAPI application that returns the container ID it runs in, perfect for demonstrating load balancing in Docker and Kubernetes.

Features

  • Returns the container/host identifier on each request
  • Health check endpoint
  • Ready for Docker and Kubernetes deployment

Quick Start

Build the Docker Image

docker build -t kube-demo:latest .

Run in Docker

docker run -p 8000:8000 kube-demo:latest

Test the Application

curl http://localhost:8000

You should see a response like:

{
  "container_id": "abc123def456",
  "hostname": "abc123def456",
  "message": "Request served by container: abc123def456",
  "counter": 1
}

Kubernetes Deployment

Prerequisites

  • Kubernetes cluster (Minikube, Kind, or any Kubernetes cluster)
  • kubectl configured to connect to your cluster

Launch Minikube (if using Minikube)

minikube start

Build and Push Image to GCP Registry

  1. Configure your .env file with GCP registry details:
REGISTRY=gcr.io/YOUR_PROJECT_ID
# or for Artifact Registry:
# REGISTRY=REGION-docker.pkg.dev/YOUR_PROJECT_ID/REPO_NAME
IMAGE_NAME=kube-demo
IMAGE_TAG=latest
  1. Authenticate with GCP:
gcloud auth configure-docker
  1. Build and push the image:
make push_gcp

This will build and push the image to your GCP container registry.

Deploy the Application

The deployment will pull the image from your GCP registry. Make sure deployment.yaml uses the same image name as in your .env file, or update it manually:

# Update deployment.yaml with your GCP image, then:
kubectl apply -f deployment.yaml

Or use the Makefile to deploy (automatically uses image from .env):

make deploy

Verify Deployment

Check that pods are running:

kubectl get pods -l app=kube-demo

You should see 3 pods running (as configured with replicas: 3).

Access the Application

Using Minikube

# Get the service URL
minikube service kube-demo-service --url

# Or access directly via NodePort
curl $(minikube ip):30080

Using kubectl port-forward

kubectl port-forward service/kube-demo-service 8000:8000

Then test:

curl http://localhost:8000

Test Load Balancing

Make multiple requests to see different container IDs:

# Using NodePort (Minikube)
for i in {1..10}; do
  curl $(minikube ip):30080
  echo ""
done

# Or using port-forward
for i in {1..10}; do
  curl http://127.0.0.1:57825/
  echo ""
done

Each request will be load balanced across the 3 pods, and you'll see different container_id values in the responses, demonstrating that Kubernetes is distributing requests across multiple instances.

View Pod Logs

To see which pod handled each request:

# View logs from all pods
kubectl logs -l app=kube-demo --tail=10

# View logs from a specific pod
kubectl logs <pod-name>

Clean Up

kubectl delete -f deployment.yaml

If using Minikube:

minikube stop

About

Demo for the kubernetes lecture

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published