Lädt...

🔧 🌐 Simple Web Load Balancer Using Vagrant and HAProxy


Nachrichtenbereich: 🔧 Programmierung
🔗 Quelle: dev.to

🛠 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

🏗 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

⚙️ 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.

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/

📋 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:

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

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

🤝 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! 😊

...

🔧 🌐 Simple Web Load Balancer Using Vagrant and HAProxy


📈 71.96 Punkte
🔧 Programmierung

📰 Load Balancer: HAProxy 2.0 bringt Neuerungen für HTTP und die Cloud


📈 40.94 Punkte
📰 IT Nachrichten

🔧 Automate Application Load Balancers With AWS Load Balancer Controller and Ingress


📈 35.9 Punkte
🔧 Programmierung

🐧 Vermin (a modern CLI for vagrant) - Now supports managing Vagrant Boxes


📈 33.71 Punkte
🐧 Linux Tipps

🕵️ Hashicorp vagrant-vmware-fusion up to 5.0.2 Vagrant Update Process privilege escalation


📈 33.71 Punkte
🕵️ Sicherheitslücken

🕵️ Hashicorp vagrant-vmware-fusion bis 5.0.2 Vagrant Update Process erweiterte Rechte


📈 33.71 Punkte
🕵️ Sicherheitslücken

🐧 HAProxy Basics - The Four Essential Sections of an HAProxy Configuration (and how to use them)


📈 33.3 Punkte
🐧 Linux Tipps

🔧 Building a Simple Load Balancer with Spring Boot: A Step-by-Step Guide


📈 31.03 Punkte
🔧 Programmierung

🔧 Building a simple load balancer in Go


📈 31.03 Punkte
🔧 Programmierung

🔧 Building a Simple Load Balancer in Go


📈 31.03 Punkte
🔧 Programmierung

🔧 Ultimate Guide to Using NGINX as a Reverse Proxy and Load Balancer:Best Practices and Tips


📈 30.86 Punkte
🔧 Programmierung

🔧 Ultimate Guide to Using NGINX as a Reverse Proxy and Load Balancer:Best Practices and Tips


📈 30.86 Punkte
🔧 Programmierung

🔧 Creating a reverse proxy and load balancer in 2 minutes using Caddy


📈 29.72 Punkte
🔧 Programmierung

🔧 Implementing Path based routing with Application Load Balancer ALB and EC2 Instances using Terraform


📈 29.72 Punkte
🔧 Programmierung

🔧 Deloy SocketIO Server Using Docker and Nginx Load Balancer (+SSL)


📈 29.72 Punkte
🔧 Programmierung

🔧 Load Balancing vs Load Shedding vs Load Leveling


📈 29.7 Punkte
🔧 Programmierung

🔧 AWS Elastic Load Balancer (ELB) Tutorial: How to Configure ELB and Add Web Servers


📈 29.14 Punkte
🔧 Programmierung

🔧 Building Highly Available and Scalable Web Applications with AWS Elastic Load Balancer


📈 29.14 Punkte
🔧 Programmierung

🔧 Using Caddy to Serve Static Files Behind a Load Balancer


📈 28.58 Punkte
🔧 Programmierung

🔧 Getting the Actual Client IP When Using Application Load Balancer (ALB) in AWS Lambda


📈 28.58 Punkte
🔧 Programmierung

🔧 Deploy Go Application using Docker Compose with Nginx Load Balancer


📈 28.58 Punkte
🔧 Programmierung

🔧 How to implement a Load Balancer Using Nginx &amp; Docker


📈 28.58 Punkte
🔧 Programmierung

🔧 Azure Load Balancer insights using Azure Monitor for Networks | Azure Friday


📈 28.58 Punkte
🔧 Programmierung

⚠️ Barracuda Web App Firewall / Load Balancer Remote Root


📈 28 Punkte
⚠️ PoC

⚠️ Barracuda Web App Firewall/Load Balancer Post Auth Remote Root Exploit (3)


📈 28 Punkte
⚠️ PoC

⚠️ Barracuda Web App Firewall/Load Balancer Post Auth Remote Root Exploit (2)


📈 28 Punkte
⚠️ PoC

🔧 Configuring a Load Balancer for Your Web Application: A Comprehensive Guide


📈 28 Punkte
🔧 Programmierung

matomo