HaProxy installation and common commands.md

Install haproxy

For haproxy installation, there are a large number of resources available on the web to refer to, most commonly using yum and compiled installation.

yum install haproxy

CentOS comes with haproxy, but it may be an older version.A stable version of haproxy can be found on the IUS source.

The main steps are as follows:

(1) Configure yum source

cat <<eof>/etc/yum.repos.d/ius-7.repo
[ius]
name = IUS for Enterprise Linux 7 - $basearch
baseurl = https://repo.ius.io/7/$basearch/
enabled = 1
repo_gpgcheck = 0
gpgcheck = 1
gpgkey = https://repo.ius.io/RPM-GPG-KEY-IUS-7
[ius-debuginfo]
name = IUS for Enterprise Linux 7 - $basearch - Debug
baseurl = https://repo.ius.io/7/$basearch/debug/
enabled = 0
repo_gpgcheck = 0
gpgcheck = 1
gpgkey = https://repo.ius.io/RPM-GPG-KEY-IUS-7
[ius-source]
name = IUS for Enterprise Linux 7 - Source
baseurl = https://repo.ius.io/7/src/
enabled = 0
repo_gpgcheck = 0
gpgcheck = 1
gpgkey = https://repo.ius.io/RPM-GPG-KEY-IUS-7
eof

(2) Clean up, update and quickly generate the yum cache

yum clean all
yum update
yum makecache fast

(3) See what haproxy versions are in the system

yum list|grep haproxy

(4) yum installation haproxy18u

#Install haproxy18u here 

yum install haproxy18u -y

Compile and install haproxy

When compiling and installing haproxy, you can use the pcre environment, where regular expression analysis compiles much faster, but you can install without it.

The main steps are as follows:

(1) Install pcre pcre-devel

yum -y install pcre pcre-devel

(2) Create users and groups to run haproxy

groupadd -r haproxy
useradd -g haproxy -M -s /sbin/nologin haproxy

(3) Compile and install haproxy

#Download address:
http://www.haproxy.org/ 
#Unzip:
tar zxf haproxy-1.8.23.tar.gz -C /usr/local
#Compile and Install:
cd /usr/local/haproxy-1.8.23
make TARGET=linux3100 ARCH=x86_64 PREFIX=/usr/local/haproxy USE_PCRE=1
make install PREFIX=/usr/local/haproxy

Description:

make requires TARGET to specify the kernel and version, which can be viewed through uname-r as follows:

- linux22     for Linux 2.2
- linux24     for Linux 2.4 and above (default)
- linux24e    for Linux 2.4 with support for a working epoll (> 0.21)
- linux26     for Linux 2.6 and above
- linux2628   for Linux 2.6.28, 3.x, and above (enables splice and tproxy)
- solaris     for Solaris 8 or 10 (others untested)
- freebsd     for FreeBSD 5 to 10 (others untested)
- netbsd      for NetBSD
- osx         for Mac OS/X
- openbsd     for OpenBSD 5.7 and above
- aix51       for AIX 5.1
- aix52       for AIX 5.2
- cygwin      for Cygwin
- haiku       for Haiku
- generic     for any other OS or version.
- custom      to manually adjust every setting

Use ARCH to specify the schema, but the ARCH option is optional.Using USE_PCR E=1 means using the PCR E environment to compile faster.

The operating system kernel version for this article is 3.10.0, and TARGET is specified as linux2628.I did not make a mistake using linux3100 here either.

After compilation and installation, there are only three directories: doc, share, and sbin, and there is only one haproxy main program, haproxy, in sbin.

(4) Manage haproxy services

ln -s /usr/local/haproxy/sbin/haproxy /usr/sbin/haproxy
cd /usr/local/haproxy-1.8.23/examples
cp haproxy.init /etc/rc.d/init.d/haproxy
cd /etc/rc.d/init.d
chmod +x haproxy

(5) Set up haproxy-related profiles

#Create Configuration Directory
mkdir /usr/local/haproxy/conf 
#Create master profile
touch /usr/local/haproxy/conf/haproxy.cfg
#Create startup script configuration directory
mkdir /etc/haproxy/  
#Add Soft Connection to Profile
ln -s /usr/local/haproxy/conf/haproxy.cfg /etc/haproxy/haproxy.cfg 
#Create Log Directory
mkdir /usr/local/haproxy/log
#Create log file
touch /usr/local/haproxy/log/haproxy.log
#Add Soft Connection
ln -s /usr/local/haproxy/log/haproxy.log /var/log/haproxy.log  
#Copy error page
cp -r /usr/local/haproxy-1.8.23/examples/errorfiles/ /usr/local/haproxy/
#Add Soft Connection to Error Page
ln -s /usr/local/haproxy/errorfiles /etc/haproxy/errorfiles

(6) Configure haproxy.cfg parameters

#Edit haproxy.cfg configuration file
vim /usr/local/haproxy/conf/haproxy.cfg    

#---------------------------------------------------------------------
# Global settings
#---------------------------------------------------------------------
global
 log 127.0.0.1 local2   ###[err warning info debug] 
 chroot /usr/local/haproxy
 pidfile /var/run/haproxy.pid ###pid store path for haproxy. Users who start the process must have access to this file 
 maxconn 4000     ###Maximum number of connections, default 4000
 user haproxy
 group haproxy
 daemon       ###Create a process to run in deamon mode.This parameter requires that the run mode be set to "daemon"

