Skip to content

Cluster HAProxy Load Balancing ​

1. Installing HAProxy ​

Preparing the HAProxy Configuration File ​

First prepare haproxy.cfg

A ready-made file is located at a-部署/负载均衡/haproxy/haproxy.cfg

shell
global
    # Log to local3 at info level, address 127.0.0.1
    log 127.0.0.1 local3 info
    # Run as a daemon
    daemon
    # Maximum connections set to 100000
    maxconn 100000
defaults
    # Apply the global log settings
    log global
    # Mode is TCP
    mode tcp
    # Enable TCP logging
    option tcplog
    # Do not enable the dontlognull option
    #option dontlognull
    # Connection timeout set to 10000 milliseconds
    timeout connect 10000
    # Note: timeout > mqtt's keepalive * 1.2
    # timeout > mqtt's keepalive * 1.2
    # Client timeout set to 240 seconds
    timeout client 240s
    # Server timeout set to 240 seconds
    timeout server 240s
    # Maximum connections set to 50000
    maxconn 50000

backend mqtt_backend
    # Mode is TCP
    mode tcp
    # Sticky session load balancing settings
    stick-table type string len 32 size 1000k expire 30m
    # Sticky sessions based on the request payload and the mqtt field value
    stick on req.payload(0,0),mqtt_field_value(connect,client_identifier)

    # Node A internal IP
    server fastbeeA 10.x.x.x:1884 check
    # Node B internal IP
    server fastbeeB 10.x.x.x:1884 check

frontend mqtt_servers
    # Bind to port 1884 on all addresses
    bind *:1883
    # Mode is TCP
    mode tcp
    # Wait for the buffer to fill in order to parse MQTT packets, delay 10 seconds
    tcp-request inspect-delay 10s
    # Reject non-MQTT connections
    tcp-request content reject unless { req.payload(0,0),mqtt_is_valid }
    # Set the default backend to mqtt_backend
    default_backend mqtt_backend

backend mqtt_ws_backend
  mode tcp
  balance roundrobin
    # Node A internal IP
    server fastbeeA 10.x.x.x:8084 check
    # Node B internal IP
    server fastbeeB 10.x.x.x:8084 check

frontend mqtt_ws_frontend
  bind *:8083
  mode tcp
  default_backend mqtt_ws_backend

Locate server fastbeeA and server fastbeeB and modify the internal IP addresses

2. Installation via docker-compose ​

Copy haproxy.cfg to /opt/haproxy

A ready-made file is in the project at a-部署/负载均衡/haproxy/docker-compose安装/docker-compose.yml

yml
version: '3'
services:
  haproxy:
    image: haproxy:latest
    ports:
      - "8083:8083"
      - "1883:1883"
    volumes:
      - /opt/haproxy/haproxy.cfg:/usr/local/etc/haproxy/haproxy.cfg:ro
    restart: always

Run the docker-compose command

3. CentOS ​

A. Installing with the Package Manager (yum) ​

1. Update the system package list

Open a terminal and run the following command to update the system package list, ensuring that the latest package information is available.

bash
sudo yum update -y

2. Install HAProxy

Use the yum command to install HAProxy.

bash
sudo yum install haproxy -y

3. Start the HAProxy service

After installation, use the following command to start the HAProxy service.

bash
sudo systemctl start haproxy

4. Enable auto-start on boot

To make HAProxy start automatically when the system boots, run the following command.

bash
sudo systemctl enable haproxy

5. Check the service status

You can use the following command to check whether the HAProxy service is running normally.

bash
sudo systemctl status haproxy

B. Building from Source ​

1. Install build dependencies

First, install the tools and dependency libraries required for building.

bash
sudo yum install -y gcc make pcre-devel zlib-devel systemd-devel

2. Download the HAProxy source code

Download the latest source package from the official HAProxy website; HAProxy 2.6.14 is used here as an example.

