DEV Community

Cover image for 🌐 Simple Web Load Balancer Using Vagrant and HAProxy
francotel
francotel

Posted on

🌐 Simple Web Load Balancer Using Vagrant and HAProxy

πŸ›  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!

loadbalancer-v1

πŸ“‚ Project Structure

load-balancer-demo/ │── Vagrantfile │── provisioning/ β”‚ β”œβ”€β”€ haproxy.sh β”‚ β”œβ”€β”€ webserver.sh 
Enter fullscreen mode Exit fullscreen mode

πŸ— Step 1: Install Vagrant & VirtualBox

First, install Vagrant and VirtualBox on your system:

πŸ”Ή Download & Install:

Vagrant
VirtualBox

Once installed, check versions:

vagrant --version VBoxManage --version 
Enter fullscreen mode Exit fullscreen mode

βš™οΈ 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 
Enter fullscreen mode Exit fullscreen mode

🌐 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 
Enter fullscreen mode Exit fullscreen mode

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 
Enter fullscreen mode Exit fullscreen mode

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 
Enter fullscreen mode Exit fullscreen mode
❯ 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`. 
Enter fullscreen mode Exit fullscreen mode

To check the HAProxy load balancer:

curl http://192.168.56.10 
Enter fullscreen mode Exit fullscreen mode

πŸ’‘ You should see alternating responses from Web Server 1 and Web Server 2.

responses

πŸ“Š 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/ 
Enter fullscreen mode Exit fullscreen mode

πŸ“‹ Example Output:

Concurrency Level: 20 Time taken for tests: 2.345 seconds Complete requests: 1000 Failed requests: 0 Requests per second: 426.45 [#/sec] 
Enter fullscreen mode Exit fullscreen mode

πŸ”Ή 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 
Enter fullscreen mode Exit fullscreen mode

Restart HAProxy:

vagrant reload --provision 
Enter fullscreen mode Exit fullscreen mode

Now visit http://192.168.56.10:8080/stats and log in with:

haproxy-stats

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 
Enter fullscreen mode Exit fullscreen mode

vagrant-destroy

πŸŽ‰ 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 
Enter fullscreen mode Exit fullscreen mode

🀝 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!

LinkedIn

If you’d like to support my work, you can buy me a coffee. Thank you for your support!

BuyMeACoffee

Thank you for reading! 😊

Top comments (0)