With the increase of network traffic, the server starts to face heavy load. At this time, it needs to be equipped with an HTTP load balancing system. How to configure HTTP load balancing system under Linux? ? Let's learn together with Xiaobian.
Today's demands for Internet-based applications and services are growing, putting increasing pressure on IT administrators. In the face of sudden traffic spikes, increased self-generated traffic, or internal challenges (such as hardware failures and emergency maintenance), your Internet applications must remain readily available anyway. Even modern development operations and continuous delivery practices can jeopardize the reliability and consistent performance of Internet services.
Unpredictable or lack of consistent performance is something you can't afford. So how can we eliminate these shortcomings? In most cases, a suitable load balancing solution is expected to meet this requirement. Today I will show you how to build an HTTP load balancing system using HAProxy.
Introduction to HTTP Load Balancing
HTTP Load Balancing is a network solution that distributes incoming HTTP or HTTPS traffic between several servers hosting the same application content. By balancing application requests across multiple available servers, the load balancing system prevents any application server from becoming a single point of failure, thereby increasing overall application availability and responsiveness. It also allows you to easily scale down/expand the size of deployed applications with changing workloads, simply adding or removing additional application servers.
Where is load balancing and when?
Because the load balancing system improves server utilization and maximizes availability, you should use it if your server is starting to face heavy loads or is planning a larger project. It is a good practice to plan the use of the load balancing system in advance. That way, when you need to expand the scale of the environment in the future, it will prove its purpose.
What is HAProxy?
HAProxy is a popular open source load balancing and proxy system for TCP/HTTP servers on the GNU/Linux platform. Designed with a single-threaded event-driven architecture, HAProxy easily handles 10G NIC line speeds and is now widely used in many production environments. Features include automatic health check, customizable load balancing algorithms, support for HTTPS/SSL, and session rate limiting.
What purpose do we achieve in this tutorial?
In this tutorial, we will walk through the process of configuring a HAProxy-based load balancing system for an HTTP web server.
Prerequisites
You need at least one (preferably two) web servers to verify the functionality of your load balancing system. We assume that the backend HTTP web server is set up and running.
Installing HAProxy on Linux
For most distributions, we can install HAProxy using the package manager of your distribution.
Installing HAProxy on Debian
In Debian, we need to add backward migration functionality 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 wheezybackports main
Update your software library data and install HAProxy.
# apt get update # apt get install haproxy
Install HAProxy on Ubuntu
# apt get install haproxy
HAProxy installed on CentOS and RHEL
# yum install haproxy
Configuring HAProxy
In this tutorial, we assume that two HTTP web servers are up and running. Its IP addresses are 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 work, you need to change several items in /etc/haproxy/haproxy.cfg. These changes are described in this section. In case a configuration differs for different GNU/Linux distributions, it will be noted in the corresponding paragraph.
1. Configuring Log Functions
One of the first things you need to do is to set up the appropriate logging function for your HAProxy, which is useful for debugging in the future. The log configuration is located in the global section of /etc/haproxy/haproxy.cfg. The following are instructions for a specific release to configure logging for HAProxy.
CentOS or RHEL:
To enable logging on CentOS/RHEL, change:
log 127.0.0.1 local2
to: Br>
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/with the following contents.
$ModLoad imudp $UDPServerRun 514 $template Haproxy,“%msg%\ ” local0.=info /var/log/haproxy.log;Haproxy local0.notice /var/log/Haproxystatus.log;Haproxy local0.* ~
This configuration will isolate all HAProxy messages based on $template to log files in /var/log. Now restart the rsyslog and let 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
Switch to:
log 127.0.0.1 local0
Next, configure a separate log file for HAProxy, edit /etc A file named haproxy.conf in /rsyslog.d/(or 49-haproxy.conf in Debian) has the following contents.
$ModLoad imudp $UDPServerRun 514 $template Haproxy,“%msg%\ ” local0.=info /var/log/haproxy.log;Haproxy local0.notice /var/log/Haproxystatus.log;Haproxy local0.* ~
This configuration will isolate all HAProxy messages based on $template to log files in /var/log. Now restart the rsyslog and let the changes take effect.
# service rsyslog restart
2. Setting default values
The next step is to set default variables for HAProxy. Find the defaults section of /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 recommends HTTP load balancer usage, but may not be the best solution for your environment Program. If so, please refer to the HAProxy Reference Manual page for appropriate changes and adjustments.
3. Configuration of a Web Server Cluster
The configuration of a Web Server (Webfarm) defines a cluster of available HTTP servers. Most of the settings for the load balancing system we build will be placed here. Now we will create some basic configuration, our nodes will be defined here. Replace all configuration 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:80 cookie node1 check server web02 192.168.100.3:80 cookie node2 check
“ Listen webfarm *:80” This line defines which interfaces our load balancing system will listen on. For the purposes of this tutorial, I set this value to “*”, which allows the load balancing system to listen on all of our interfaces. In an actual scenario, this may be undesirable and should be replaced with an interface that is accessible from the Internet.
stats enable stats uri /haproxy? Stats stats realm Haproxy\\ Statistics stats auth haproxy:stats
The above setting statement can be found at http://load-balancer-IP/haproxy? Statistics on the load balancing system at stats. This access is ensured by simple HTTP authentication as well as 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 completely.
Previous12Next Total 2 Pages
In the Linux virtual machine, the device id is the drive letter of the mapped disk, so the device id
ln is an important command in the Linux system. It can establish links for files an
Xen virtual machine can be used for server application integration, software development testing, et
An executable file is a portable executable file that is used for program execution
Linux prompt Unable to locate package how to deal with?
Ubuntu disables the shutdown confirmation box step
How to add time and user parameters to history on Linux
How to make a bootable img/iso image file on a Linux system
Ubuntu how to install and configure JRE
How to adjust the resolution and screen refresh rate of Ubuntu system?
CentOS 7 tips for resetting the root password
View the Linux version of the skill
Linux how to use the cal command to view the calendar
Ubuntu 64-bit installation WPS office software error how to do?
Killing the original PC version of the story of the fourth level of how to play
Win10 browser Microsoft Edge new logo exposure
Win7 system press L key to automatically enter the lock screen state how to do
Solve the problem of notebook under Win7 "Please consider replacing the battery" problem
Old graphics card with Windows7 system after the blue screen
What is the solution to the slow response of the Win7 system?
How to solve the problem that Windows 7 cannot recognize the optical drive
How to set the login password for win10?