Haproxy Web load Balancing cluster with Nginx

Source: Internet
Author: User
Tags nginx server haproxy rsyslog

Brief introduction

Haproxy is a free and open source software written in the C language that provides high availability, load balancing, and application proxies based on TCP and HTTP. Haproxy is especially useful for Web sites that are heavily loaded, and often require session-hold or seven-tier processing. The haproxy runs on the current hardware and can support tens of thousands of concurrent connections. and its operating mode makes it easy and safe to integrate into your current architecture, while protecting your Web server from being exposed to the web.

Haproxy scheduling algorithm principle one, RR (Round Robin). Polling scheduling

Understanding Examples:
There are three nodes a, B, C, the first user access is assigned to Node A, the second user access is assigned to node B, the third user access is assigned to node C, and the fourth user access continues to be assigned to Node A, polling the allocation access request for load balancing effect. This algorithm also has a weighted poll that assigns access requests based on the weight of each node poll

Second, LC (Least Connections). LC algorithm, which is the minimum number of connections, dynamically allocates front-end requests based on the size of the backend node connections

Understanding Examples:
There are three nodes a, B, C, the number of connections for each node is a:4, B:5, C:6, at this time if there is a first user connection request, will be assigned to a, the number of connections to A:5, B:5, C:6, the second user request will continue to be divided into a, the number of connections into A:6, B:5, C:6; A new request is assigned to B, each time assigning a new request to the client with the least number of connections. Because the connection number of a, B and C will be released dynamically, it is difficult to get the same number of connections, so this algorithm has a great improvement in comparison with the RR algorithm, which is currently used in more than one algorithm.

Three, SH (Source Hashing). SH is based on the source access scheduling algorithm, this algorithm is used for some of the session sessions recorded on the server side of the scene, can be based on the source of IP, cookies, etc. to do cluster scheduling

Understanding Examples:
Using the cluster scheduling algorithm based on the source p, there is a node A, B, C, the first user of the first access is assigned to a, the second user is assigned to the first access B, when the first user on the second visit will be continued to assign to a, the second user will still be assigned to the second access to B, As long as the load Balancer Scheduler does not restart, the first user access will be assigned to a, and the second user access will be assigned to B to implement the scheduling of the cluster. The advantage of this scheduling algorithm is to achieve session retention, but some IP traffic is very large when the load is unbalanced, some of the node access is very large, affecting business use

Experimental topology

Experimental environment
Host Operating System IP Address main software
Haproxy Server CentOS 7.4 x86_64 192.168.100.71 Haproxy-1.4.24.tar.gz
Nginx Server 01 CentOS 7.4 x86_64 192.168.100.72 Nginx-1.12.0.tar.gz
Nginx Server 02 CentOS 7.4 x86_64 192.168.100.73 Nginx-1.12.0.tar.gz
Client Side CentOS 7.4 x86_64 192.168.100.74
Build step One, NGIXN server 01 configuration 1, install dependent packages

[Email protected]_2 ~]# yum-y install gcc gcc-c++
[Email protected]_2 ~]# yum-y Install Pcre-devel zlib-devel

2. Create Nginx Process User

[Email protected]_2 ~]# useradd-m-s/sbin/nologin nginx

3, source installation Nginx

[Email protected]_2 ~]# tar xvfz nginx-1.12.0.tar.gz
[Email protected]_2 ~]# CD nginx-1.12.0/
[Email protected]_2 nginx-1.12.0]#/configure--prefix=/usr/local/nginx--user=nginx--group=nginx
[[Email protected]_2 nginx-1.12.0]# make && make install

4, optimize the path

[Email protected]_2 ~]# ln-s/usr/local/nginx/sbin/nginx/usr/sbin/

5. Edit Site Homepage

[Email protected]_2 ~]# cd/usr/local/nginx/html/
[Email protected]_2 html]# echo "Welcome to Nginx aaaaaaaaaa" > index.html

6. Start Nginx

[Email protected]_2 ~]# Nginx
[[Email protected]_2 ~]# NETSTAT-ANPT | grep ": 80"

Second, NGIXN server 02 Configuration configuration method and NGIXN server 01 the same place slightly ... 1. Edit Site Homepage