#---------------------------------------------------------------------
# common defaults that all the 'listen' and 'backend' sections will 
# use if not designated in their block
#---------------------------------------------------------------------
defaults
 mode http    ###Default mode {tcp | http | health}, tcp is layer 4, HTTP is layer 7, health only returns OK
 log global   ###Logs with global definition
 option dontlognull  ###Do not log health checks
 option httpclose  ###Actively close the http channel after each request 
 option httplog   ###Log Category http Log Format 
 option forwardfor  ###Client ip can be obtained from Http Header if the back-end server needs to obtain the parameters that the client's true ip needs to be configured 
 option redispatch  ###Force targeting to other healthy servers after serverId's corresponding server hangs up
 timeout connect 10000 #default 10 second timeout if a backend is not found
 timeout client 300000 ###Client connection timeout
 timeout server 300000 ###Server connection timeout
 maxconn  60000  ###maximum connection
 retries  3   ###If you fail three times, the service is considered unavailable or you can set it later 
####################################################################
listen stats
  bind 0.0.0.0:1080   #Listening Port 
  stats refresh 30s   #Statistics page auto refresh time 
  stats uri /stats   #Statistics page url 
  stats realm Haproxy Manager #Prompt text on Statistics page password box 
  stats auth admin:admin  #Statistics Page User Name and Password Settings 
  #stats hide-version   #Hide HAProxy version information on Statistics page
#---------------------------------------------------------------------
# main frontend which proxys to the backends
#---------------------------------------------------------------------
frontend main
 bind 0.0.0.0:80
 acl url_static path_beg -i /static /p_w_picpaths /javascript /stylesheets
 acl url_static path_end -i .jpg .gif .png .css .js

 use_backend static if url_static  ###Respond to the backend page of the policy definition if the policy requirements are met
 default_backend dynamic   ###Respond to the default page of backend if not satisfied

#---------------------------------------------------------------------
# static backend for serving up p_w_picpaths, stylesheets and such
#---------------------------------------------------------------------

backend static
 balance  roundrobin     ###Load Balance Mode Polling
 server  static 127.0.0.1:80 check ###Backend Server Definition

backend dynamic
 balance roundrobin
 server   websrv1 192.168.180.9:80 check maxconn 2000
 server   websrv2 192.168.180.4:80 check maxconn 2000

#---------------------------------------------------------------------
# round robin balancing between the various backends
#---------------------------------------------------------------------

errorfile 403 /etc/haproxy/errorfiles/403.http
errorfile 500 /etc/haproxy/errorfiles/500.http
errorfile 502 /etc/haproxy/errorfiles/502.http
errorfile 503 /etc/haproxy/errorfiles/503.http

(7) Start haproxy service

#haproxy profile syntax check
haproxy -c -f /usr/local/haproxy/conf/haproxy.cfg
Configuration file is valid 
#haproxy service startup
systemctl start haproxy
#haproxy service boots and starts
chkconfig --add haproxy
chkconfig haproxy on
chkconfig --list|grep haproxy

(8) Set up haproxy log

#Edit/etc/rsyslog.conf Uncomment:

$ModLoad imudp 
$UDPServerRun 514 
$ModLoad imtcp 
$InputTCPServerRun 514 
#Add to:
local2.*                                                /var/log/haproxy.log          

Description:

The above modifications to the haproxy log are written to both / var/log/haproxy.log and / var/log/messages files. Because of the large number of haproxy logs, only writing them to the / var/log/haproxy.log file requires modification to the / etc/rsyslog.conf file This configuration:

*.info;mail.none;authpriv.none;cron.none                /var/log/messages
 Change to:
*.info; mail.none; authpriv.none; cron.none; local2.none/var/log/messages #This will only write to/var/log/haproxy.log

Modify/etc/sysconfig/rsyslog:

#Modify the SYSLOGD_OPTIONS parameter
-c 2 Use compatibility mode, default is -c 5;-r Open remote log;-m 0 Mark the timestamp, in minutes, 0 to disable the function.
#The complete content is as follows
# Options for rsyslogd
# Syslogd options are deprecated since rsyslog v3.
# If you want to use them, switch to compatibility mode 2 by "-c 2"
# See rsyslogd(8) for more details
#SYSLOGD_OPTIONS=""
SYSLOGD_OPTIONS="-c 2 -r -m 0"
#SYSLOGD_OPTIONS="-c 2 -r -m 0"
&~
#Restart rsyslog service
systemctl restart rsyslog.service

(9) Testing

Open your browser and open the haproxy monitoring page, 192.168.246.168:1080/stats, account: admin, password: admin

View haproxy log:tail-f/var/log/haproxy.log

extend

Common haproxy commands:

# Check profile syntax
haproxy -c -f /etc/haproxy/haproxy.cfg

# Start in daemon mode and systemd managed daemon mode
haproxy -D -f /etc/haproxy/haproxy.cfg [-p /var/run/haproxy.pid]
haproxy -Ds -f /etc/haproxy/haproxy.cfg [-p /var/run/haproxy.pid]

# Start debugging, all connection and processing information will be displayed on the screen
haproxy -d -f /etc/haproxy/haproxy.cfg

# restart.You need to specify the pid list with the st option
haproxy -f /etc/haproxy.cfg [-p /var/run/haproxy.pid] -st `cat /var/run/haproxy.pid`

# graceful restart, reload.You need to specify the pid list with the sf option
haproxy -f /etc/haproxy.cfg [-p /var/run/haproxy.pid] -sf `cat /var/run/haproxy.pid`

# Show haproxy compilation and startup information
haproxy -vv

Reference Documents

Tags: Linux yum RPM Cygwin

Posted on Thu, 16 Jan 2020 12:10:14 -0500 by Boo-urns