bash
wget https://www.haproxy.org/download/2.6/src/haproxy-2.6.14.tar.gz

3. Extract the source package

Use the following command to extract the downloaded source package.

bash
tar -zxvf haproxy-2.6.14.tar.gz
cd haproxy-2.6.14

4. Build and install

Run the following commands in sequence to build and install.

bash
make TARGET=linux-glibc USE_OPENSSL=1 USE_ZLIB=1 USE_PCRE=1 USE_SYSTEMD=1
sudo make install

5. Create the configuration file and service file

Create the HAProxy configuration directory and configuration file, and create the Systemd service file.

bash
sudo mkdir -p /etc/haproxy
sudo cp examples/haproxy.cfg /etc/haproxy/
sudo tee /etc/systemd/system/haproxy.service <<EOF
[Unit]
Description=HAProxy Load Balancer
After=network.target

[Service]
ExecStart=/usr/local/sbin/haproxy -f /etc/haproxy/haproxy.cfg
ExecReload=/usr/local/sbin/haproxy -f /etc/haproxy/haproxy.cfg -st \$MAINPID
Restart=always

[Install]
WantedBy=multi-user.target
EOF

6. Start the service and enable auto-start on boot

Reload the Systemd configuration, start the HAProxy service, and enable auto-start on boot.

bash
sudo systemctl daemon-reload
sudo systemctl start haproxy
sudo systemctl enable haproxy

4. Ubuntu ​

A. Installing with the Package Manager (apt) ​

1. Update the system package list

Open a terminal and run the following command to update the system package list.

bash
sudo apt update

2. Install HAProxy

Use the apt command to install HAProxy.

bash
sudo apt install haproxy -y

3. Start the HAProxy service

After installation, use the following command to start the HAProxy service.

bash
sudo systemctl start haproxy

4. Enable auto-start on boot

To make HAProxy start automatically when the system boots, run the following command.

bash
sudo systemctl enable haproxy

5. Check the service status

You can use the following command to check whether the HAProxy service is running normally.

bash
sudo systemctl status haproxy

B. Building from Source ​

1. Install build dependencies

First, install the tools and dependency libraries required for building.

bash
sudo apt install -y build-essential libpcre3-dev zlib1g-dev libssl-dev systemd-dev

2. Download the HAProxy source code

Download the latest source package from the official HAProxy website, using HAProxy 2.6.14 as an example.

bash
wget https://www.haproxy.org/download/2.6/src/haproxy-2.6.14.tar.gz

3. Extract the source package

Use the following command to extract the downloaded source package.

bash
tar -zxvf haproxy-2.6.14.tar.gz
cd haproxy-2.6.14

4. Build and install

Run the following commands in sequence to build and install.

bash
make TARGET=linux-glibc USE_OPENSSL=1 USE_ZLIB=1 USE_PCRE=1 USE_SYSTEMD=1
sudo make install

5. Create the configuration file and service file

Create the HAProxy configuration directory and configuration file, and create the Systemd service file.

bash
sudo mkdir -p /etc/haproxy
sudo cp examples/haproxy.cfg /etc/haproxy/
sudo tee /etc/systemd/system/haproxy.service <<EOF
[Unit]
Description=HAProxy Load Balancer
After=network.target

[Service]
ExecStart=/usr/local/sbin/haproxy -f /etc/haproxy/haproxy.cfg
ExecReload=/usr/local/sbin/haproxy -f /etc/haproxy/haproxy.cfg -st \$MAINPID
Restart=always

[Install]
WantedBy=multi-user.target
EOF

6. Start the service and enable auto-start on boot

Reload the Systemd configuration, start the HAProxy service, and enable auto-start on boot.

bash
sudo systemctl daemon-reload
sudo systemctl start haproxy
sudo systemctl enable haproxy

After installation, modify the /etc/haproxy/haproxy.cfg configuration file according to actual needs to implement different load balancing strategies.

FastBee | fastbee.cn