[Email protected]_3 ~]# cd/usr/local/nginx/html/
[Email Protected]_3 html]# echo "Welcome to Nginx bbbbbbbbbb" > index.html

Third, test page

Iv. haproxy Server Configuration 1, install dependent packages

[Email protected]_1 ~]# yum-y install gcc gcc-c++
[Email protected]_1 ~]# yum-y Install Pcre-devel zlib-devel

2, Installation Haproxy

[Email protected]_1 ~]# tar zxvf haproxy-1.4.24.tar.gz
[Email protected]_1 ~]# CD haproxy-1.4.24/
[Email protected]_1 haproxy-1.4.24]# make target=linux26 #TARGET代表64位的操作系统
[Email protected]_1 haproxy-1.4.24]# make install

3. Establish Haproxy configuration file

[Email protected]_1 ~]# Mkdir/etc/haproxy
[Email protected]_1 ~]# CD haproxy-1.4.24/examples/
[Email protected]_1 examples]# CP haproxy.cfg/etc/haproxy/

4. Detailed configuration file

[Email protected]_1 ~]# vim/etc/haproxy/haproxy.cfg

Global configuration

Global
Log 127.0.0.1 local0 #配置日志记录, local0 to log device, default to System log
Log 127.0.0.1 local1 Notice #notice为日志级别, usually with 24 levels
Maxconn 4096 #最大连接数
Chroot/usr/share/haproxy
UID #用户uid
GID #用户gid
Daemon

Default configuration

Defaults
Log Global #定义日志为global配置中的日志定义
Mode http #模式为http
Option Httplog #采用http日志格式记录日志
Option Dontlognull
Retries 3 #检查节点服务器失败次数, three consecutive failures, the node is considered unavailable
Option Redispatch
Maxconn #最大连接数
Contimeout #连接超时时间
Clitimeout 50000 #客户端超时时间
Srvtimeout 50000 #服务器超时时间

Application Component Configuration

Listen Webcluster 0.0.0.0:80 #定义webcluster application
Option Httpchk get/index.html #检查服务器的index. html
Option persist #强制将请求发送到已经down掉的服务器
Balance Roundrobin #负载均衡调度算法使用轮询算法
Server Inst1 192.168.100.72:80 Check Inter Fall 3 #定义在线节点
Server Inst2 192.168.100.73:80 check Inter fall 3

5, optimize the path, and start the Haproxy service

[Email protected]_1 ~]# CP Haproxy-1.4.24/examples/haproxy.init/etc/init.d/haproxy
[Email protected]_1 ~]# ln-s/usr/local/sbin/haproxy/usr/sbin/haproxy
[Email protected]_1 ~]# chmod +x/etc/init.d/haproxy
[Email protected]_1 ~]# Mkdir/usr/share/haproxy
[[Email protected]_1 ~]# service Haproxy start

V. Test Haproxy Cluster

Vi. Configuring Haproxy Log Management 1, editing the Haproxy configuration file

[Email protected]_1 ~]# vim/etc/haproxy/haproxy.cfg

Add the following parameters:

Log/dev/log local0 Info
Log/dev/log local0 Notice

[Email protected]_1 ~]# systemctl restart Haproxy.service #重启haproxy服务

2. Modify Rsyslog Configuration

[Email protected]_1 ~]# touch/etc/rsyslog.d/haproxy.conf #创建独立配置文件
[Email protected]_1 ~]# vim/etc/rsyslog.d/haproxy.conf

Add the following parameters:

if ($programname = = ' Haproxy ' and $syslogseverity-text = = ' Info ')
Then-/var/log/haproxy/haproxy-info.log #info日志记录路径
&~
if ($programname = = ' Haproxy ' and $syslogseverity-text = = ' notice ')
Then-/var/log/haproxy/haproxy-notice.log #notice日志记录路径
&~ # "&~" when the log is written to the journal file, Rsyslog stops processing this information

3. Save the configuration file and start Rsyslog

[Email protected]_1 ~]# systemctl start Rsyslog.service

4. View Logs

[Email protected] ~]# cd/var/log/haproxy/
[[email protected] haproxy]# ls

Haproxy Web load Balancing cluster with Nginx

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.