Load balancing Web Application with HA Proxy

In previous post, I have demonstrated how to use NGINX as the load balancer to devide load between application servers. Beside NginX, another popular software load balancer is HAProxy. HAProxy supports for Layer 4 (TCP) and Layer 7 (HTTP) protocol for load balancing. HAProxy Community is an open source, so you can use it for free. In this post, I wll show how to utilize HAProxy as a load balancer for load balancing the traffic between application servers.

For my environment, I have set up as following

  • Server 1 : OS Ubuntu 20.4 LTS, Application Server Wildfly 10.1.0 Final
  • Server 2 : OS Ubuntu 20.4 LTS, Application Server Wildfly 10.1.0 Final
  • Server 3 : OS Ubuntu 20.4 LTS, HA Proxy Community Edition version 2.4

Table Contents

For the contents of this post, I have arranged as below.

  1. Install HA Proxy on Ubuntu Server
  2. Configuring HA Proxy as Load Balancer
  3. Testing HA Proxy for Load Balancing
  4. Reference

Let's see, How I have implemented the system as following.


1. Install HA Proxy on Ubuntu Server

First, I will update my repository's mirror.

Update Repository's Mirror
$sudo sed -i 's|http://us.|http://|g' /etc/apt/sources.list
$sudo apt update

To create load balancing by using HA Proxy, I firstly need to installl HA Proxy into my server. Installing HA Proxy is very easy, you could proceed as below.

Install HAProxy
$sudo apt install haproxy

Once you finished installing HAProxy, you could check version of HAProxy as below.

Check Version of HAProxy
$ haproxy -v
HA-Proxy version 2.0.13-2ubuntu0.3 2021/08/27 - https://haproxy.org/    

2. Configuring HA Proxy as Load Balancer

Next is to configure HA prox to route traffic from the client to each web application server. For my set up, I will apply simple routing algorithm like roundrobin to round the request to backend server. You can use others routing algorithms base on your requirements or enviroment's restriction. Below is how I configure my HAProxy.

Configuring HAProxy (Roundrobin)
$ sudo less /etc/haproxy/haproxy.cfg
global
        log /dev/log    local0
        log /dev/log    local1 notice
        chroot /var/lib/haproxy
        stats socket /run/haproxy/admin.sock mode 660 level admin expose-fd listeners
        stats timeout 30s
        user haproxy
        group haproxy
        daemon

        # Default SSL material locations
        ca-base /etc/ssl/certs
        crt-base /etc/ssl/private

        # See: https://ssl-config.mozilla.org/#server=haproxy&server-version=2.0.3&config=intermediate
        ssl-default-bind-ciphers ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-GCM-SHA384:ECDHE-ECDSA-CHACHA20-POLY1305:ECDHE-RSA-CHACHA20-POLY1305:DHE-RSA-AES128-GCM-SHA256:DHE-RSA-AES256-GCM-SHA384
        ssl-default-bind-ciphersuites TLS_AES_128_GCM_SHA256:TLS_AES_256_GCM_SHA384:TLS_CHACHA20_POLY1305_SHA256
        ssl-default-bind-options ssl-min-ver TLSv1.2 no-tls-tickets

defaults
        log     global
        mode    http
        option  httplog
        option  dontlognull
        timeout connect 5000
        timeout client  50000
        timeout server  50000
        errorfile 400 /etc/haproxy/errors/400.http
        errorfile 403 /etc/haproxy/errors/403.http
        errorfile 408 /etc/haproxy/errors/408.http
        errorfile 500 /etc/haproxy/errors/500.http
        errorfile 502 /etc/haproxy/errors/502.http
        errorfile 503 /etc/haproxy/errors/503.http
        errorfile 504 /etc/haproxy/errors/504.http

# HAproxy for web servers
frontend web-frontend
   bind *:80
   mode http
   default_backend web-backend

backend web-backend
   balance roundrobin
   server wildfly01 192.168.0.9:8080 check
   server wildfly02 192.168.0.11:8080 check

listen stats
    bind :32600
    stats enable
    stats uri /
    stats hide-version
    stats auth ha_proxy_admin:EXAMPLE_PASSWORD

After configuring the HAProxy, I need to restart my HAProxy

Restarting HAProxy
$ sudo systemctl retart haproxy

3. Testing HA Proxy for Load Balancing

Now, I will test my haproxy loading server by accessing to the application server through HAProxy via http://192.168.0.10/java-ee-haproxy-web-01/ . You can see as following that your request has been routed thought server 1 and Server 2.

Fig 1. Routing Result

4. Reference

  1. How to install HAProxy Load Balancer on Ubuntu
  2. Ubuntu Changing Mirror

No comments:

Post a Comment

Feature Recently

Running Wildfly Application Server in Domain Mode

  Wildfly application server provides two modes of how to run application one wildfly application server. It is very simple if you run your ...

Most Views