๐ŸŒ Simple Web Load Balancer Using Vagrant and HAProxy
francotel

francotel @francotel

Location:
Austin, Texas
Joined:
Mar 11, 2020

๐ŸŒ Simple Web Load Balancer Using Vagrant and HAProxy

Publish Date: Mar 2
0 0

๐Ÿ›  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! ๐Ÿ˜Š

Comments 0 total

    Add comment