Explain the process of installing and configuring haproxy in Linux system

Time:2020-9-19

1、 Haproxy concept

Haproxy provides high availability, load balancing and proxy based on TCP and HTTP applications. It supports virtual hosts. It is a free, fast and reliable solution. Haproxy is especially suitable for web sites with heavy load, which usually need to maintain or seven layers of processing. Haproxy runs on current hardware and can support tens of thousands of concurrent connections. And its operation mode makes it easy and safe to integrate into your current architecture, while protecting your web server from being exposed to the network.

2、 Install haproxy

Copy code

The code is as follows:

[[email protected] app]# tar zxvf haproxy-1.4.24.tar.gz
[[email protected] app]# mv haproxy-1.4.24 haproxy
[[email protected] app]# cd haproxy
[[email protected] hadoop]# uname -a
Linux node3 2.6.18-164.el5 #1 SMP Tue Aug 18 15:51:54 EDT 2009 i686 i686 i386 GNU/Linux
[[email protected] haproxy]# make TARGET=linux24 prefix=/usr/local/haproxy
gcc -Iinclude -Iebtree -Wall -O2 -g -fno-strict-aliasing -DTPROXY -DCONFIG_HAP_CRYPT -DENABLE_POLL -DNETFILTER -DUSE_GETSOCKNAME -DCONFIG_HAPROXY_VERSION=\”1.4.24\” -DCONFIG_HAPROXY_DATE=\”2013/06/17\” \
-DBUILD_TARGET='”linux24″‘ \
-DBUILD_ARCH='””‘ \
-DBUILD_CPU='”generic”‘ \
-DBUILD_CC='”gcc”‘ \
-DBUILD_CFLAGS='”-O2 -g -fno-strict-aliasing”‘ \
-DBUILD_OPTIONS='””‘ \
-c -o src/haproxy.o src/haproxy.c
[[email protected] haproxy]# make install PREFIX=/usr/local/haproxy
install -d /usr/local/haproxy/sbin
install haproxy /usr/local/haproxy/sbin
install -d /usr/local/haproxy/share/man/man1
install -m 644 doc/haproxy.1 /usr/local/haproxy/share/man/man1
install -d /usr/local/haproxy/doc/haproxy
for x in configuration architecture haproxy-en haproxy-fr; do \
install -m 644 doc/$x.txt /usr/local/haproxy/doc/haproxy ; \
done

3、 Configure haproxy

The haproxy configuration is divided into five parts. Of course, these components are not required and can be selected as configuration according to needs.

Global: parameters are process level and are usually operating system (OS) – related. These parameters are generally set only once. If the configuration is correct, there is no need to modify the configuration again;

· default: if the default parameters are configured, these parameters can be used to configure the frontend, backend, and listen components;

Front end: the front-end virtual node that receives the request. Frontend can directly specify the backend of the back-end according to the rules (dynamically selected);

Back end: the configuration of the back-end service cluster, which is a real server. One backend corresponds to one or more entity servers;

· listen: the combination of front and back end.

Compilation haproxy.cfg Document:

Copy code

The code is as follows:

global
maxconn 51200
chroot /usr/local/haproxy
uid 99
gid 99
daemon
#quiet
Nbproc 1 ා number of processes
pidfile /usr/local/haproxy/logs/haproxy.pid

defaults
Mode http ා the default mode is mode {TCP | http | health}. TCP is layer 4 and HTTP is layer 7. Health only returns OK
#Retries 2 ා if the connection fails twice, the server will be considered unavailable, which can also be set later
Option redispatch ා when the server corresponding to the serverid is down, it is forced to direct to other healthy servers
Option abortonclose ා when the server load is very high, automatically end the link that the current queue has been processing for a long time
Timeout connect 5000ms
Timeout client 30000ms ා client timeout
Timeout server 30000ms ා server timeout
#Timeout check 2000 ා = heartbeat detection timeout
log 127.0.0.1 local0 err #[err warning info debug]
Load balancing algorithm
#Option httplog ා log category, using httplog
#Option httpclose ා close the HTTP channel after each request. Ha proxy does not support keep alive and can only simulate the implementation of this mode
# option dontlognull
#Option forward for ා if the back-end server needs to obtain the parameters that the real IP of the client needs to configure, it can obtain the client IP from the HTTP header

listen admin_stats
Bind 0.0.0.0:8888
Option httplog ා uses HTTP log format
Stats refresh 30s ා statistics page auto refresh time
Stats URI / stats ා statistics page URL
Stats realm haproxy manager ා prompt text in password box of Statistics page
stats auth admin:admin # Statistics page user name and password settings
#Stats hide version ා hide the version information of haproxy on the statistics page

listen test1
bind :12345
mode tcp
server t1 192.168.1.101:8881
server t2 192.168.1.102:8881

listen test2 :80
option httpclose
option forwardfor
server s1 192.168.1.101:8080 check weight 1 minconn 1 maxconn 3 check inter 40000
server s2 192.168.1.102:8080 check weight 1 minconn 1 maxconn 3 check inter 40000

4、 Start haproxy

Copy code

The code is as follows:

