CentOS 6.4 installation configuration HAProxy-1.4.24
HAProxy is a free, fast, and reliable proxy solution that supports high availability and Server Load balancer. It is also suitable for proxies for TCP and HTTP-based applications. For some websites with high load, HAProxy is particularly suitable. HAProxy supports tens of thousands of concurrent connections. It is easy to configure and can easily integrate into our existing application architecture.
Next, we will install and configure HAProxy on CentOS 6.4.
Install configurations
Follow these steps:
1
wgethttp://haproxy.1wt.eu/download/1.4/src/haproxy-1.4.24.
tar
.gz
2
tar
xvzf haproxy-1.4.24.
tar
.gz
3
cd
haproxy-1.4.24
4
make
TARGET=linux26
5
make
install
By default, the storage path of the configuration file corresponding to HAProxy is/etc/haproxy. cfg.
The default configuration file is as follows:
01
#---------------------------------------------------------------------
02
# Example configuration for a possible web application. See the
03
# full configuration options online.
04
#
05
#http://haproxy.1wt.eu/download/1.4/doc/configuration.txt
06
#
07
#---------------------------------------------------------------------
08
09
#---------------------------------------------------------------------
10
# Global settings
11
#---------------------------------------------------------------------
12
global
13
# to have these messages end up in /var/log/haproxy.log you will
14
# need to:
15
#
16
# 1) configure syslog to accept network log events. This is done
17
# by adding the '-r' option to the SYSLOGD_OPTIONS in
18
# /etc/sysconfig/syslog
19
#
20
# 2) configure local2 events to go to the /var/log/haproxy.log
21
# file. A line like the following can be added to
22
# /etc/sysconfig/syslog
23
#
24
# local2.* /var/log/haproxy.log
25
#
26
log 127.0.0.1 local2
27
28
chroot /var/lib/haproxy
29
pidfile /var/run/haproxy.pid
30
maxconn 4000
31
user haproxy
32
group haproxy
33
daemon
34
35
# turn on stats unix socket
36
stats socket /var/lib/haproxy/stats
37
38
#---------------------------------------------------------------------
39
# common defaults that all the 'listen' and 'backend' sections will
40
# use if not designated in their block
41
#---------------------------------------------------------------------
42
defaults
43
mode http
44
log global
45
option httplog
46
option dontlognull
47
option http-server-close
48
option forwardfor except 127.0.0.0/8
49
option redispatch
50
retries 3
51
timeout http-request 10s
52
timeout queue 1m
53
timeout connect 10s
54
timeout client 1m
55
timeout server 1m
56
timeout http-keep-alive 10s
57
timeout check 10s
58
maxconn 3000
59
60
#---------------------------------------------------------------------
61
# main frontend which proxys to the backends
62
#---------------------------------------------------------------------
63
frontend main *:5000
64
acl url_static path_beg -i /static /images /javascript /stylesheets
65
acl url_static path_end -i .jpg .gif .png .css .js
66
67
use_backend static if url_static
68
default_backend app
69
70
#---------------------------------------------------------------------
71
# static backend for serving up images, stylesheets and such
72
#---------------------------------------------------------------------
73
backend static
74
balance roundrobin
75
server static 127.0.0.1:4331 check
76
77
#---------------------------------------------------------------------
78
# round robin balancing between the various backends
79
#---------------------------------------------------------------------
80
backend app
81
balance roundrobin
82
server app1 127.0.0.1:5001 check
83
server app2 127.0.0.1:5002 check
84
server app3 127.0.0.1:5003 check
85
server app4 127.0.0.1:5004 check
Let's give a simple explanation of the content of the above configuration file, which can be expanded as appropriate:
Global segments are used to configure process-level parameters. Based on the parameter function, the official website classifies global configuration parameters into three groups:
- Process Management and Security
- Performance Tuning
- Debugging
For more information, see the document.
The ults segment mainly refers to the default configuration segment of the proxy configuration and sets the default parameters. These default configurations can be used in other sections configured later. If you want to modify the default configuration parameters in other segments, you only need to overwrite the configuration items that appear in the defaults segment.
For details about the parameters that can be configured for the ults segment, refer to the official website documentation.
The frontend section mainly configures the Socket-related attributes of the front-end listener, that is, the virtual node that receives the request link. In addition to configuring these static attributes, you can also redirect requests to the configured backend based on certain rules. The backend may be configured as a server, it may also be a group of servers (clusters ).
The backend segment mainly refers to the information of the configured actual server. It is forwarded to the backend configured server through the redirection request configured by frontend.
The listen segment integrates the frontend and backend segments and forwards requests directly from the proxy to the actual backend server.
Start the HAProxy agent
It is very easy to start. Execute the following command:
1
sudo
haproxy -f /etc/haproxy/haproxy.cfg
Let's simply modify the configuration file and configure a server to balance the backend SolrCloud search cluster, as shown below:
01
#---------------------------------------------------------------------
02
# Global settings
03
#---------------------------------------------------------------------
04
global
05
log 127.0.0.1 local2
06
07
chroot /var/lib/haproxy
08
pidfile /var/run/haproxy.pid
09
maxconn 4000
10
user haproxy
11
group haproxy
12
daemon
13
14
# turn on stats unix socket
15
stats socket /var/lib/haproxy/stats
16
17
#---------------------------------------------------------------------
18
# common defaults that all the 'listen' and 'backend' sections will
19
# use if not designated in their block
20
#---------------------------------------------------------------------
21
defaults
22
mode http
23
log global
24
option httplog
25
option dontlognull
26
option http-server-close
27
option forwardfor except 127.0.0.0/8
28
option redispatch
29
retries 3
30
timeout http-request 10s
31
timeout queue 1m
32
timeout connect 10s
33
timeout client 1m
34
timeout server 1m
35
timeout http-keep-alive 10s
36
timeout check 10s
37
maxconn 3000
38
39
#---------------------------------------------------------------------
40
# main frontend which proxys to the backends
41
#---------------------------------------------------------------------
42
frontend haproxy-lbserver
43
bind 0.0.0.0:80
44
acl url_solr_path path_beg /solr-cloud
45
acl url_static path_beg -i /static /images /javascript /stylesheets
46
acl url_static path_end -i .jpg .gif .png .css .js
47
48
use_backend static if url_static
49
use_backend solr-cloud if url_solr_path
50
default_backend static
51
52
#---------------------------------------------------------------------
53
# static backend for serving up images, stylesheets and such
54
#---------------------------------------------------------------------
55
backend static
56
balance roundrobin
57
server static 127.0.0.1:4331 check
58
59
#---------------------------------------------------------------------
60
# round robin balancing between the various backends
61
#---------------------------------------------------------------------
62
backend solr-cloud
63
balance roundrobin
64
server solr-1 slave1:8888 check
65
server solr-2 slave2:8888 check
66
server solr-3 slave3:8888 check
67
server solr-4 slave4:8888 check
Frontend is named haproxy-lbserver. In fact, it is mapped to a specific service IP address, bound to port 80, and then the Request Path is set to/solr-cloud, that is, the frontend receives a request similar to "http: // link starting with haproxy-lbserver/solr-cloud. Other request parameters can be added later.
In frontend, The use_backend command is used to specify a backend to be forwarded. The name is solr-cloud. You can use the filter condition command if to specify the backend name after the use_backend command.
Backend specifies the configuration of the actual Cluster Server and performs load balancing on it. A total of four Solr search servers are specified and the roundrobin load balancing policy is used.
We copy the default configuration file to the directory/home/hadoop/shiyanjun/haproxy-1.4.24/conf and then start haproxy:
1
sudo
haproxy -f /home/hadoop/shiyanjun/haproxy-1.4.24/conf/haproxy.cfg
After the startup is successful, you can access a request link similar to the following:
1
Http: // haproxy-lbserver/solr-cloud/mycollection/select? Q = Beijing & fl = * & fq = building_type: 1 & start = 0 & rows = 10
HAProxy forwards the request to the backend Cluster Server for actual request processing.
The official HAProxy documentation is quite detailed. We recommend that you refer to the official documentation to learn about the corresponding configuration options and usage methods.
Reference
- Http://haproxy.1wt.eu
- Http://haproxy.1wt.eu/download/1.4/src/haproxy-1.4.24.tar.gz
- Http://cbonte.github.io/haproxy-dconv/configuration-1.4.html