HaProxy installation and common commands
Install haproxy
For haproxy installation, there are a large number of resources available online, the most common is to use yum and compiler installation.
Yum install haproxy
CentOS comes with haproxy, but the version may be older. A stable version of haproxy can be found on the IRS source.
The main steps are as follows:
(1) configure yum source
Cat 0.21)-linux26 for Linux 2.6 and above- linux2628 for Linux 2.6.283.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 saved. Using USE_PCRE=1 means to compile in a PCRE environment to speed up the compilation.
The kernel version of the operating system in this article is 3.10.0, which is specified as linux2628 by target. I have not reported wrong when I use linux3100 here.
After the compilation and installation is complete, there are only three directories: doc, share, and sbin,sbin. There is only one haproxy main program, haproxy.
(4) manage haproxy services
Ln-s / usr/local/haproxy/sbin/haproxy / usr/sbin/haproxycd / usr/local/haproxy-1.8.23/examplescp haproxy.init / etc/rc.d/init.d/haproxycd / etc/rc.d/init.dchmod + x haproxy
(5) set haproxy-related configuration files
# create configuration directory mkdir / usr/local/haproxy/conf # create main configuration file touch / usr/local/haproxy/conf/haproxy.cfg# create startup script configuration directory mkdir / etc/haproxy/ # add configuration file soft connection 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 ln-s / usr/local/haproxy/errorfiles / etc/haproxy/errorfiles to error page
(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 # haproxy pid storage path The user who starts the process must have permission to access this file maxconn 4000 # maximum number of connections By default, 4000 user haproxy group haproxy daemon # creates a process to run in deamon mode. This parameter requires that the running 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 mode {tcp | http | health} Tcp is layer 4 and http is layer 7. Health will only return OK log global # use globally defined log option dontlognull # do not record health check log information option httpclose # actively close http channel option httplog # log category http log format option forwardfor # if the back-end server needs to obtain the parameters that the client real ip needs to configure. You can obtain the client ip option redispatch # serverId from Http Header. After the server hangs, force the direction to another healthy server, 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 number of connections retries 3 # the service is considered unavailable if the connection fails 3 times You can also automatically refresh the stats uri / stats # system by setting the # listen stats bind 0.0.0.0 stats uri 1080 # listening port stats refresh 30s # statistics page. Count page url stats realm Haproxy Manager # statistics page password box prompt text stats auth admin:admin # statistics page username and password settings # stats hide-version # hide the version information of HAProxy on the statistics page #- -# main frontend which proxys to the backends#----frontend main bind 0.0.0.0 static 80 acl url_static path_beg-I / static / pendant _ Picpaths / javascript / stylesheets acl url_static path_end-I .jpg .gif .png .css .js use_backend static if url_static # meet the policy requirements Then the backend page defined by the response policy default_backend dynamic # does not meet the default page #-# static backend for serving up p_w_picpaths of the response backend Stylesheets and such#----backend static balance roundrobin # load balance mode polling server static 127.0.0.1 check 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 check maxconn 80 check maxconn 2000 Murmuri # round robin balancing between the various backends#- -errorfile 403 / etc/haproxy/errorfiles/403.httperrorfile 500 / etc/haproxy/errorfiles/500.httperrorfile 502 / etc/haproxy/errorfiles/502.httperrorfile 503 / etc/haproxy/errorfiles/503.http
(7) start the haproxy service
# haproxy configuration file syntax check haproxy-c-f / usr/local/haproxy/conf/haproxy.cfgConfiguration file is valid # haproxy service startup systemctl start haproxy#haproxy service boot chkconfig-- add haproxychkconfig haproxy onchkconfig-- list | grep haproxy
(8) set haproxy log
# Edit / etc/rsyslog.conf uncomment: $ModLoad imudp $UDPServerRun 514$ ModLoad imtcp $InputTCPServerRun 514 # add: local2.* / var/log/haproxy.log
Description ⚠️:
The above modified haproxy logs are written to both / var/log/haproxy.log and / var/log/messages files. Since there are a large number of haproxy logs, only writing them to the / var/log/haproxy.log file needs to modify the configuration of the / etc/rsyslog.conf file:
* .info;mail.none;authpriv.none;cron.none / var/log/messages changed to: * .info; mail.none;authpriv.none;cron.none;local2.none / var/log/messages # so that it will only be written to / var/log/haproxy.log
Modify / etc/sysconfig/rsyslog:
# modify the "SYSLOGD_OPTIONS" parameter-c 2 uses compatibility mode. By default, remote log is enabled by-c 5pm;-m 0 is marked with a timestamp in minutes, and 0 means to disable this feature. # 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 the rsyslog service systemctl restart rsyslog.service
(9) testing
Open the browser to open the haproxy monitoring page, 192.168.246.168:1080/stats, account: admin, password: admin
Check haproxy's log:tail-f / var/log/haproxy.log
Expansion
Common haproxy commands:
# check the configuration file syntax haproxy-c-f / etc/haproxy/haproxy.cfg# starts in daemon mode, starts haproxy-D-f / etc/haproxy/haproxy.cfg [- p / var/run/haproxy.pid] haproxy-Ds-f / etc/haproxy/haproxy.cfg [- p / var/run/haproxy.pid] # in systemd managed daemon mode, and all connection and processing information will be displayed on the screen haproxy-d-f / etc/haproxy/haproxy.cfg# restart. You need to use the st option to specify the pid list haproxy-f / etc/haproxy.cfg [- p / var/run/haproxy.pid]-st `cat / var/run/ haproxy.pid` # graceful restart, namely reload. You need to use the sf option to specify the pid list haproxy-f / etc/haproxy.cfg [- p / var/run/haproxy.pid]-sf `cat / var/run/ haproxy.pid` # display haproxy compilation and startup information haproxy-vv reference documentation
HAproxy (1) installation and configuration
Install haproxy and haproxy commands