The s0.d1.small server is ideal for demonstrating HAProxy configuration and testing load balancing. In this tutorial, HAProxy runs on the server alongside three lightweight local web servers that simulate backend services.
For production deployments, choose a server configuration based on expected traffic, connection counts, and resource requirements.
Prerequisites
Log in to the BMC portal (if you are a new user, set up your account). You will also need a deployed s0.d1.small BMC server running Ubuntu. Connect to your server via SSH or remote console.
Step 1: Install HAProxy
HAProxy receives incoming requests and forwards them to the backend servers according to its configuration.
1. Add the HAProxy repository:
sudo add-apt-repository ppa:vbernat/haproxy-1.8Code language: Bash (bash)
Press Enter when asked to add the repository.
2. Update the package list:
sudo apt-get updateCode language: Bash (bash)
3. Install HAProxy:
sudo apt-get install haproxyCode language: Bash (bash)
Step 2: Create Test Web Servers
Create three local web servers to simulate backend services. Each server returns a different page so you can identify which backend handled a request.
1. Create three directories:
mkdir server{1..3}Code language: Bash (bash)
2. Create an index.html file in each directory:
touch server{1..3}/index.htmlCode language: Bash (bash)
3. Add a distinct HTML page to each file. For example:
sudo vi server1/index.htmlCode language: Bash (bash)
Add the following content:
<!DOCTYPE html>
<html>
<head>
<title>Server 1</title>
</head>
<body>Connected!</body>
</html>
Code language: HTML, XML (xml)
4. Repeat the process for server2/index.html and server3/index.html. Change the title and body text to identify each server.
Step 3: Configure HAProxy Load Balancer
1. Edit the HAProxy configuration file (/etc/haproxy/haproxy.cfg) and add the following configuration:
defaults
mode http
timeout client 10s
timeout connect 5s
timeout server 10s
timeout http-request 10s
frontend myfrontend
bind 127.0.0.1:80
default_backend myservers
backend myservers
balance roundrobin
server server1 127.0.0.1:8000
server server2 127.0.0.1:8001
server server3 127.0.0.1:8002
Code language: Properties (properties)
The configuration has three sections:
defaults. The shared settings defined in all the following sections. The section sets the wait times to help avoid common connection problems.frontend. Defines the address and port where HAProxy receives requests. Thedefault_backendline points to where the load balancer should relay the connections.backend. Stores the server addresses and ports for multiple servers, defined in the following step. The load balancing algorithm is set to round robin.
Save the file.
2. Restart HAProxy to apply the configuration:
sudo systemctl restart haproxyCode language: Bash (bash)
Step 4: Run the Test Servers and Verify Load Balancer
Run each test web server in a separate terminal session:
1. Open three additional terminal tabs and connect to the same BMC server in each tab:
ssh ubuntu@[PUBLIC_IP]Code language: Bash (bash)
2. In each session, navigate to a different directory and start the web server.
Session 1:
python3 -m http.server 8000 --bind 127.0.0.1Code language: Bash (bash)
Session 2:
python3 -m http.server 8001 --bind 127.0.0.1Code language: Bash (bash)
Session 3:
python3 -m http.server 8002 --bind 127.0.0.1Code language: Bash (bash)
Keep these processes running.
3. In your original terminal session, send a request to HAProxy:
curl 127.0.0.1:80Code language: Bash (bash)
Repeat the command several times. The response identifies different backend packages as HAProxy distributes new requests using round-robin balancing.