Skip to content

Bhuvan-08/LoadBalancedURLShortner

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

SnipBalancer

Load-Balanced URL Shortener

This project is a URL shortener service designed to efficiently generate short URLs from long URLs via a simple web interface. The application is built using a Flask backend that manages endpoints for URL creation. Key features of this project include:

  • Fast URL Resolution: Shortened URLs are stored in a Redis database, ensuring rapid lookup times.
  • High Availability & Scalability: The service is deployed with Docker and orchestrated using Kubernetes. This setup uses three replicated instances for the Flask application along with Redis Sentinel for enhanced database redundancy.
  • Load Balancing & Auto-Scaling: Kubernetes manages container orchestration, enabling automatic scaling and load balancing to maintain performance during traffic spikes.

Week 1

Week 1: To Build the URL Shortener in Docker

1. Set Up a Python Virtual Environment

python3 -m venv venv
source venv/bin/activate

2. Create a Docker Network for Redis

docker network create redis

3. Prepare the Clustering Environment (Navigate to the clustering directory)

chmod +x script.sh
./script.sh
docker pull redis:7.4.2-alpine

4. Build and Run the SnipBalancer Docker Image

Build the image (development target):

docker build --target dev -t snipbal .

Or build the default image:

docker build -t snipbal .

Run the container:

docker run -it -v "${PWD}:/work" -p 5000:5000 \
    --net redis \
    -e REDIS_SENTINELS="sentinel-0:5000,sentinel-1:5000,sentinel-2:5000" \
    -e REDIS_MASTER_NAME="mymaster" \
    -e REDIS_PASSWORD="okok" \
    snipbal

5. Check Redis Status

Access a Redis container shell:

docker exec -it redis-0 sh

Connect with Redis CLI:

redis-cli
auth <password>
KEYS *
GET <key_val>

Notes

  • Replace <password> and <key_val> with your password and desired Redis key.
Week 2

Week 2: Deploy the URL Shortener using Kubernetes

1. Create a Kubernetes Cluster with Kind

kind create cluster --name redis --image kindest/node:v1.23.5

2. Create a Namespace for Redis

kubectl create ns redis

3. Deploy Redis Cluster

Navigate to the redis/kubernetes/ directory and apply the configuration files:

kubectl apply -n redis -f ./redis/redis-configmap.yaml
kubectl apply -n redis -f ./redis/redis-statefulset.yaml

Check the status of pods and persistent volumes:

kubectl -n redis get pods
kubectl -n redis get pv

4. Verify Redis Cluster

Access the Redis pod shell:

kubectl -n redis exec -it redis-0 -- sh

Connect to Redis CLI and check replication status:

redis-cli
auth <your-redis-password>
info replication

View logs for Redis pods:

kubectl -n redis logs redis-0
kubectl -n redis logs redis-1
kubectl -n redis logs redis-2

5. Deploy Redis Sentinel

Apply the Sentinel StatefulSet:

kubectl apply -n redis -f ./sentinel/sentinel-statefulset.yaml

Check Sentinel pods and logs:

kubectl -n redis get pods
kubectl -n redis get pv
kubectl -n redis logs sentinel-0

6. Deploy SnipBalancer Application

Navigate to the /redis/kubernetes/app/ directory and deploy the application:

kubectl apply -n redis -f app-deployment.yaml
kubectl apply -n redis -f app-configmap.yaml
kubectl apply -n redis -f app-secret.yaml

7. Verify Application Deployment

Check if the SnipBalancer pods are running:

kubectl get pods -n redis -l app=snipbal

Check deployment and service status:

kubectl get deployment -n redis snipbal
kubectl get service -n redis snipbal

8. Access the Application

Port-forward the SnipBalancer service to your local machine:

kubectl port-forward -n redis service/snipbal 5000:5000

9. Debugging and Logs

Get the names of SnipBalancer pods:

kubectl get pods -n redis -l app=snipbal

Check logs for a specific pod:

kubectl logs -n redis <pod-name>

10. Interact with Redis

Access the Redis CLI from a pod:

kubectl exec -it -n redis redis-0 -- redis-cli

Authenticate and interact with Redis:

auth <your-redis-password>
KEYS *
GET <key_name>

11. Testing Failover and High Availability

To verify Redis Sentinel failover and cluster availability:

  1. Check Current Redis Master:

    kubectl exec -n redis sentinel-0 -- redis-cli -p 5000 SENTINEL get-master-addr-by-name mymaster
  2. Simulate Master Failure:

    kubectl delete pod -n redis redis-0
  3. Monitor Sentinel Logs for Failover Events:

    kubectl logs -f -n redis sentinel-0
  4. Check Pod Status and Master Re-election:

    kubectl -n redis get pods -o wide
  5. Verify New Master:

    • Repeat step 1 to confirm which Redis pod is now the master.
    • You can also refer to step 4 above to check the roles of redis-0, redis-1, and redis-2.

These steps help ensure your Redis cluster remains available and automatically recovers from node failures.

Notes

  • Replace <password> and <key_val> with your password and desired Redis key.
Week 3

Week 3: Scaling, Load Balancing & Monitoring

1. Enable Horizontal Pod Autoscaling (HPA)

Navigate to the /redis/kubernetes/app directory and apply the HPA configuration:

kubectl apply -f app-hpa.yaml -n redis

2. Deploy Metrics Server

Navigate to the /redis/kubernetes/metric-server directory and deploy the metrics server:

kubectl apply -f components.yaml -n redis

3. Generate Load for Autoscaling

Start a temporary load generator pod:

kubectl run -n redis -it --rm load-generator --image=busybox -- /bin/sh

Inside the load-generator pod shell, run the following command to continuously send requests to the SnipBalancer service:

while true; do wget -q -O- http://snipbal:5000; done

You can exit the load generator at any time by pressing Ctrl + C.

4. Monitor Autoscaling Activity

In separate terminals, monitor the status of pods and the HPA:

kubectl get pods -n redis --watch
kubectl get hpa -n redis --watch

Observe as the HPA scales the number of SnipBalancer pods up and down in response to the generated load. After stopping the load generator, the number of replicas will decrease following the HPA cooldown period (typically 10–15 minutes).

Note: The cooldown period is a standard HPA property and may vary based on your configuration.

Stress Testing

To perform stress testing on the SnipBalancer application, follow these steps:

  1. Navigate to the stress-tests directory:

    cd stress-tests
  2. Make the test scripts executable:

    chmod +x get-test.sh
    chmod +x post-test.sh
  3. Run the Application and Start Stress Tests

kubectl port-forward -n redis service/snipbal 5000:5000

Then, in a separate terminal, run a stress test:

# For GET requests:
./get-test.sh

# Or for POST requests:
./post-test.sh
  1. Monitor Scaling Activity in Real Time

Open another terminal and watch the scaling behavior:

kubectl get pods -n redis --watch
kubectl get hpa -n redis --watch

These steps will help you observe how the application automatically scales up and down in response to increased load.

Reference:
This project setup was taken from this guide.

About

SnipBalancer is a cloud-native, load-balanced URL shortener built with Flask and Redis. It ensures fast URL resolution, high availability using Redis Sentinel, and scalable deployments through Docker and Kubernetes with load balancing and horizontal pod autoscaling.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors