First, we deploy WordPress on two or more virtual machines.
Then, we will use HAProxy as a load balancer.
HAProxy(High Availability Proxy):- It is an open-source load balancer that can load balance any TCP service. It is particularly suited for HTTP load balancing as it supports session persistence and layer 7 processing.
Installing HAProxy
Install HAProxy on the machine that will serve as the load balancer.
apt-get install haproxy
To check the installation.
haproxy -v
Configuring HAProxy
Create a configuration file for HAProxy. This file will specify the behavior of the load balancer, including the addresses and ports of the backend servers it will be balancing traffic to. In the configuration file, define the frontend and backend sections.
sudo nano /etc/haproxy/haproxy.cfg
We'll get this in configure file by default.
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 lis>
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.>
ssl-default-bind-ciphers ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128>
ssl-default-bind-ciphersuites TLS_AES_128_GCM_SHA256:TLS_AES_256_GCM_SH>
ssl-default-bind-options ssl-min-ver TLSv1.2 no-tls-tickets
defaults
log global
mode http
option httplog
option dontlognull
option redispatch
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
You can modify this if necessary and make changes, or leave it as-is.
The frontend section defines the IP address and port that the load balancer will listen on, while the backend section defines the addresses and ports of the backend servers.
frontend web-frontend
bind *:80:
mode http
default_backend web-backend
In the backend section, configure the load balancing algorithm to be used. HAProxy supports several algorithms, including round-robin, least-connections, and IP-hash.
backend web-backend
balance roundrobin
server web1 192.168.1.13:80 check
server web2 192.168.1.14:80 check
The server directive declares a backend server, the syntax is:
server <name> <address>[:port] [param*]
To use HAProxy's statistics page to monitor the real-time statistics of the load balancer.
listen stats
bind 192.168.1.9:8030
mode http
stats enable
stats hide-version
stats refresh 30s
stats show-node
stats auth admin:password
stats uri /stats
HYProxy stats can be accessed on the machine's IP and port specified in the configuration file above.