Bootstrap FreeKB - HAProxy (Load Balance) - Install HAProxy on Linux
HAProxy (Load Balance) - Install HAProxy on Linux


HAProxy is a service that can be used to load balance requests between a TCP service, most often, web servers. For example, let's say you have two web servers. HAProxy can be used to load balance the requests across the two web servers, using Round Robin by default.

 

On a Linux Red Hat distribution (CentOS, Fedora, Red Hat), the dnf or yum command can be used to install haproxy. On a Debian distribution, the apt-get command can be used.

dnf install haproxy

 

If you are installing HAProxy on an Amazon Web Services (AWS) EC2 instance, you will first want to ensure that your EC2 instance was created using one of the Amazon Linux 2023 (AL2023) Amazon Machine Images (AMI) because the Amazon Linux 2 (AL2) images contain a repo that installs HAProxy version 1.5 whereas the AL2023 images contain a repo that install HAProxy version 2.8. Check out my article FreeKB - Amazon Web Services (AWS) - Create an EC2 instance using the AWS CLI.

Next you will typically enable the /stats page to confirm HAProxy is working as expected. Add the following block to /etc/haproxy/haproxy.cfg.

listen stats
    bind *:8080
    stats enable
    stats uri /stats

 

Notice this prior block is listening on port 8080. If you have a firewall, such as iptables or firewalld, allow port 8080 in the firewall, just for testing purposes.

firewall-cmd --add-port=8080/tcp

 

Or, if running HAProxy on an Amazon Web Services (AWS) EC2 Instance, you will probably add an inbound rule to the Security Group associated with the EC2 Instance to allow inbound on port 8080. This can be done using the aws ec2 authorize-security-group-ingress command.

aws ec2 authorize-security-group-ingress --group-id sg-0778124087b3d14d4 --protocol tcp --port 8080 --cidr 0.0.0.0/0

 

The ps command can be used to determine if your system is using init or systemd. If PID 1 is init, then you will use the service command. If PID 1 is systemd, then you will use the systemctl command.

If your system is using systemd, use the systemctl command to start and enable HAProxy.

systemctl enable haproxy
systemctl start haproxy
systemctl status haproxy

 

If your system is using init, use the service command to start and enable HAProxy.

service haproxy enable
service haproxy start
service haproxy status

 

You should now be able to access the stats page at http://<ip address>:8080/stats.

 

Typically, you will next configure HAProxy to load balance a single website.




Did you find this article helpful?

If so, consider buying me a coffee over at Buy Me A Coffee



Comments


Add a Comment


Please enter c1b195 in the box below so that we can be sure you are a human.