Skip to content

About

No description, website, or topics provided.

Resources

Stars

0 stars

Watchers

1 watching

Forks

Repository files navigation

🔗 Load-Balanced URL Shortener using Docker & Kubernetes

The URL Shortener is a scalable, containerized service that allows users to shorten long URLs and seamlessly redirect to the original ones. Built using Flask and Redis, and deployed via Docker and Kubernetes, the project is designed for horizontal scalability and high availability. Features like Ingress routing, Horizontal Pod Autoscaling, and Redis-based persistence make it production-ready and cloud-native.


📜 Overview

The system is composed of microservices orchestrated through Kubernetes:

  • Flask App: Handles API requests to shorten and expand URLs.
  • Redis: Stores mappings between short and long URLs.
  • Docker: Used to containerize the application.
  • Kubernetes: Handles deployment, scaling, and networking.
  • Ingress Controller: Exposes the application via a custom domain.
  • HPA (Horizontal Pod Autoscaler): Scales application pods automatically based on resource usage.

🚀 Features

  • 🔗 Generate and manage short URLs.
  • 📦 Fully containerized with Docker.
  • ☁️ Deployed using Kubernetes for easy scalability.
  • ⚖️ Load balanced using Kubernetes Ingress.
  • 🔁 Automatically scaled using HPA and Metrics Server.
  • 🔐 Secrets and ConfigMaps for secure and configurable deployments.

🛠 Setup Instructions

1. Docker Build and Push (Optional)

Build the image locally:

docker build -t url-shortener:latest .

Login to Docker Hub and push your image:

docker login
docker tag url-shortener:latest shivadarshan/url-shortener:latest
docker push shivadarshan/url-shortener:latest

2. Kubernetes Deployment

Apply all the Kubernetes manifests:

kubectl apply -f redis-deployment.yaml
kubectl apply -f redis-service.yaml
kubectl apply -f url-shortener-deployment.yaml
kubectl apply -f url-shortener-service.yaml
kubectl apply -f configmap.yaml
kubectl apply -f redis-secret.yaml

3. Verify Redis Connection

Check if Redis pods are running:

kubectl get pods -l app=redis

Enter the Redis container:

kubectl exec -it <redis-pod-name> -- sh

Access Redis CLI:

redis-cli -n 0
AUTH PESU123#

Replace PESU123# with your actual Redis password from the Kubernetes secret.


📊 Autoscaling with HPA

1. Enable Metrics Server

minikube addons enable metrics-server

2. Create Horizontal Pod Autoscaler

kubectl autoscale deployment url-shortener \
  --cpu-percent=50 \
  --min=3 \
  --max=7

Check the autoscaler:

kubectl get hpa

🌐 Enable Ingress Routing

1. Enable Ingress Addon

minikube addons enable ingress

2. Apply Ingress Configuration

Apply your Ingress resource:

kubectl apply -f url-shortener-ingress.yaml

3. Set Up Domain

Get your Minikube IP:

minikube ip

Edit your /etc/hosts file:

sudo nano /etc/hosts

Add the following line:

<minikube-ip> url-shortener.local

Now your website will be available at:

http://url-shortener.local

🧪 Stress Testing the URL Shortener

Use the included script stress_test_shortener.py to simulate multiple users generating short URLs concurrently.

1. Prerequisites

Install Python dependencies:

pip install requests beautifulsoup4

2. Run the Stress Test

python stress_test_shortener.py

This will:

  • Simulate 200 users
  • Each sending 100 requests
  • Randomly generating long URLs
  • Printing responses (shortened URLs or errors)

Make sure the app is accessible at http://url-shortener.local and Ingress is working before running the script.


🔐 Handling Secrets

Base64 encode your Redis password:

echo -n "password" | base64

Paste the encoded string into redis-secret.yaml.


🧰 Helpful Kubernetes Commands

List all pods:

kubectl get pods

Check logs of a specific pod:

kubectl logs <pod-name>

List services:

kubectl get svc

View deployments:

kubectl get deployments

Describe a specific pod:

kubectl describe pod <pod-name>

Delete a Kubernetes resource:

kubectl delete -f <filename>.yaml

👨‍💻 Author


📜 License

This project is licensed under the MIT License.


💬 Feedback and Support

For suggestions or issues, feel free to open an issue on GitHub:

Open an Issue


Thank you for checking out the URL Shortener project! 🎉

About

No description, website, or topics provided.

Resources

Stars

0 stars

Watchers

1 watching

Forks

Releases

Packages

Used by

Contributors

Languages