In Linux, how does one configure the HTTP Server Load balancer System Using HAProxy? (1)

Source: Internet
Author: User
Tags website server haproxy rsyslog

In Linux, how does one configure the HTTP Server Load balancer System Using HAProxy? (1)

Nowadays, the demand for Internet-based applications and services is growing, which puts more and more pressure on the IT administrators. In the face of sudden traffic spikes, increases in self-generated traffic, or internal challenges such as hardware faults and urgent maintenance), your internet applications must be available at any time. Even the modern development, operation, and continuous delivery practices may compromise the reliability and consistent performance of Internet services.

You cannot predict or lack consistent performance. So how can we eliminate these shortcomings? In most cases, a suitable Server Load balancer solution is expected to meet this requirement. Today, I will show you how to use HAProxy to build an HTTP Server Load balancer system.

Introduction to HTTP Server Load balancer

HTTP Server Load balancer is a network solution that distributes incoming HTTP or HTTPS traffic between several servers hosting the same application content. Because application requests are balanced among multiple available servers, the Server Load balancer system can prevent any application server from becoming a single fault point, thus improving the overall application availability and response capabilities. It also allows you to easily scale down/expand the scale of deployed application systems with the changing workload. You only need to add or delete additional application servers.

Where should I use Server Load balancer and when?

The server Load balancer system improves the server utilization and maximizes availability. As long as your server is facing heavy loads or is planning a large project architecture, you should use it. It is a good habit to plan the use of the Server Load balancer system in advance. In this way, it will prove its purpose when you need to scale up the environment in the future.

What is HAProxy?

HAProxy is a popular open-source Server Load balancer and proxy system for TCP/HTTP servers on the GNU/Linux platform. HAProxy is designed with a single-threaded event-driven architecture. It can easily process 10 Gbit/s Nic line speeds and is widely used in many production environments. Its features include automatic health check, customizable Load Balancing algorithms, support for HTTPS/SSL, and session rate restrictions.

What is the purpose of this tutorial?

In this tutorial, we will gradually introduce how to configure the HAProxy-Based Server Load balancer System for HTTP website servers.

Prerequisites

You need at least one server (preferably two) to verify the functions of the Server Load balancer system. We assume that the backend HTTP website server has been set up and running.

Install HAProxy on Linux

For most releases, we can use the package manager of your release to install HAProxy.

Install HAProxy On Debian

In Debian, we need to add the backward porting function for Wheezy. Therefore, create a new file named "backports. list" in/etc/apt/sources. list. d. The content of the file is as follows:

 
 
  1. deb http://cdn.debian.net/debian wheezy­backports main 

Update the data of your software library and install HAProxy.

 
 
  1. # apt­ get update  
  2. # apt ­get install haproxy 

Install HAProxy on Ubuntu

 
 
  1. # apt ­get install haproxy 

Install HAProxy on CentOS and RHEL

 
 
  1. # yum install haproxy 

Configure HAProxy

In this tutorial, we assume that two HTTP website servers have been set up and run, and their IP addresses are 192.168.100.2 and 192.168.100.3, respectively. We also assume that the Server Load balancer system will be configured on the server whose IP address is 192.168.100.4.

To enable HAProxy to function, you need to change several items in/etc/haproxy. cfg. These changes are described in this section. If a configuration is different for different GNU/Linux distributions, it will be noted in the corresponding section.

1. Configure the log function

One of the first tasks you need to do is to create a proper log function for your HAProxy, which is useful for future debugging. The log configuration content is located in the global part of/etc/haproxy. cfg. The following commands are for specific releases and are used to configure logs for HAProxy.

CentOS or RHEL:

To enable the log function on CentOS/RHEL, run the following command:

Log 127.0.0.1 local2

Replace:

Log 127.0.0.1 local0

Next, create a separate log file for HAProxy in/var/log. Therefore, we need to modify the current rsyslog configuration. To make the configuration simple and clear, we will create a new file named haproxy. conf in/etc/rsyslog. d/with the following content.

 
 
  1. $ModLoad imudp   
  2. $UDPServerRun 514    
  3. $template Haproxy,"%msg%\n"   
  4. local0.=info ­/var/log/haproxy.log;Haproxy   
  5. local0.notice ­/var/log/haproxy­status.log;Haproxy   
  6. local0.* ~   

This configuration isolates all HAProxy messages based on $ template into log files in/var/log. Restart rsyslog to make the change take effect.

 
 
  1. # service rsyslog restart 

Debian or Ubuntu:

