Home >System Tutorial >LINUX >HAProxy on Ubuntu: Load Balancing and Failover for Resilient Infrastructure
In today's fast-paced digital environment, ensuring application availability and performance is critical. Modern infrastructure requires powerful solutions to efficiently allocate traffic, maintaining service availability even in case of server failure. HAProxyAs the actual standard for high-performance load balancing and failover, it came into being.
This article explores the synergy between HAProxy and Ubuntu, one of the most popular Linux distributions. From installation to advanced configuration, we'll dive into how HAProxy can transform your infrastructure with load balancing and failover capabilities.
Load balancing is the process of allocating incoming network traffic to multiple servers. By balancing the load, it ensures that no single server is overwhelmed, thereby improving performance, reliability, and fault tolerance.
Main advantages:
Load balancing type:
Failover ensures continuity by automatically redirecting traffic to backup resources in the event of a major resource failure. It is the cornerstone of the High Availability (HA) setup.
With HAProxy, failover is seamless:
Let's start by installing and configuring HAProxy on Ubuntu.
Prerequisite:
Step 1: Install HAProxy
sudo apt update && sudo apt upgrade -y
sudo apt install haproxy -y
haproxy -v
Step 2: Configure HAProxy
Edit /etc/haproxy/haproxy.cfg
Configuration file:
<code>global log /dev/log local0 log /dev/log local1 notice maxconn 2048 daemon defaults log global option httplog option dontlognull timeout connect 5000ms timeout client 50000ms timeout server 50000ms frontend http_front bind *:80 default_backend http_back backend http_back balance roundrobin server server1 192.168.1.101:80 check server server2 192.168.1.102:80 check</code>
sudo systemctl restart haproxy
Load balancing algorithm:
Update the balance
directive in the backend accordingly.
Health Check: Health Check ensures that traffic is sent to normal servers only. check
The instruction performs regular health checks.
SSL Termination: To protect traffic, configure HAProxy to handle SSL Termination.
frontend https_front bind *:443 ssl crt /etc/haproxy/certs/example.pem default_backend http_back
Access Control List (ACL): Filter traffic using ACL:
<code>frontend http_front acl is_api path_beg /api use_backend api_back if is_api</code>
VRRP with Keepalived: To enable failover, integrate Keepalived with HAProxy.
sudo apt install keepalived -y
/etc/keepalived/keepalived.conf
): <code>vrrp_instance VI_1 { state MASTER interface eth0 virtual_router_id 51 priority 100 advert_int 1 authentication { auth_type PASS auth_pass mypassword } virtual_ipaddress { 192.168.1.100 } }</code>
sudo systemctl restart keepalived
Keepalived ensures that the secondary server takes over seamlessly when the primary server fails.
HAProxy Statistics Panel: Enable the panel for real-time monitoring:
<code>listen stats bind *:8404 stats enable stats uri /stats stats auth admin:password</code>
Access it in http://<server-ip>:8404/stats</server-ip>
.
Optimization skills:
maxconn
and timeout
settings. Connection problem:
Performance bottleneck:
Maintenance:
By combining HAProxy and Ubuntu, you get a powerful combination to manage traffic and ensure uptime. With the above steps, you can build a resilient infrastructure that can handle high loads and server failures.
Start trying HAProxy now and unlock the full potential of Ubuntu system capabilities.
The above is the detailed content of HAProxy on Ubuntu: Load Balancing and Failover for Resilient Infrastructure. For more information, please follow other related articles on the PHP Chinese website!