Step 1: Install HAProxy
As a first step in the process, you will need to proceed with the installation of HAProxy on your Ubuntu server to ensure proper functionality.
sudo apt update sudo apt install haproxy
Copy
Step 2: Configure HAProxy
After installing HAProxy, configure it by editing the configuration file located at /etc/haproxy/haproxy.cfg using your favorite text editor, such as nano or vim.
sudo nano /etc/haproxy/haproxy.cfg
Step 3: Add Your Configuration
Modify the current settings or incorporate your own configuration into the haproxy.cfg file. According to the configuration you provided, it should appear as follows:
haproxy
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&version=2.0.3&config=intermediate ssl-default-bind-ciphers PROFILE=SYSTEM ssl-default-bind-options no-sslv3 defaults log global mode http option httplog option dontlognull timeout connect 5000ms timeout client 50000ms timeout server 50000ms 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 frontend myapp bind *:80 timeout client 6s default_backend myservers backend myservers timeout connect 6s timeout server 6s server myserver1 192.168.4.148:80 check server myserver2 192.168.4.131:80 check server myserver3 192.168.4.136:80 check
Explanation of the Configuration:
global: This section contains global settings for HAProxy, such as logging, chroot, and the user/group under which HAProxy runs.
defaults: This section contains default settings for all other sections. These settings can be overridden in specific sections.
frontend myapp: This section defines the frontend configuration, which listens on port 80 for incoming HTTP traffic. It binds to all available IP addresses (*:80) and sets a client timeout of 6 seconds. The default_backend directive specifies that traffic should be forwarded to the myservers backend.
backend myservers: This section defines the backend servers that will handle the traffic. It sets a connect timeout and server timeout of 6 seconds. The server directives define the backend servers by their IP addresses and ports. The check option enables health checks for each server.
Step 4: Enable and Start HAProxy
After configuring HAProxy, you need to enable and start the HAProxy service.
sudo systemctl enable haproxy sudo systemctl start haproxy
Copy
Step 5: Check HAProxy Status
To ascertain that HAProxy is operating effectively, you may verify its status:
sudo systemctl status haproxy
Copy
Step 6: Test Your Configuration
To evaluate your HAProxy configuration, you can access your website using the IP address or domain name of the HAProxy server. HAProxy is designed to efficiently distribute traffic among the backend servers (myserver1, myserver2, and myserver3).
Step 7: (Optional) Enable HAProxy Logs
If you want to enable logging for HAProxy, you can configure it in the haproxy.cfg file under the global section:
haproxy
global log /dev/log local0 log /dev/log local1 notice ...
Then, configure rsyslog to handle HAProxy logs:
sudo nano /etc/rsyslog.d/49-haproxy.conf
Add the following line:
local0.* /var/log/haproxy.log
Restart rsyslog and HAProxy:
sudo systemctl restart rsyslog sudo systemctl restart haproxy
Step 8: (Optional) Configure SSL
If you want to configure SSL for your website, you can add an SSL frontend in the haproxy.cfg file:
frontend myapp-ssl bind *:443 ssl crt /etc/ssl/private/your-certificate.pem timeout client 6s default_backend myservers
Make sure to replace /etc/ssl/private/your-certificate.pem with the path to your SSL certificate.
Conclusion
Congratulations on successfully configuring HAProxy on Ubuntu to efficiently distribute traffic among multiple backend servers for your website. You have the opportunity to further refine the configuration to meet your specific needs, including the addition of more frontends, backends, or implementing advanced load balancing strategies.
#Configure HAProxy on Ubuntu
#HAProxy load balancing
#HAProxy Ubuntu setup
#HAProxy configuration guide
#Load balancing with HAProxy