Haproxy achieves load balancing and dynamic/static Separation

Source: Internet
Author: User
Tags haproxy rsyslog

Haproxy achieves load balancing and dynamic/static Separation

After haprxy Server Load balancer and dynamic/static separation, haproxy is more professional in these two aspects than nginx, including session persistence, cookie guidance, and url monitoring backend, mysql Server Load balancer is not available in nginx, but it cannot be said that haprox can replace nginx, and nginx cache is not available in proxy.

Next we will use haproxy to do some basic configuration.

Haproxy: 192.168.3.124

Node1: 192.168.3.128

Node2: 192.168.3.129

I. Basic Configuration:

1. Install proxy

Yum install haproxy-y

Current stable version 1.5.X

Cp/etc/haproxy. cfg/etc/haproxy. cfg. bak

2. Enable the proxy System Log

Vim/etc/rsyslog. conf
$ ModLoad imudp
$ UDPServerRun 514
Local2. */var/log/haproxy. log
Service rsyslog restart

3. modify the configuration file to implement simple Load Balancing

Global
Log 127.0.0.1 local2
Chroot/var/lib/haproxy
Pidfile/var/run/haproxy. pid
Maxconn 4000 # defines the maximum number of connections for each haproxy process. Since each connection includes one client and one server, the maximum number of TCP sessions for a single process is twice the value.
User haproxy
Group haproxy
Daemon # Run as a daemon
Nbproc 16 # set the number of processes when haproxy starts. According to the explanation in the official documentation, I understand that the setting of this value should be consistent with the number of CPU cores on the server, that is, the common two eight-core CPU servers, that is, a total of 16 cores, you can set the value to: <= 16, create multiple processes, it can reduce the task queue of each process, but too many processes may also cause the process to crash. Here I set it to 16
# Turn on stats unix socket
Stats socket/var/lib/haproxy/stats
Ults
Mode http
Log global
Option httplog
Option dontlognull # enable this option. No empty connections are logged in the log. The so-called empty connection is when the upstream Server Load balancer or monitoring system needs to regularly connect to or obtain a fixed component or page to detect whether the service is alive or available, or whether the scan port is listening or opening is called an empty connection. this parameter is not recommended if no other Server Load balancer instance exists in the upstream of the service, because malicious scans or other actions on the Internet are not recorded
Option http-server-close # if some servers do not support http persistent connections, you can use this parameter to use the client-to-haproxy for persistent connections, while haproxy for short connections to the server.
Option forwardfor partition t 127.0.0.0/8
Option redispatch # When a cookie is used, haproxy inserts the serverID of the backend server requested by haproxy into the cookie to ensure SESSION persistence, if the backend server goes down, but the client's cookie is not refreshed, if this parameter is set, the customer's request will be forcibly directed to another backend server, to ensure normal service.
Retries 3 # defines the number of failed reconnections to the backend server. When the number of connection failures exceeds this value, the corresponding backend server is marked as unavailable.
Timeout http-request 10 s
Timeout queue 1 m
Timeout connect 10 s
Timeout client 1 m
Timeout server 1 m
Timeout http-keep-alive 10 s
Timeout check 10 s
Maxconn 3000
Frontend main *: 80 # define a front-end part named main
Default_backend app # the backend server to which it is mapped by default without any matching conditions
Backend app # define a backend part named app
Balance roundrobin
Server app1 192.168.3.128: 80 check
Server app2 192.168.3.129: 80 check
Stats enable # enable the server status monitoring page

After the configuration is complete, restart haproxy.

Service haproxy restart

4. Test

[Root @ usvr-124 haproxy] # curl 192.168.3.124/1.html

Hello, 192.168.3.129

[Root @ usvr-124 haproxy] # curl 192.168.3.124/1.html

Hello, 192.168.3.128

From this we can see that the load balancing policy is round-robin access.

5. Let's take a look at the status monitoring page.

Through the above basic configuration, we have almost understood the working mechanism of haproxy. Let's take a look at the next step.

2. Implementing static/dynamic separation of access pages

Global
Chroot/var/lib/haproxy
Pidfile/var/run/haproxy. pid
Maxconn 4000
User haproxy
Group haproxy
Daemon
# Turn on stats unix socket
Stats socket/var/lib/haproxy/stats
Ults
Mode http
Log global
Option httplog
Option dontlognull
Option http-server-close
Option forwardfor partition t 127.0.0.0/8
Option redispatch
Retries 3
Timeout http-request 10 s
Timeout queue 1 m
Timeout connect 10 s
Timeout client 1 m
Timeout server 1 m
Timeout http-keep-alive 10 s
Timeout check 10 s
Maxconn 3000
Frontend main *: 80
Acl url_static path_beg-I/static/images/javascript/stylesheets # create an acl Policy path_beg: access path starting with/static/images.-I ignores case sensitivity.
Acl url_static path_end-I. jpg. gif. png. css. js # create an acl Policy path_end: access path ending with .jpg. gif, etc.-I ignore case sensitivity

Use_backend static if url_static # if the acl Policy url_static is matched, the backend is static.
Default_backend app # use the default backend app without any configuration

Backend static
Balance roundrobin # Round-Robin access
Server static 192.168.3.128: 80 check

Backend app
Balance roundrobin # Round-Robin access
Server app2 192.168.3.129: 80 check

Here we have created several new acl policies for implementing

1. when accessing the paths starting with/static/images/javascript/stylesheetsand ending with .jpg. gif. png. css. js, point to the backend server 192.168.3.180

2. If there is no matching, point to the default backend server 192.168.3.129

I will not post the access results.

Conclusion: through the two examples above, we have learned about the power of haproxy in load balancing, and of course we can also achieve mysql read load balancing, for more advanced features, let's get to know it.

Haproxy + Keepalived build Weblogic high-availability server Load balancer Cluster

Keepalived + HAProxy configure high-availability Load Balancing

Haproxy + Keepalived + Apache configuration notes in CentOS 6.3

Haproxy + KeepAlived WEB Cluster on CentOS 6

Haproxy + Keepalived build high-availability Load Balancing

Haproxy + Apache for Linux Server Load balancer Software

For details about HAproxy, click here
HAproxy: click here

This article permanently updates the link address:

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.