Apache Load Balancing and Failover

Evidian SafeKit brings network load balancing and failover to Apache. This article explains how to implement quickly a Apache load balancing cluster without network boxes or dedicated load balancing servers. A free trial is offered.

How the Evidian SafeKit software simply implements Apache load balancing and failover without network load balancers or dedicated servers

How the Evidian SafeKit farm cluster implements Apache load balancing and failover?

On the previous figure, Apache is running of the 3 servers. Users are connected to a virtual IP address which is configured locally on each server. The input traffic for the virtual IP address is received by all the servers and split among them by a filter inside each server's kernel.

The network load balancing algorithm inside the filter is based on the identity of the client packets (client IP address, client TCP port). Depending on the identity of the client packet input, only one filter in a server accepts the packet; the other filters in other servers reject it.

Once a packet is accepted by the filter on a server, only the CPU and memory of this server are used by Apache that responds to the request of the client. The output messages are sent directly from the Apache server to the client.

If a server fails, the SafeKit membership protocol reconfigures the filters in the network load balancing cluster to re-balance the traffic on the remaining available servers.

Note that SafeKit works in VMware environments without the Microsoft NLB problems of unicast and muticast modes.  And note also that SafeKit network load balancing is working not only on Windows (including Windows editions for PCs) but also on Linux.

Configuration overview of Apache load balancing and failover

With SafeKit, you can configure either a farm application module or a mirror application module according the high availability architecture suited for an application. For Apache load balancing and failover, the right module is the farm module.

Configuration overview of Apache load balancing and failover

The configuration files for Apache load balancing and failover are given for Windows here and for Linux here.

They include:

1. the Apache stop and start scripts,

2. the configuration file userconfig.xml which contains:

Deployment of Apache load balancing and failover requires no specific IT skills:

    • install Apache on two standard servers (or more)
    • install the SafeKit software on both servers
    • install the apache_farm.safe module

SafeKit configuration files on Windows for Apache load balancing and failover

Installation instructions

userconfig.xml

<!DOCTYPE safe>
<safe>
<macro name="VIRTUAL_IP" value="VIRTUAL_IP_TO_BE_DEFINED" />
<macro name="APACHE_PORT" value="TCP_PORT_TO_BE_DEFINED" />
<service mode="farm" maxloop="3" loop_interval="24">
  <!-- Farm topology configuration for the membership protocol -->
  <!-- Names or IP addresses on the default network are set during initialization in the console -->
  <farm>
    <lan name="default" />
  </farm>
  <!-- Virtual IP Configuration -->
  <!-- Use VIRTUAL_IP defined in macro above -->
  <vip>
    <interface_list>
      <interface check="on" arpreroute="on">
        <virtual_interface type="vmac_directed">
          <virtual_addr addr="%VIRTUAL_IP%" where="alias"/>
        </virtual_interface>
      </interface>
    </interface_list>
    <loadbalancing_list>
      <group name="APACHE">
        <!-- Set load-balancing rule on APACHE_PORT defined in macro above -->
        <rule port="%APACHE_PORT%" proto="tcp" filter="on_addr"/>
      </group>
    </loadbalancing_list>
  </vip>
  <!-- TCP Checker Configuration -->
  <!-- Use VIRTUAL_IP and APACHE_PORT defined in macros above -->
  <check>
    <tcp ident="HTTP_APACHE" when="both">
      <to
         addr="%VIRTUAL_IP%"
         port="%APACHE_PORT%"
         interval="10"
         timeout="5"
       />
    </tcp>
  </check>
  <!-- User scripts activation -->
  <user nicestoptimeout="300" forcestoptimeout="300" logging="userlog" />
</service>
</safe>

start_both.cmd

@echo off

rem Script called on all servers for starting applications

rem For logging into SafeKit log use:
rem "%SAFE%\safekit" printi | printe "message"

rem stdout goes into Application log
echo "Running start_both %*" 

set res=0

net start Apache2
if not %errorlevel% == 0 (
  %SAFE%\safekit printi "Apache start failed"
) else (
  %SAFE%\safekit printi "Apache started"
)

set res=%errorlevel%
if %res% == 0 goto end

:stop
set res=%errorlevel%
"%SAFE%\safekit" printe "start_both failed"

rem uncomment to stop SafeKit when critical
rem "%SAFE%\safekit" stop -i "start_both"

:end

stop_both.cmd

@echo off

rem Script called on all servers for stopping application

rem For logging into SafeKit log use:
rem "%SAFE%\safekit" printi | printe "message"

rem ----------------------------------------------------------
rem
rem 2 stop modes:
rem
rem - graceful stop
rem   call standard application stop with net stop
rem
rem - force stop (%1=force)
rem   kill application's processes
rem
rem ----------------------------------------------------------

rem stdout goes into Application log
echo "Running stop_both %*" 

set res=0

rem default: no action on forcestop
if "%1" == "force" goto end

%SAFE%\safekit printi "Stopping Apache..."
net stop Apache2

rem If necessary, uncomment to wait for the real stop of services
rem "%SAFEBIN%\sleep" 10

if %res% == 0 goto end

"%SAFE%\safekit" printe "stop_both failed"

:end

SafeKit configuration files on Linux for Apache load balancing and failover

Installation instructions

userconfig.xml

