In part one, Introduction to Clustering in PHP, we explored the concepts of load balancing, PHP sessions, and how to set up a rudimentary PHP cluster that allows for redundancy as well as load balancing.

 

The final configuration was one load balancer exposing an NFS share for all of the client PHP servers to use for session storage. While effective, this still gives us a single point of failure (the load balancer). More load balancers can be added, but sits us squarely back on our original problem: All of the sessions are on the first load balancer, not the second.

 

GlusterFS

GlusterFS is a networkable file system that behaves much like a hardware RAID volume. In this case, however, each Gluster server is a "drive" in the overall file system, which is spread out over multiple servers.

 

Gluster works by deploying "bricks" to servers, and letting clients access this virtual file system. The bricks can be defined as a distributed file system (RAID 0) or a redundant file system (RAID 1). For our example, we are going to set up a replicated file system. In our previous example, we had one NFS share on the load balancer. We can continue this concept with Gluster, but we will add an additional load balancer as a hot spare.

 

Configuration

 

 

To begin, we need to configure two load balancers. In practice, one will sit mostly idle while the other serves your traffic. The idle (spare) server will monitor the active load balancer and take over if it goes down. An easy way to accomplish this is to utilize a virtual network adapter to ping the active server. If this ping fails, then the IP address of the virtual network adapter is changed to the IP of the failed load balancer, and all of your traffic continues to flow.

 

Setting up the GlusterFS Servers

The next step is to set up two GlusterFS bricks on each load balancer. To do this, we need to make sure we have the GlusterFS daemon installed. In Ubuntu, simply type:

 

apt-get install glusterfs-server

 

After the daemons are installed and running on both load balancers, we need to add LB2 to the trusted storage pool.

 

gluster peer probe lb2

 

You should see the "Probe Successful" message. Next, you can check the status to make sure everything is running like we expect.

 

gluster peer status

 

Output:

 

Hostname: lb2
Uuid: 7cd93007-fccb-4fcb-8063-133e6ba81cd9
State: Peer in Cluster (Connected) 

 

Creating the Volume

The next step is creating the volume across both servers. In the command below, we will create a volume named sessions residing in the /sessiondirectory on the load balancers.

 

gluster volume create sessions replica 2 transport tcp lb1:/session lb2:/session

 

When you run this, you should see a success message. The "replica 2" command tells gluster that we want to replicate (mirror) the data across two servers (in this case, both servers). The only thing left now is to start the volume.

 

gluster volume start sessions

 

You can also get status of the volume by typing

 

gluster volume info 

 

Client Installation and Configuration

All we have left to do now is set up our PHP servers as clients. Again, we need to install the glusterfs client.

 

apt-get install glusterfs-client

 

Create a mount point.

 

mkdir /media/sessions

 

Mount the volume.

 

mount.glusterfs lb1:/sessions /media/sessions

 

To make this mount on the clients at startup, edit /etc/fstab and add the following line

 

lb1:/sessions /media/sessions glusterfs defaults,_netdev 0 0

 

All that is left is to point PHP's session management to /media/sessions, and all of your session information will be safely replicated to both load balancers. As you add nodes, just follow the steps above to have them access the virtual file system.

 

Resources