Skip to content

Tinobrace/Haproxy-LoadBalancing-Lab

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

19 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

===================================HAProxy Load Balancing Project================================

πŸ“Œ Project Overview

This project demonstrates how to configure HAProxy as a load balancer for Apache2 and Nginx web servers. It implements three load balancing algorithms and includes testing and troubleshooting steps to ensure optimal performance.

πŸš€ System Architecture

Component Internal IP External IP Port HAProxy (Load Balancer) 10.128.0.5 35.239.94.162 80 Apache2 Web Server 10.128.0.6 34.136.46.2 80 Nginx Web Server 10.128.0.7 34.31.83.112 80

πŸ”§ Installation & Configuration

1️⃣ Deploy Web Servers

πŸ”Ή Apache2 Setup (on apache2-server) > sudo apt update && sudo apt install apache2 -y > echo "

Apache2 Web Server

" | sudo tee /var/www/html/index.html > sudo systemctl restart apache2

πŸ”Ή Nginx Setup (on nginx-server) > sudo apt update && sudo apt install nginx -y > echo "

Nginx Web Server

" | sudo tee /var/www/html/index.html > sudo systemctl restart nginx

βœ… Verify Setup: > curl -I http://10.128.0.6/ # Apache2 > curl -I http://10.128.0.7/ # Nginx

2️⃣ Install & Configure HAProxy (on haproxy-lb)

πŸ”Ή Install HAProxy > sudo apt update && sudo apt install haproxy -y

πŸ”Ή Configure HAProxy Edit HAProxy config file > sudo nano /etc/haproxy/haproxy.cfg

Paste the following configuration:

global stats socket /run/haproxy/admin.sock mode 660 level admin

defaults mode http timeout connect 5s timeout client 30s timeout server 30s

frontend http_front bind *:80 use_backend backend_roundrobin if { hdr(host) -i roundrobin.local } use_backend backend_leastconn if { hdr(host) -i leastconn.local } use_backend backend_iphash if { hdr(host) -i iphash.local }

# HAProxy Statistics
stats enable
stats uri /haproxy?stats
stats refresh 5s
stats auth admin:haproxy123

backend backend_roundrobin balance roundrobin option httpchk GET / server web1 10.128.0.6:80 check server web2 10.128.0.7:80 check

backend backend_leastconn balance leastconn option httpchk GET / server web1 10.128.0.6:80 check server web2 10.128.0.7:80 check

backend backend_iphash balance source option httpchk GET / server web1 10.128.0.6:80 check server web2 10.128.0.7:80 check

βœ… Restart HAProxy: > sudo systemctl restart haproxy

βœ… Enable HAProxy at boot: > sudo systemctl enable haproxy

βœ… Check HAProxy status: > sudo systemctl status haproxy

πŸ“Š Testing & Validation

1️⃣ Test Load Balancing Algorithms

πŸ”Ή Round-Robin > curl -I -H "Host: roundrobin.local" http://35.239.94.162/

βœ… Expected: Alternates between Apache2 & Nginx.

πŸ”Ή Least Connections > curl -I -H "Host: leastconn.local" http://35.239.94.162/

βœ… Expected: Routes traffic to the server with the fewest active connections.

πŸ”Ή IP Hashing > curl -I -H "Host: iphash.local" http://35.239.94.162/

βœ… Expected: Same client IP is always routed to the same backend.

2️⃣ Load Testing (Apache Benchmark) > ab -n 1000 -c 50 -H "Host: roundrobin.local" http://35.239.94.162/

βœ… Expected: Requests are distributed across both servers.

3️⃣ Monitor HAProxy Performance

πŸ”Ή Check Backend Status > echo "show stat" | sudo socat unix-connect:/run/haproxy/admin.sock stdio

πŸ”Ή View HAProxy Logs > sudo journalctl -xeu haproxy | tail -n 20

πŸ”Ή Access HAProxy Web Dashboard > http://35.239.94.162/haproxy?stats

βœ… Login with: Username: admin Password: haproxy123

πŸ“Œ Final Testing

πŸ”Ή Verify Load Balancing

Run multiple requests:

for i in {1..10}; do curl -I http://haproxy-ip/; done

βœ… Expected: Some responses from Apache2, some from Nginx.

πŸ“Œ Conclusion βœ” HAProxy efficiently balances traffic across Apache2 and Nginx. βœ” Multiple load balancing algorithms ensure high availability. βœ” Health checks keep the system reliable. βœ” Testing & monitoring provide performance insights. βœ” Tested Load Distribution with Apache Benchmark (ab)

πŸ“œ Author πŸ‘€ Valentine Uchenna Ukah πŸ“§ val.ukah01@gmail.com πŸ“Œ GitHub: https://github.com/Tinobrace/Haproxy-LoadBalancing-Lab

About

This is a solid networking task focused on setting up and testing HAProxy as a Load Balancer

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors