Install HA-proxy as load balance in webserver
Hello Guys,
I will tell you how you can setup your HA proxy in 10 min for high availability from source so that if one of your web server down it wont affect you much.as your other server are working just fine and your company's or clients business is uninterrupted.
HA proxy work on transport layer protocol so same proxy can be use for multiple application.
It will server a great like same ha proxy can be used to configure the Mysql server as well as apache web-server or nginx or tomcat and many more.You will also get the statistics of the network in real time look at the image below.
Look in the image you can see that its showing the working web-server by green.Stop or not working or not responding web server by red.
Its working as proxy server as well as a load balance.
Lets start with installation and configuration.I am using ubuntu 12.04 as my host machine.
so
apt-get install haproxy
This will do the trick and install haproxy fro me.
We also need to enable the ha proxy in init script so edit the following file
nano /etc/default.haproxy
enable = 1
Then start with the configuration first of all backup your existing haproxy.conf file to haproxy.back file
mv /etc/haproxy/haproxy.cfg /etc/haproxy/haproxy.cfg.back
global
log 127.0.0.1 local0
log 127.0.0.1 local1 notice
maxconn 4096
user haproxy
group haproxy
The
log
directive mentions a syslog server to which log
messages will be sent. On Ubuntu rsyslog is already installed and
running but it doesn't listen on any IP address. We'll modify the config
files of rsyslog later.
The
maxconn
directive specifies the number of concurrent connections on the frontend. The default value is
2000
and should be tuned according to your VPS' configuration.
The
user
and
group
directives changes the HAProxy process to the specified user/group. These shouldn't be changed.
defaults
log global
mode http
option httplog
option dontlognull
retries 3
option redispatch
maxconn 4096
timeout connect 5000
timeout client 50000
timeout server 50000
We're specifying default values in this section. The values to be modified are the various
timeout
directives. The
connect
option specifies the maximum time to wait for a connection attempt to a VPS to succeed.
The
client
and
server
timeouts apply when
the client or server is expected to acknowledge or send data during the
TCP process. HAProxy recommends setting the
client
and
server
timeouts to the same value.
The
retries
directive sets the number of retries to perform on a VPS after a connection failure.
The
option redispatch
enables session redistribution in case of connection failures. So session stickness is overriden if a VPS goes down.
On Web-server 1 & 2
Create a file in web-servers called check.txt using command and we will configure the same in ha proxy to check for the particular file if file found when server is ping that means server is operational.
for HA-proxy and proxy will show it as green in the panel.
listen ApplicationCluster 192.168.1.100:80
mode http
stats enable
stats auth nrathi:nrathi123
balance roundrobin
cookie JSESSIONID prefix
option httpclose
option forwardfor
option httpchk HEAD /check.txt HTTP/1.0
server webserver1 192.168.1.102:80 cookie A check
server webserver2 192.168.1.103:80 cookie B check
This contains configuration for both the frontend and backend. We are configuring HAProxy to listen on port 80 for
appname
which is just a name for identifying an application. The
stats
directives enable the connection statistics page and protects it with HTTP Basic authentication using the credentials specified by the
stats auth
directive.
This page can viewed with the URL mentioned in
stats uri
so in this case, it is
http://192.168.1.100/haproxy?stats
a demo of this page can be
viewed here.
The
balance
directive specifies the load balancing algorithm to use. Options available are Round Robin (
roundrobin
), Static Round Robin (
static-rr
), Least Connections (
leastconn
), Source (
source
), URI (
uri
) and URL parameter (
url_param
).
Information about each algorithm can be obtained from the
official documentation.
The
server
directive declares a backend server, the syntax is:
server <name> <address>[:port] [param*]
The name we mention here will appear in logs and alerts. There are
many paratmeters supported by this directive and we'll be using the
check
and
cookie
parameters in this article. The
check
option enables health checks on the VPS otherwise, the VPS is
always considered available.
Once you're done configuring start the HAProxy service:
sudo service haproxy start
SO the sample configuration file is as follows:
global
log 127.0.0.1 local0
log 127.0.0.1 local1 notice
#log loghost local0 info
maxconn 4096
#debug
#quiet
user haproxy
group haproxy
defaults
log global
mode http
option httplog
option dontlognull
retries 3
option redispatch
maxconn 4096
timeout connect 5000
timeout client 50000
timeout server 50000
listen webfarm 192.168.1.100:80
mode http
stats enable
stats auth someuser:somepassword
balance roundrobin
cookie JSESSIONID prefix
option httpclose
option forwardfor
option httpchk HEAD /check.txt HTTP/1.0
server webA 192.168.1.102:80 cookie A check
server webB 192.168.1.103:80 cookie B check
# and thats it we are ready to go.