Home >System Tutorial >LINUX >HAProxy on Ubuntu: Load Balancing and Failover for Resilient Infrastructure

HAProxy on Ubuntu: Load Balancing and Failover for Resilient Infrastructure

Joseph Gordon-Levitt
Joseph Gordon-LevittOriginal
2025-03-05 09:06:11603browse

HAProxy on Ubuntu: Load Balancing and Failover for Resilient Infrastructure

Introduction

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.

Detailed explanation of load balancing

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:

  • Scalability: Training growing traffic by adding more servers.
  • Reliability: Easy the impact of server failure by routing traffic to a normal server.
  • Performance: Reduce latency by evenly distributing workloads.

Load balancing type:

  • Layer 4 (transport layer): Distribute traffic according to IP and port information.
  • Layer 7 (application layer): Make routing decisions based on application-level data (such as HTTP headers).

Failover Concept

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:

  • If the backend server is not available, HAProxy will detect it through a health check.
  • Traffic will be rerouted to other available servers, thus maintaining uninterrupted service.

Set HAProxy on Ubuntu

Let's start by installing and configuring HAProxy on Ubuntu.

Prerequisite:

  • A Ubuntu server (recommended to use 20.04 or higher).
  • Multiple backend servers for testing load balancing.
  • Basic Linux command line skills.

Step 1: Install HAProxy

  1. Update your system: sudo apt update && sudo apt upgrade -y
  2. Installing HAProxy: sudo apt install haproxy -y
  3. Verify installation: haproxy -v

Step 2: Configure HAProxy Edit /etc/haproxy/haproxy.cfgConfiguration 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>
  1. Restart HAProxy to apply changes: sudo systemctl restart haproxy
  2. Test by accessing the server's IP address. HAProxy will allocate requests between backends.

Advanced Configuration

Load balancing algorithm:

  • Poll: Sequentially allocate requests.
  • Number of connections: Routed to the server with the least active connections.
  • Source: Make sure the client is always routed to the same server.

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.

  1. Get the SSL certificate.
  2. Update configuration to use HTTPS: 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>

Enable high availability

VRRP with Keepalived: To enable failover, integrate Keepalived with HAProxy.

  1. Installing Keepalived: sudo apt install keepalived -y
  2. Configuration Keepalived (/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>
  1. Restart Keepalived: sudo systemctl restart keepalived

Keepalived ensures that the secondary server takes over seamlessly when the primary server fails.

Monitoring and Performance Tuning

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:

  • Adjust the maxconn and timeout settings.
  • Use gzip compression for HTTP traffic.
  • Monitor exceptions in the log.

User cases and actual scenarios

  • Microservices: Assign API requests to multiple services.
  • Web Application: Handle traffic spikes by extending backend servers.
  • Database load balancing: Optimize read and write operations.

Troubleshooting of FAQs

Connection problem:

  • Check firewall rules.
  • Verify server health check.

Performance bottleneck:

  • Add the file descriptor's ulimit.
  • Optimize backend server configuration.

Maintenance:

  • Always backup configuration.
  • Apply updates during low traffic.

Conclusion

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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn