In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly introduces "how to use HAProxy to configure HTTP load balancing system on Linux". In daily operation, I believe many people have doubts about how to configure HTTP load balancing system on Linux using HAProxy. The editor consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful for you to answer the doubt of "how to configure HTTP load balancing system using HAProxy on Linux". Next, please follow the editor to study!
Nowadays, the demand for Internet-based applications and services is increasing, which puts more and more pressure on the majority of IT administrators. In the face of a sudden surge in traffic, an increase in self-generated traffic, or internal challenges (such as hardware failures and emergency maintenance), your Internet applications must remain available at any time. Even modern development operations and continuous delivery practices can endanger the reliability and consistent performance of Internet services.
Unpredictability or lack of consistent performance is more than you can bear. So how can we eliminate these shortcomings? In most cases, a suitable load balancing solution is expected to meet this requirement. Today I'm going to show you how to build a HTTP load balancing system using HAProxy.
Brief introduction of HTTP load balancing
HTTP load balancing is a network solution that distributes incoming HTTP or HTTPS traffic among several servers hosting the same application content. Because the application requests are balanced among multiple available servers, the load balancing system can prevent any application server from becoming a single point of failure, thus improving the overall application availability and responsiveness. It also allows you to easily reduce / expand the size of deployed applications with changing workloads by adding or removing additional application servers.
Where and when is load balancing used?
As the load balancing system improves server utilization and increases availability to a limited extent, you should use it as long as your server starts to face heavy load or is planning an architecture for a larger project. It is a good habit to plan the use of the load balancing system in advance. That way, when you need to expand the size of the environment in the future, it will prove its use.
What is HAProxy?
HAProxy is a popular open source load balancing and proxy system for TCP/HTTP servers on the GNU/Linux platform. HAProxy is designed with a single-thread event-driven architecture that can easily handle the line speed of 10G network cards and is now widely used in many production environments. Its functional features include automatic health check, customizable load balancing algorithm, support for HTTPS/SSL and session rate limit.
What are we going to achieve in this tutorial?
In this tutorial, we will step through the process of configuring a HAProxy-based load balancing system for HTTP website servers.
prerequisite
You need at least one (*) website server to verify the function of the load balancing system. Let's assume that the back-end HTTP web server is up and running.
Install HAProxy on Linux
For most distributions, we can use the package manager of your distribution to install HAProxy.
Install HAProxy on Debian
In Debian, we need to add backward migration capabilities to Wheezy. To do this, create a new file called "backports.list" in / etc/apt/sources.list.d with the following contents:
Deb http://cdn.debian.net/debian wheezy backports main
Update your library data and install HAProxy.
# apt get update # apt get install haproxy
Install HAProxy on Ubuntu
# apt get install haproxy
Install HAProxy on CentOS and RHEL
# yum install haproxy
Configure HAProxy
In this tutorial, we assume that two HTTP Web servers are up and running with IP addresses of 192.168.100.2 and 192.168.100.3, respectively. We also assume that the load balancing system will be configured at the server with the IP address 192.168.100.4.
In order for HAProxy to function, you need to change several items in / etc/haproxy/haproxy.cfg. These changes are described in this section. In case a configuration is different for different GNU/Linux distributions, it will be noted in the appropriate paragraph.
1. Configure the logging featur
One of the first things you need to do is to set up proper logging for your HAProxy, which is very useful for future debugging. The log configuration is located in the global section of / etc/haproxy/haproxy.cfg. The following are release-specific instructions for configuring logs for HAProxy.
CentOS or RHEL:
To enable logging on CentOS/RHEL, put:
Log 127.0.0.1 local2
Replace it with:
Log 127.0.0.1 local0
Next, create a separate log file for HAProxy in / var/log. To do this, we need to change the current rsyslog configuration. To make the configuration simple and clear, we will create a new file called haproxy.conf in / etc/rsyslog.d/, which reads as follows.
$ModLoad imudp $UDPServerRun 514$ template Haproxy, "% msg%\ n" local0.=info / var/log/haproxy.log;Haproxy local0.notice / var/log/haproxy status.log;Haproxy local0.* ~
This configuration will isolate all HAProxy messages based on $template to the log file in / var/log. Now, restart rsyslog to make the changes take effect.
# service rsyslog restart
Debian or Ubuntu:
To enable logging for HAProxy on Debian or Ubuntu, put:
Log / dev/log local0 log / dev/log local1 notice
Replace it with:
Log 127.0.0.1 local0
Next, configure a separate log file for HAProxy and edit a file named haproxy.conf in / etc/rsyslog.d/ (or 49-haproxy.conf in Debian) as follows.
$ModLoad imudp $UDPServerRun 514$ template Haproxy, "% msg%\ n" local0.=info / var/log/haproxy.log;Haproxy local0.notice / var/log/haproxy status.log;Haproxy local0.* ~
This configuration will isolate all HAProxy messages based on $template to the log file in / var/log. Now, restart rsyslog to make the changes take effect.
# service rsyslog restart
two。 Set default value
The next step is to set the default variable for HAProxy. Locate the defaults section in / etc/haproxy/haproxy.cfg and replace it with the following configuration.
Log global mode http option httplog option dontlognull retries 3 option redispatch maxconn 20000 contimeout 5000 clitimeout 50000 srvtimeout 50000
The above configuration is recommended for HTTP load balancer, but it may not be the most suitable solution for your environment. If so, refer to the HAProxy reference man pages for appropriate changes and adjustments.
3. Configuration of website server cluster
The configuration of the website server cluster (Webfarm) defines the available HTTP server clusters. Most of the settings of the load balancing system we built will be placed here. Now, we will create some basic configurations, and our nodes will be defined here. Replace all configurations from the frontend section to the end of the file with the following code:
Listen webfarm *: 80 mode http stats enable stats uri / haproxy?stats stats realm Haproxy\ Statistics stats auth haproxy:stats balance roundrobin cookie LBN insert indirect nocache option httpclose option forwardfor server web01 192.168.100.2 cookie node1 check server web02 80 cookie node2 check
The line "listen webfarm *: 80" defines which interfaces our load balancing system will listen on. For the sake of this tutorial, I set this value to "*", which allows the load balancing system to listen on all of our interfaces. In the actual scenario, this may not be desirable and should be replaced with an interface that can be accessed from the Internet.
Stats enable stats uri / haproxy?stats stats realm Haproxy\ Statistics stats auth haproxy:stats
The above settings declare that the statistics of the load balancing system can be accessed at http:///haproxy?stats. This access is secured by simple HTTP authentication and the login name "haproxy" and password "stats". These settings should be replaced with your own login information. If you don't want these statistics to be seen, you can disable them altogether.
Here is an example of HAProxy statistics.
The "balance roundrobin" line defines what type of load balancing we will use. In this tutorial, we will use a simple round-robin scheduling algorithm, which is more than sufficient for HTTP load balancing. HAProxy also provides other types of load balancing:
Leastconn: the server with the least number of connections receives connections first.
Source: hashes the source IP address and divides the total weight of the running server by the hash value to determine which server will receive the request.
The left part of the uri:URI (before the question mark) is hashed and divided by the hash value by the total weight of the running server. The result determines which server will receive the request.
Url_param: the URL parameter specified in the variable will be queried in the query string of each HTTP GET request. You can basically lock requests using deliberately crafted URL (crafted URL) to specific load balancing nodes.
Hdr (name): the HTTP header will be queried in each HTTP request and directed to a specific node.
The "cookie LBN insert indirect nocache" line allows our load balancing system to store persistent cookie, which allows us to find out exactly which node in the cluster is used for a particular session. These node cookie will be stored with the specified name. In our example, I used "LBN", but you can specify any name you like. The node will store the string as a value for the cookie.
Server web01 192.168.100.2:80 cookie node1 check server web02 192.168.100.3:80 cookie node2 check
The above part defines the server node cluster of the website. Each server is represented by internal names (such as web01 and web02), IP addresses, and unique cookie strings. The cookie string can be defined as any name you need. I used simple node1, node2... Node (n).
Start HAProxy
After you have finished the configuration, you can launch HAProxy and verify that everything is working as expected.
Start HAProxy on Centos/RHEL
Use the following instructions to enable HAProxy to start after the system boots and open it:
# chkconfig haproxy on # service haproxy start
Of course, don't forget to enable port 80 in the firewall, as shown below.
Firewall on CentOS/RHEL 7:
# firewall cmd permanent zone=public add port=80/tcp # firewall cmd reload
Firewall on CentOS/RHEL 6:
Add the following line to the section ": OUTPUT ACCEPT" in / etc/sysconfig/iptables:
An INPUT m state state NEW m tcp p tcp dport 80 j ACCEPT
Then restart iptables:
# service iptables restart
Start HAProxy on Debian
Start HAProxy using the following directive:
# service haproxy start
Don't forget to enable port 80 in the firewall, so add the following line to / etc/iptables.up.rules:
An INPUT p tcp dport 80 j ACCEPT
Start HAProxy on Ubuntu
To enable HAProxy to start after the system starts, simply set the "ENABLED" option to "1" in / etc/default/haproxy:
ENABLED=1
Start HAProxy:
# service haproxy start
Then enable port 80 in the firewall:
# ufw allow 80
Test HAProxy
To check that HAproxy is working properly, we can perform the following steps:
First, prepare the test.php file with the following:
This PHP file will tell us which server (that is, the load balancing system) forwards the request and which back-end website server actually handles the request.
Put the PHP file in the root directory of the two back-end Web servers. Now use the curl command to extract the PHP file from the load balancing system (192.168.100.4).
$curl http://192.168.100.4/test.php
When we run this command multiple times, we should see the following two outputs appear alternately (due to the round robin scheduling algorithm).
Server IP: 192.168.100.2
X-Forwarded-for: 192.168.100.4
Server IP: 192.168.100.3
X-Forwarded-for: 192.168.100.4
If we stop one of these two back-end Web servers, the curl command should still execute, directing the request to another available Web server.
Concluding remarks
At this point, you should have a fully practical load balancing system that can provide requests for your site nodes in round robin mode. As always, you can change the configuration at will to make it more suitable for your infrastructure. I hope this tutorial will help you make your website projects more resistant to stress and higher availability.
As you have noticed, the settings included in this tutorial apply to only one load balancing system. This means that we have replaced one single point of failure with another. In an actual scenario, you should deploy at least two or three load balancing systems to prevent any failures that may occur, but this is beyond the scope of this tutorial.
At this point, the study on "how to use HAProxy to configure HTTP load balancing system on Linux" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.