[Freeswitch-users] Load balancing solution using pure FreeSWITCH

Vitalii Colosov vetali100 at gmail.com
Tue Mar 16 05:04:21 PDT 2010


Hi dear FS community,

After a brief analysis of 2 solutions using OpenSIPS and Kamailio (
http://www.opensips.org/html/docs/modules/devel/load_balancer.html#id226109)
and (
http://wiki.freeswitch.org/wiki/SBC_Setup) I decided to implement my own
solution using ONLY FreeSWITCH and ONLY 1 table of any database server (we
use MySQL).

It works on few of our servers right now without any issues.

Feel free to reuse / improve it or provide your comments if you see
something wrong with it.


We have one routing server (RS) and several Media Servers (MS).
FreeSWITCH is installed on each server in default configuration.

--> MS_1
RS  --> MS_2
--> MS_N


(Failover can be implemented by adding one more routing server, but this is
out of scope of current description).

Routing server routes calls to media servers based on logic that you can
easily define.
(Generally speaking, Routing server can route part of the calls onto itself,
but to simplify let's say that it will route only to Media Servers).


Routing server is used to register subscribers (it will have directory files
./freeswitch/conf/directory/default/10001whatever.xml-99999whatever.xml).
Media servers do NOT contain files with users (security issue? probably
should be managed on dialplan stage of media servers by comparing ip
addresses of requests).


The whole routing can be presented by the following chain:

Call arrives into Default context-->RS default dialplan-->Lua script-->PHP
script to get destination MEDIA SERVER IP-->back to Lua script-->Bridge
"sofia/external/" to determined MS IP address-->[Bridge to fail over media
server, optionally]-->End

Let's say we need to route all international calls based on less loaded
server (other logic can be easily implemented in PHP script as well).

*1. RS Dialplan: default.xml*
<include>
        <extension name="route">
                <condition field="destination_number"
expression="^(00)(\d+)">
                        <action application="lua" data="route.lua $2"/>
                </condition>
        </extension>
</include>


*2. RS Lua script: route.lua*
called_number = argv[1]; //without "00"
api = freeswitch.API();
server_ip_address = api:executeString("curl
http://localhost/get_server_ip.php);
forwarding_session_string =
"sofia/external/"..called_number.."@"..server_ip_address;
session:setVariable("bypass_media", "true"); //described below!!!
session:execute("bridge",forwarding_session_string);

*3. PHP script: get_server_ip.php*
<<<  ANY PHP CODE THAT WILL QUERY COMMON DATABASE AND WILL DETERMINE LESS
LOADED AT THE MOMENT SERVER>>>
die($less_loaded_server_ip);


All Media Servers should provide information about their current load into
one common database (dedicated server or routing server can be used for
database).
If you use scripts you can update this table on starting a call and after
call finished.
Or you can create a job that will query FreeSWITCH and will update the
database every, say, 10 seconds.


Basically that's all (really!)
I beleive it can be used for most general purpose scenarios without need of
third-party tools.

P.S.
The only problem we had from the beginning was that RTP media traffic was
passing thru both servers: RS and Media Server.
But after we added the following command to the routing script, RTP media
traffic started to pass only thru Media server, making RS almost free.
session:setVariable("bypass_media", "true");



Best regards,
vIT
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.freeswitch.org/pipermail/freeswitch-users/attachments/20100316/d4956db4/attachment-0002.html 


More information about the FreeSWITCH-users mailing list