To enable the log function for HAProxy On Debian or Ubuntu:

 
 
  1. log /dev/log local0  
  2. log /dev/log local1 notice 

Replace:

 
 
  1. log 127.0.0.1 local0 

Next, configure a separate log file for HAProxy, edit a file named haproxy. conf in/etc/rsyslog. d/or 49-haproxy.conf In Debian. The content is as follows.

 
 
  1. $ModLoad imudp   
  2. $UDPServerRun 514    
  3. $template Haproxy,"%msg%\n"   
  4. local0.=info ­/var/log/haproxy.log;Haproxy   
  5. local0.notice ­/var/log/haproxy­status.log;Haproxy   
  6. local0.* ~   

This configuration isolates all HAProxy messages based on $ template into log files in/var/log. Restart rsyslog to make the change take effect.

 
 
  1. # service rsyslog restart 

2. Set the default value

The next step is to set the default variable for HAProxy. Find the ults section in/etc/haproxy. cfg and replace it with the following configuration.

 
 
  1. log     global   
  2. mode    http   
  3. option  httplog   
  4. option  dontlognull   
  5. retries 3   
  6. option redispatch   
  7. maxconn 20000   
  8. contimeout      5000   
  9. clitimeout      50000   
  10. srvtimeout      50000  

The above configuration is recommended for HTTP Server Load balancer, but may not be the most suitable solution for your environment. If that is the case, see the HAProxy reference manual page to make appropriate changes and adjustments.

3. Configure the website server cluster

Website server cluster Webfarm) configuration defines the available HTTP server cluster. Most of the settings of the Server Load balancer system we have built will be put here. Now, we will create some basic configurations, and our nodes will be defined here. Replace all the configurations from the frontend part to the end of the file with the following code:

 
 
  1. listen webfarm *:80   
  2.      mode http   
  3.      stats enable   
  4.      stats uri /haproxy?stats   
  5.      stats realm Haproxy\ Statistics   
  6.      stats auth haproxy:stats   
  7.      balance roundrobin   
  8.      cookie LBN insert indirect nocache   
  9.      option httpclose   
  10.      option forwardfor   
  11.      server web01 192.168.100.2:80 cookie node1 check   
  12.      server web02 192.168.100.3:80 cookie node2 check  

The "listen webfarm *: 80" line defines the interfaces that our Server Load balancer system will listen on. For the purpose of this tutorial, I set this value to "*", which allows the Server Load balancer system to listen to all our interfaces. In actual scenarios, this may be undesirable and should be replaced by an interface that can be accessed from the Internet.

 
 
  1. stats enable   
  2. stats uri /haproxy?stats   
  3. stats realm Haproxy\ Statistics   
  4. stats auth haproxy:stats  

The preceding settings can be declared at http: // <load-balancer-IP>/haproxy? Statistics on access to the server Load balancer system at stats. This access is ensured by simple HTTP verification and the login name "haproxy" and password "stats. These settings should be replaced with your own login information. If you do not want these statistics to be viewed, you can disable them completely.

The following is an example of HAProxy statistics.

The "balance roundrobin" line defines the type of Server Load balancer we will use. In this tutorial, we will use a simple scheduling algorithm, which is more than enough for HTTP Server Load balancer. HAProxy also provides other types of Load Balancing:

• Leastconn:Servers with the least number of connections receive connections first.

• Source:Hash the source IP address by dividing the total weight of the running server by the hash value to determine which server will receive the request.

• Uri:(Before the question mark on the left of the URI) is hashed, and the total weight of the running server is divided by the hash value. The result determines which server will receive the request.

• Url_param:The URL parameter specified in the variable is queried in the query string of each http get request. You can basically lock requests that use a deliberately crafted URLcrafted URL to a specific Server Load balancer node.

• Hdr (name ):The HTTP header <name> is queried in each HTTP request and directed to a specific node.

The Line "cookie LBN insert indirect nocache" allows our Server Load balancer system to store persistent cookies, which allows us to accurately identify which node in the cluster is used for a session. These Node cookies are stored together with the specified name. In our example, "LBN" is used, but you can specify any name you like. The node stores the string as a value for the cookie.

 
 
  1. server web01 192.168.100.2:80 cookie node1 check   
  2. server web02 192.168.100.3:80 cookie node2 check   

The preceding section defines the website server node cluster. Each server is represented by an internal name such as web01 and web02), an IP address, and a unique cookie string. Cookie strings can be defined as any name you need. I used simple node1, node2... node (n ).

Start HAProxy

After completing the configuration, you can start HAProxy to verify that everything runs as expected.


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.