As I said earlier loading balancing is a hot topic when it comes to High Availability(HA). So here goes the 2nd post on HAProxy. Let’s distribute the workload among server using our favorite load balancer HAProxy. If you didn’t read the 1st post on MySQL load balancing with HAProxy it’s time now or never. Today we are going to take a look at Apache(Application server) loading balancing with HAProxy.
HAProxy is a free, very fast and reliable solution offering high availability, load balancing, and proxying for TCP and HTTP-based applications. It is particularly suited for very high traffic web sites and
powers quite a number of the world’s most visited ones.
Server 1 – Node 1
Server 2 – Node 2
Server 3 – Load Balancer
What’s Sticky Session
Before jump into anything else sticky session is a concept to be grabbed. Read the below extraction from amazon.
“By default, a load balancer routes each request independently to the application instance with the smallest load. However, you can use the sticky session feature (also known as session affinity) which enables the load balancer to bind a user’s session to a specific application instance. This ensures that all requests coming from the user during the session will be sent to the same application instance. You can use sticky sessions for only HTTP/HTTPS load balancer listeners” – Source
Now let’s move on.
Installing & Configuring HAProxy
Let’s Install HAProxy
apt-get install haproxy
Now let’s configure HAProxy
edit the /etc/default/haproxy file and set the
ENABLED=0 to ENABLED=1
Create a duplicate of the main configuration file or if you have a main configuration file which was used in the previous post, go ahead and edit it.
Add the confiugration
listen appname 0.0.0.0:80 mode http balance roundrobin option httpclose option forwardfor cookie SERVERNAME insert indirect nocache server apache-1 192.ABC.X.31:80 cookie s1 check server apache-2 192.ABC.Y.32:80 cookie s2 check
Let me explain the unclear stuff for you.
The line “cookie SERVERNAME insert indirect nocache” tells HAProxy to setup a cookie called SERVERNAME only if the user does not have a cookie named SERVERNAME. It is going to append a “Cache-Control: nocache” as well, since this type of traffic is supposed to be personnal and doesn’t need to be cached on the internet.
The line “cookie xxx” on the server line definition tells the value of the cookie inserted by HAProxy. When the client comes back, then HAProxy knows directly which server to choose for this client. Let’s see it how this works.
At the first response, HAProxy will send the client the following header, if the server chosen by the load-balancing algorithm is s1
For the second request, the client will send a request containing the header below.
So now the HAProxy knows to which server the second request should be sent to.
If you need stat add the below block as well in your configuration file.
listen stats bind 0.0.0.0:8080 mode http stats enable stats uri / stats realm Strictly\ Private stats auth A_Username:YourPassword stats auth Another_User:password
You can comment out the unnecessary lines in the HAProxy configuration file.
Viewing the Stats
Open up a browser and type
Use A_Username as username and YourPassword as your password.
Tips & Tricks – Using Application Session
Rather than using HAProxy’s own cookie the configuration below shows how to configure HAProxy load balancer to use the cookie setup by the application server to maintain sticky session.
listen appname 0.0.0.0:80 balance roundrobin cookie JSESSIONID prefix indirect nocache server apache-1 192.ABC.X.31:80 cookie s1 check server apache-2 192.ABC.Y.32:80 cookie s2 check
Just replace JSESSIONID by your application cookie. It can be anything, like the default ones from PHP is PHPSESSID. Let’s see how this works. Shall we,
At the first response, the server will send the client the following header
When response goes through HAProxy, the cookie is modified as below
Note that the Set-Cookie header has been prefixed by the HAProxy cookie value (“s1″ in this case) and a “~” is used as a separator between this information and the cookie value.
For the second request, the client will send a request containing the header as below
HAProxy will clean it up on the fly to set it up back like the origin and forward the request to the apache-1(s1) server.
That’s it. Hope you got an idea how to load balance Apache with HAProxy. If you have any questions let me know in the comments below. Your feedback is highly appreciated(happy-face).
But what happens if the apache server goes down abruptly by a problem of power supply for example.
And the client attempts to connect to the same server and HAProxy routes the request to the webcache for example and it does not find the web server anymore?
All you can do is delete the cookie browser and reconnect.
How can I avoid deleting cookies in the browser and just try a F5 reload?
How should I set HAProxy persistence?
Hi, What about this plan: two HAProxy servers (supporting each other when one goes down) and 3 Apache servers (one server as a standby for the others)? How these should be handled?