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.

What’s 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 Setup

Server 1 – Node 1
Hostname: apache-1
IP: 192.ABC.X.31

Server 2 – Node 2
Hostname: apache-2
IP: 192.ABC.Y.32

Server 3 – Load Balancer
Hostname: haproxy
IP: 192.ABC.Z.30

Apache load balancing with HAProxy

Apache load balancing with HAProxy

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

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

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.

You can comment out the unnecessary lines in the HAProxy configuration file.

Viewing the Stats

Open up a browser and type

http://192.ABC.Z.30:8080/

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.

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).

Be Sociable, Share!