Configure Haproxy to implement load balancing

1. Introduction to haproxy

HAProxy is a free and open source software written in C language[1], which provides high availability, load balancing, and applications based on TCP and HTTP Program agent.
HAProxy is especially suitable for web sites with heavy load, these sites usually require session retention or seven-layer processing. HAProxy runs on current hardware and can fully 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 network.

Haproxy’s configuration file consists of two parts: global settings and proxy settings, which are divided into five sections: global, defaults, frontend, backend, listen.

Second, install haproxy

Go to haproxy to download the source package and upload it to the server or wget

Then unzip

[[emailprotected] yasuobao ]# tar -zxvf haproxy-2.0.1.tar.gz
[[emailprotected] yasuobao]# cd haproxy-2.0.1

Compile
[[emailprotected] ] yasuobao]# make TARGET=linux31

The TARGET parameter here depends on the centos version, uname -r to view the system version centos6.X needs to use TARGET=linux26 centos7.x to use linux31
Installation

[[emailprotected] yasuobao]# make install PREFIX=/usr/local/haproxy

Create conf directory
[ [email protected] yasuobao]# mkdir /usr/local/haproxy/conf

Copy files

[[email protected] yasuobao]# cp examples/option-http_proxy .cfg /usr/local/haproxy/conf/haproxy.cfg
[[emailprotected] yasuobao]# cd /usr/local/haproxy/conf

Edit configuration file

< p>[[email protected] conf]# vim haproxy.cfg

Configure HAProxy for load balancing

The meaning of the fields are as follows

global # Global parameter settings
log 127.0.0. 1 local0 info # log syntax: log [max_level_1] # Global log configuration, use the log keyword, specify the local0 log device in the syslog service on 127.0.0.1, and record the log with log level info

maxconn 4096 #Maximum number of connections
user nobody #Owned user
group nobody #Owned group
daemon #Run haproxy as a daemon
nbproc 1 #Specify start The number of haproxy processes can only be used for haproxy in daemon mode, the default is 1
pidfile /usr/local/haproxy/logs/haproxy.pid #Specify the pid file generation directory
defaults
mode http #default mode mode {tcp|http|health }, tcp is 4 layers, http is 7 layers, health will only return OK
retries 3 # Define the number of failed reconnections to the back-end server, When the number of connection failures exceeds this value, the corresponding backend will be corresponded
timeout connect 10s #Connection timeout
timeout client 20s #Client timeout
timeout server 30s #Server-side timeout
timeout check 5s #Detection timeout
frontend www
bind *:80 #Listening address is 80
mode http
option httplog #Enable the function of recording HTTP requests, session status and timer
br /> option forwardfor #Allow to insert "X-Forwarde d-For" header.
option httpclose #Actively close the http channel after each request
log global #Enable event and traffic log
default_backend htmpool #default backend
backend htmpool
mode http
option redispatch # When cookies are used, haproxy will insert the serverID of the back-end server it requests into the cookie to ensure the session persistence; at this time, if the back-end server goes down, But the client's cookie will not be refreshed. If this parameter is set, the client's request will be forcibly directed to another back-end server to ensure the normal service.
option abortonclose #Discard the request that closed the connection due to the long waiting time of the client but is still waiting in the haproxy queue.
balance static-rr #Load balancing algorithm (#banlance roundrobin polling, balance source saves the session Value, support static-rr, leastconn, first, uri and other parameters)
cookie SERVERID #Allow insert serverid into cookie, serverid can be defined after
option httpchk GET /index.html
server myslave02 172.18.74.87:80 cookie 2 weight 3 check inter 2000 rise 2 fall 3 #cookie 1 means serverid is 1, check inter 1500 is to detect the heartbeat frequency, rise 2 is correct to think the server is available for 2 times, fall 3 is to think that the server is failed 3 times Not available, weight represents the weight
server myslave 172.18.74.119:80 cookie 1 weight 3 check inter 2000 rise 2 fall 3

listen admin_stats
bind 0.0.0.0:9188 #Listener Port
mode http
log 127.0.0.1 local0 err #Global log configuration, local0 is the log device, err represents the log level. Among them, the log level has four options: err, warning, info, and debug. This configuration means to use the local0 log device in the rsyslog service on 127.0.0.1, and the logging level is err.
stats refresh 30s #Stats page automatic refresh time
stats uri /haproxy-status #Stats page url
stats realm welcome login\ Haproxy #Prompt text on the password box of the statistics page
stats auth admin:admin123 #Statistics page username and password settings
stats hide-version #Hide HAProxy version information on the statistics page
stats admin if TRUE #By setting this option, you can manually enable it on the monitoring page or Disable the back-end real server, only valid in haproxy1.4.9 or later

Start haproxy

[[emailprotected] conf]# /usr/local/haproxy/sbin/ haproxy -f /usr/local/haproxy/conf/haproxy.cfg

Then edit index.html on the backend server

[[emailprotected] html]# cat index.html
this is 119 html

[[emailprotected] html]# cat index.html
this is 87 html

curl check haproxy Is it working properly
Configure HAProxy for load balancing

Browser access 172.18.74.59:9188/haproxy-status, username admin, password admin123, log in. You can see the working status of haproxy
Configure HAProxy to achieve load balancing

p>

Leave a Comment

Your email address will not be published.