<!DOCTYPE safe>
<safe>
<macro name="VIRTUAL_IP" value="VIRTUAL_IP_TO_BE_DEFINED" />
<macro name="APACHE_PORT" value="TCP_PORT_TO_BE_DEFINED" />
<service mode="farm" maxloop="3" loop_interval="24">
  <!-- Farm topology configuration for the membership protocol -->
  <!-- Names or IP addresses on the default network are set during initialization in the console -->
  <farm>
    <lan name="default" />
  </farm>
  <!-- Virtual IP Configuration -->
  <!-- Use VIRTUAL_IP defined in macro above -->
  <vip>
    <interface_list>
      <interface check="on" arpreroute="on">
        <virtual_interface type="vmac_directed">
          <virtual_addr addr="%VIRTUAL_IP%" where="alias"/>
        </virtual_interface>
      </interface>
    </interface_list>
    <loadbalancing_list>
      <group name="APACHE">
        <!-- Set load-balancing rule on APACHE_PORT defined in macro above -->
        <rule port="%APACHE_PORT%" proto="tcp" filter="on_addr"/>
      </group>
    </loadbalancing_list>
  </vip>
  <!-- TCP Checker Configuration -->
  <!-- Use VIRTUAL_IP and APACHE_PORT defined in macros above -->
  <check>
    <tcp ident="HTTP_APACHE" when="both">
      <to
         addr="%VIRTUAL_IP%"
         port="%APACHE_PORT%"
         interval="10"
         timeout="5"
       />
    </tcp>
  </check>
  <!-- User scripts activation -->
  <user nicestoptimeout="300" forcestoptimeout="300" logging="userlog" />
</service>
</safe>

start_both

#!/bin/sh 
# Script called on the primary server for starting applications 

# For logging into SafeKit log use:
# $SAFE/safekit printi | printe "message" 

#---------- Clean Apache residual processes 
# Call this function before starting Apache 
# to clean eventual resual Apache processes
clean_Apache()
{
  retval=0

  # $SAFE/safekit printw "Cleaning Apache processes"

  # example of a kill of started Apache process
  # warning: this command also kills the httpd process which managed the SafeKit web console
  # ps -e -o pid,comm | grep httpd | $AWK '{print "kill " $1}'| sh >/dev/null 2>&1

  return $retval
}

#---------- Apache
# Call this function for starting Apache Server
start_Apache()
{
  retval=0

  $SAFE/safekit printw "Starting Apache Server"

  # Apache - Starting 
  service httpd start   
  if [ $? -ne 0 ] ; then 
    $SAFE/safekit printw "Apache server start failed"
  else
    $SAFE/safekit printw "Apache server started"
  fi

  return $retval
}

# stdout goes into Application log
echo "Running start_both $*" 

res=0

[ -z "$OSNAME" ] && OSNAME=`uname -s`
OSNAME=`uname -s`
case "$OSNAME" in
    Linux)
	AWK=/bin/awk
	;;
    *)
	AWK=/usr/bin/awk
	;;
esac

# TODO
# remove Apache boot start 

# Clean Apache residual processes 
clean_Apache || res=$?

# Start Apache
start_Apache || res=$?

if [ $res -ne 0 ] ; then
  $SAFE/safekit printi "start_both failed"

  # uncomment to stop SafeKit when critical
  # $SAFE/safekit stop -i "start_both"
fi

exit 0

stop_both

#!/bin/sh
# Script called on the primary server for stopping application services

# For logging into SafeKit log use:
# $SAFE/safekit printi | printe "message" 

#----------------------------------------------------------
#
# 2 stop modes:
#
# - graceful stop
#   call standard application stop
#
# - force stop ($1=force)
#   kill application's processes
#
#----------------------------------------------------------

#---------- Clean Apache residual processes
# Call this function on force stop 
# to clean eventual resual Apache processes 
clean_Apache()
{
  retval=0

  # $SAFE/safekit printw "Cleaning Apache processes "

  # example of a kill of started Apache process
  # warning: this command also kills the httpd process which managed the SafeKit web console
  # ps -e -o pid,comm | grep httpd | $AWK '{print "kill " $1}'| sh >/dev/null 2>&1

  return $retval
}

#---------- Apache
# Call this function for stopping Apache
stop_Apache()
{
  retval=0

  if [ "$1" = "force" ] ; then
    # Apache force stop
    clean_Apache
    return $retval
  fi

  # Apache graceful stop
  $SAFE/safekit printw "Stopping Apache server"  
  service httpd stop
  if [ $? -ne 0 ] ; then 
    $SAFE/safekit printw "Apache server stop failed"
  else
    $SAFE/safekit printw "Apache server stopped"  
  fi

  return $retval
}

# stdout goes into Application log
echo "Running stop_both $*" 

res=0

[ -z "$OSNAME" ] && OSNAME=`uname -s`
case "$OSNAME" in
    Linux)
	AWK=/bin/awk
	;;
    *)
	AWK=/usr/bin/awk
	;;
esac

mode=
if [ "$1" = "force" ] ; then
  mode=force
  shift
fi

# Stop Apache  server 
stop_Apache $mode || res=$?

[ $res -ne 0 ] && $SAFE/safekit printi "stop_both failed"

exit 0

Demonstration

This demonstration is made with Apache on Windows. The operating mode is the same on Windows and Linux.

More on SafeKit

Other examples of high availability modules:

Mirror modules

Windows

Linux

Microsoft SQL ServerWindows module-
OracleWindows moduleLinux module
MySQLWindows moduleLinux module
PostgreSQLWindows moduleLinux module
FirebirdWindows moduleLinux module
Hyper-VWindows module-
Hanwha SSMWindows module-
Milestone XProtectWindows module-
Generic moduleWindows moduleLinux module

Farm modules

Windows

Linux

IIS moduleWindows module-
Apache moduleWindows moduleLinux module
Generic moduleWindows moduleLinux module
contact
CONTACT
Demonstration

Evidian SafeKit Pricing





White Papers

contact
NEWS

To receive Evidian news, please fill the following form.