How to configure software load balancing (such as HAProxy) on Linux

WBOY
Release: 2023-07-05 14:40:38
Original
1749 people have browsed it

How to configure software load balancing (such as HAProxy) on Linux

Introduction:
In modern Internet applications, high availability and high performance are crucial. To achieve scalability and fault tolerance, a load balancer is often used to distribute network traffic to multiple servers. This article will introduce how to configure software load balancing on Linux, taking HAProxy as an example, and provide code examples.

1. Install and configure HAProxy
First, we need to install the HAProxy software. On Ubuntu, you can install it with the following command:

sudo apt-get install haproxy
Copy after login

After installation, we need to configure HAProxy. Open the configuration file /etc/haproxy/haproxy.cfg and use a text editor to modify it.

sudo vi /etc/haproxy/haproxy.cfg
Copy after login

In the configuration file, we need to set up the listener and backend server. The following is the content of a sample configuration file:

global
    log         /dev/log local0
    log         /dev/log local1 notice
    chroot      /var/lib/haproxy
    stats socket /run/haproxy/admin.sock mode 660 level admin
    stats timeout 30s
    maxconn     4096
    user        haproxy
    group       haproxy
    daemon

defaults
    log     global
    mode    http
    option  httplog
    option  dontlognull
    timeout connect 5000
    timeout client  50000
    timeout server  50000

frontend http_front
    bind *:80
    default_backend http_back

backend http_back
    balance roundrobin
    server web1 192.168.0.101:80 check
    server web2 192.168.0.102:80 check
Copy after login

In the above configuration, we set up an http front-end that listens on port 80 and distributes traffic to the back-end server named http_back. Through the balance command, we can choose the load balancing algorithm, such as roundrobin, leastconn, etc. In the example, we use roundrobin algorithm for round robin distribution. At the same time, we defined two backend servers, respectively 192.168.0.101:80 and 192.168.0.102:80. The check command means to check the health status of the backend server.

After completing the configuration, save and exit.

2. Start and monitor HAProxy
After completing the configuration, we need to start and monitor the HAProxy service. Use the following command to start HAProxy:

sudo service haproxy start
Copy after login

After the service is started, you can use the following command to check the service status:

sudo service haproxy status
Copy after login

Use the statistical reporting function of HAProxy to monitor traffic distribution in real time. In the global section of the configuration file, we set the stats socket and stats timeout. We can access the statistical report through the following command:

sudo socat stdio /run/haproxy/admin.sock
Copy after login

In addition, we can also access http:// in the browser localhost:1936 to view graphical HAProxy statistical reports.

3. Use HAProxy for load balancing
After the configuration is completed and the HAProxy service is started, we can use the load balancer to distribute traffic to multiple back-end servers. For example, an application running locally listens on port 8080 and hopes to be load balanced through HAProxy. We can access the application by visiting http://localhost:80 in the browser.

In this configuration, HAProxy will package and forward traffic requests to the application on the backend server according to the selected load balancing algorithm.

Conclusion:
This article introduces how to configure software load balancing on Linux, taking HAProxy as an example. By installing and configuring HAProxy, we can achieve high availability and high performance network traffic distribution. This article provides code examples and monitoring guidelines to help readers quickly get started using HAProxy. The use of load balancers can significantly improve the performance and scalability of applications and is an indispensable part of modern Internet applications.

The above is the detailed content of How to configure software load balancing (such as HAProxy) on Linux. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!