π Introduction
Load balancers help distribute traffic across multiple servers, improving performance, availability, and scalability. In this guide, weβll set up a simple web load balancer using Vagrant, VirtualBox, and HAProxy.
We will:
β
Set up two web servers using Nginx
β
Deploy HAProxy to balance traffic
β
Test load balancing with Apache Benchmark (ab)
β
Enable HAProxy Stats Page for monitoring
π Letβs get started!
π Project Structure
load-balancer-demo/ βββ Vagrantfile βββ provisioning/ β βββ haproxy.sh β βββ webserver.sh
π Step 1: Install Vagrant & VirtualBox
First, install Vagrant and VirtualBox on your system:
πΉ Download & Install:
Once installed, check versions:
vagrant --version VBoxManage --version
βοΈ Step 2: Create a Vagrantfile
In your project folder (load-balancer-demo/), create a file named Vagrantfile and add:
Vagrant.configure("2") do |config| # Web Server 1 config.vm.define "web1" do |web1| web1.vm.box = "ubuntu/focal64" config.vm.box_version = "20240821.0.1" web1.vm.network "private_network", ip: "192.168.56.11" web1.vm.provision "shell", path: "provisioning/webserver.sh" end # Web Server 2 config.vm.define "web2" do |web2| web2.vm.box = "ubuntu/focal64" config.vm.box_version = "20240821.0.1" web2.vm.network "private_network", ip: "192.168.56.12" web2.vm.provision "shell", path: "provisioning/webserver.sh" end # HAProxy Load Balancer config.vm.define "haproxy" do |haproxy| haproxy.vm.box = "ubuntu/focal64" config.vm.box_version = "20240821.0.1" haproxy.vm.network "private_network", ip: "192.168.56.10" haproxy.vm.provision "shell", path: "provisioning/haproxy.sh" end end
π Step 3: Provision Web Servers
Create a provisioning script for Nginx Web Servers:
π provisioning/webserver.sh
#!/bin/bash sudo apt update && sudo apt install -y nginx echo "<h1>Web Server $(hostname -I | awk '{print $2}')</h1>" | sudo tee /var/www/html/index.html sudo systemctl restart nginx
This script installs Nginx and creates a basic webpage showing the server name.
βοΈ Step 4: Provision HAProxy Load Balancer
π provisioning/haproxy.sh
#!/bin/bash sudo apt update && sudo apt install -y haproxy # HAProxy Configuration cat <<EOF | sudo tee /etc/haproxy/haproxy.cfg frontend http_front bind *:80 default_backend web_servers backend web_servers balance roundrobin server web1 192.168.56.11:80 check server web2 192.168.56.12:80 check EOF sudo systemctl restart haproxy
This config:
β
Listens on port 80
β
Balances traffic between two web servers
π Step 5: Start the Virtual Machines
Run the following command to start all virtual machines:
vagrant up
β― vagrant status Current machine states: web1 running (virtualbox) web2 running (virtualbox) haproxy running (virtualbox) This environment represents multiple VMs. The VMs are all listed above with their current state. For more information about a specific VM, run `vagrant status NAME`.
To check the HAProxy load balancer:
curl http://192.168.56.10
π‘ You should see alternating responses from Web Server 1 and Web Server 2.
π Step 6: Generate Traffic with Apache Benchmark (ab)
Run the following command to simulate 1000 requests with 20 concurrent users:
ab -n 1000 -c 20 http://192.168.56.10/
π Example Output:
Concurrency Level: 20 Time taken for tests: 2.345 seconds Complete requests: 1000 Failed requests: 0 Requests per second: 426.45 [#/sec]
πΉ This confirms HAProxy is distributing traffic correctly!
π‘ Step 7: Enable HAProxy Stats Page
π Add this to haproxy.sh before restarting HAProxy:
listen stats bind *:8080 stats enable stats uri /stats stats refresh 5s stats auth admin:password
Restart HAProxy:
vagrant reload --provision
Now visit http://192.168.56.10:8080/stats and log in with:
Here, you can monitor:
β
Requests per second
β
Active connections
β
Server health
β Step 9: Destroy Everything
When you're done, clean up everything with:
vagrant destroy -f
π Final Thoughts
π Now you have:
β
A working HAProxy load balancer
β
Traffic simulation using Apache Benchmark
β
A real-time monitoring UI
π‘ Next Steps?
Try different balancing algorithms (roundrobin, leastconn, source)
Test with more web servers
Deploy it on a real cloud environment
1οΈβ£ Clone the repository:
git clone https://github.com/francotel/loadbalancer-haproxy-vagrant.git cd loadbalancer-haproxy-vagrant vagrant up
π€ Let's Connect!
If you find this repository useful and want to see more content like this, follow me on LinkedIn to stay updated on more projects and resources!
If youβd like to support my work, you can buy me a coffee. Thank you for your support!
Thank you for reading! π
Top comments (0)