[[email protected] haproxy]# /usr/local/haproxy/sbin/haproxy -f /usr/local/haproxy/haproxy.cfg
[[email protected] app]# ps -ef |grep haproxy |grep -v grep
root 6950 1 0 19:35 ? 00:00:00 /usr/local/haproxy/sbin/haproxy -f /usr/local/haproxy/haproxy.cfg

5、 Start and close haproxy through script

1. Write haproxy script

Copy code

The code is as follows:

[[email protected] ~]# cat /etc/rc.d/init.d/haproxy
#!/bin/bash
BASE_DIR=”/usr/local/haproxy”
ARGV=”[email protected]

start()
{
echo “START HAPoxy SERVERS”
$BASE_DIR/sbin/haproxy -f $BASE_DIR/haproxy.cfg
}

stop()
{
echo “STOP HAPoxy Listen”
kill -TTOU $(cat $BASE_DIR/logs/haproxy.pid)
echo “STOP HAPoxy process”
kill -USR1 $(cat $BASE_DIR/logs/haproxy.pid)
}
case $ARGV in

start)
start
ERROR=$?
;;

stop)
stop
ERROR=$?
;;

restart)
stop
start
ERROR=$?
;;

*)
echo “hactl.sh [start|restart|stop]”
esac
exit $ERROR

2. Let the script start automatically with the system

Copy code

The code is as follows:

[[email protected] ~]# chmod +x /etc/rc.d/init.d/haproxy
[[email protected] ~]# chkconfig –add haproxy
service haproxy does not support chkconfig
—-The solution to the above problem is to add the following two sentences to / etc / rc.d/init.d/haproxy after ා / bin / bash
[[email protected] ~]# cat /etc/rc.d/init.d/haproxy
#!/bin/bash
#chkconfig: 2345 10 90
#description:haproxy
BASE_DIR=”/usr/local/haproxy”
ARGV=”[email protected]

start() 
—-Among them, 2345 is the default startup level, and there are 7 levels from 0 to 6.
—-Level 0 means: shutdown
—-Level 1 means single user mode
—-Level 2 means multi-user command line mode without network connection
—-Level 3 means multi-user command line mode with network connection
—-Level 4 means: not available
—-Level 5 indicates multi-user mode with graphical interface
—-Level 6 means restart
—-10 is the start priority, 90 is the shutdown priority, the priority range is 0-100, the larger the number, the lower the priority.

3. Start and stop haproxy

Copy code

The code is as follows:

[[email protected] ~]# service haproxy stop
STOP HAPoxy Listen
STOP HAPoxy process
[[email protected] ~]# ps -ef |grep haproxy |grep -v grep</p>
<p>[[email protected] ~]# service haproxy start
START HAPoxy SERVERS
[[email protected] ~]# ps -ef |grep haproxy |grep -v grep
root 11259 1 0 15:33 ? 00:00:00 /usr/local/haproxy/sbin/haproxy -f /usr/local/haproxy/haproxy.cfg

6、 Haproxy log configuration
Haproxy does not log by default, except in the haproxy.conf In addition to specifying the output of the log, the configuration file of the system log needs to be configured. Taking CentOS 6.4 as an example, haproxy uses the RPM provided by the system to report version 1.4

1、vim /etc/haproxy/haproxy.conf

Copy code

The code is as follows:

global
Log 127.0.0.1 local3 ා local3 is the device, corresponding to / etc/ rsyslog.conf By default, the log level of recycle info is set
maxconn 1024
user haproxy
group haproxy
daemon
pidfile /var/run/haproxy.pid
defaults
mode http
log global
option httplog
option dontlognull
option http-server-close
option forwardfor except 127.0.0.0/8
retries 2
option redispatch
maxconn 1024

2. Edit system log configuration

Copy code

The code is as follows:

vim /etc/rsyslog.conf

The following settings are set by default. The configuration file in the / etc / rsyslog. D / *. Conf directory will be read

Copy code

The code is as follows:

$IncludeConfig /etc/rsyslog.d/*.conf

Create a separate configuration file for haproxy

Copy code

The code is as follows:

vim /etc/rsyslog.d/haproxy.conf
$ModLoad imudp
$UDPServerRun 514
local3.* /var/log/haproxy.log
#If you do not add the following configuration, you can set the/ haproxy.log In addition to the log, the message file is also written
&~

3. Configure the main configuration file of rsyslog and open the remote log

Copy code

The code is as follows:

vim /etc/sysconfig/rsyslog
SYSLOGD_OPTIONS=”-c 2 -r -m 0″
#-C 2 uses compatibility mode, the default is – C 5
#-R enable remote log
#-M 0 marks the time stamp. The unit is minutes. When it is 0, this function is disabled

After configuration, restart the haproxy and rsyslog services

Copy code

The code is as follows:

/etc/init.d/rsyslog restart
/etc/init.d/haproxy restart

4. The configuration of system log on red hat 5 is different from that of red hat 6. The / etc of red hat 5 is different/ rsyslog.conf It does not contain / etc / rsyslog. D / *. Conf, which is configured as follows

Copy code

The code is as follows:

#Join haproxy.none Indicates that the haproxy log is not written to message
$ModLoad imudp
$UDPServerRun 514
*.info;mail.none;authpriv.none;cron.none;local3.none /var/log/messages
local3.* /var/log/haproxy.log