Search code examples
databaseload-balancingasterisksipkamailio

Kamailio: How to load balance calls among multiple Asterisk servers based on their IPs


My scenario is: A SIP trunk is connected to a Kamailio server which is connected to multiple Asterisk servers locally and should load balance calls among these asterisk servers. How? There is a mysql database table inside Kamailio server which maps TO part of sip header of incoming calls to one of asterisks' IPs.

Kamailio should read sip header and search inside database and after getting IP, forward the call to the proper asterisk server.

For example, value of To inside incoming sip header is 123456 so kamailio does query database and finds number 123456 is inside 192.168.1.10 so call should be forwarded to server 192.168.1.10.

I have read multiple articles and Kamailio's help from it's website but couldn't find anything related to this scenario. Does anyone know how to write the route inside kamailio.cfg?


Solution

  • There are some ways to do that. One of them is by using the dispatcher module.

    This module offers SIP load balancer functionality and it can be used as a SIP traffic dispatcher. There are many load balancing and traffic dispaching algorithms that you can choose from, for example: round-robin, weight based load balancing, call load distribution, and hashing over SIP message attributes.

    The module can be used as a stateless load balancer; it does not depend on any call state tracking module. It requires the TM module if you enable auto-discovery of active/inactive gateways.

    It is very lightweight, therefore suitable for handling heavy SIP traffic. As the module has a small footprint and the ability to load balancing rules from a plain text file, it is suitable for embedded systems.

    To make it work:

    You need to add a dispatcher.list file with a list of the IPs of your asterisks like so:

    1 sip:192.168.0.10 #asterisk 01
    1 sip:192.168.0.11 #asterisk 02
    

    Then before relaying the request you will do a ds_select_dst(1, 0);


    Be sure that you specify the list file for your dispatcher module:

    loadmodule "dispatcher.so"
    modparam("dispatcher", "list_file", "/var/run/kamailio/dispatcher.list")
    

    If you want to use a database instead of a file you can do that by specifying the db:

    modparam("dispatcher", "db_url", "mysql://user:passwb@localhost/database")
    

    Also, there are other parameters for specifying the database table etc.

    You can read more in the kamailio dispatcher documentation