8. Location-Based Load Balancing (IPDB/LGRP)

Overview

Location-Based Load Balancing Network Setup

This example is similar to example 5, but introduces the following new key concepts available with BalanceNG 2.160 and higher:

  • Location-Based Load Balancing with the IPDB IP-to-location database
  • Grouping of targets using “Location Groups” (LGRP)

First of all it’s needed to load a IP-to-Location database file into memory. This is done with the “ipdb” command loading the file from the default location:

ipdb      "/opt/BalanceNG/IpToCountry.csv"

or, just include the following line at the top of /etc/bng.conf:

ipdb

BalanceNG supports the seven column format as available from software77.net (for IPv4/IPv6 upgrade instructions see this knowledge base article.

The five column format from WebHosting.info is no longer available.

Then, two “location groups” are defined: Group A containing the countries Germany, Austria and Switzerland and Group B containing everything else (including client IP addresses which are not found in the database). This is done by including the following lines in the BalanceNG configuration:

lgrp      {
          A "DE,AT,CH"
          B "*,!A"
}

The server section needs to have “ipdb enable” included to enable the IPDB lookup mechanism like this:

server    1 {
          ipaddr 172.16.1.10
          targets 1,2,3,4
          port 80,443
          protocol tcp
          ipdb enable
          method agent
}

Please note that the ipdb lgrp feature does not work with the server methods “rr” (round-robin) and “hash”.

As the next step the targets need to be associated with the “location groups” defined earlier in the configuration file. This is done by including the directive “lgrp” in the target section.

In this example target 1 and two are member of lgrp A, and target 3 and 4 are member of lgrp B:

target    1 {
          ipaddr 172.16.1.100
          lgrp A
          agent 439,2,5
          script "/usr/lib/mon/mon.d/http.monitor -p $port$ -u /index.html $ipaddr$",2,7
          dsr enable
}
target    2 {
          ipaddr 172.16.1.101
          lgrp A
          agent 439,2,5
          script "/usr/lib/mon/mon.d/http.monitor -p $port$ -u /index.html $ipaddr$",2,7
          dsr enable
}
target    3 {
          ipaddr 172.16.1.102
          lgrp B
          agent 439,2,5
          script "/usr/lib/mon/mon.d/http.monitor -p $port$ -u /index.html $ipaddr$",2,7
          dsr enable
}
target    4 {
          ipaddr 172.16.1.103
          lgrp B
          agent 439,2,5
          script "/usr/lib/mon/mon.d/http.monitor -p $port$ -u /index.html $ipaddr$",2,7
          dsr enable
}

Informational Commands

The command “show ipdb” displays information about the currently loaded IP-to-location database:

NodeA# show ipdb
  IPDB loaded from /opt/BalanceNG/ip-to-country.csv
  83429 valid 5-column lines
  83429 total IPDB entries available
  no consecutive area overlaps
  235 different IPDB locations referenced
NodeA#

Another interesting feature is the ability to “locate” any IPv4 address using the locate command like this:

NodeA# locate 82.135.110.2
  address 82.135.110.2 is in DE (GERMANY)
NodeA#

The command “show locations” displays an overview of the current known locations in the IPDB database. Additionally, there’s a session counter maintained for each location (example output truncated):

NodeA# sh locations
  key  counter description
  --- -------- ------------------------------------
   AD          ANDORRA
   AE        2 UNITED ARAB EMIRATES
   AF          AFGHANISTAN
   ...
   CN       44 CHINA
   CO        9 COLOMBIA
   CR          COSTA RICA
   CS        4 SERBIA AND MONTENEGRO
   CU          CUBA
   CV          CAPE VERDE
   CY        2 CYPRUS
   CZ        9 CZECH REPUBLIC
   DE      596 GERMANY
   DJ          DJIBOUTI
   DK       23 DENMARK
   DM          DOMINICA
   DO        8 DOMINICAN REPUBLIC
   ...
   UM          UNITED STATES MINOR OUTLYING ISLANDS
   US      708 UNITED STATES
   UY        3 URUGUAY
   UZ        1 UZBEKISTAN
   VA          HOLY SEE (VATICAN CITY STATE)
   VC          SAINT VINCENT AND THE GRENADINES
   VE        8 VENEZUELA
   VG          VIRGIN ISLANDS, BRITISH
   VI          VIRGIN ISLANDS, U.S.
   VN       23 VIET NAM
   VU          VANUATU
   WF          WALLIS AND FUTUNA
   WS          SAMOA
   YE          YEMEN
   YT          MAYOTTE
   ZA          SOUTH AFRICA
   ZM          ZAMBIA
   ZW          ZIMBABWE
   -        30 *** NOT FOUND PSEUDO ENTRY ***
  --- -------- ------------------------------------
  235     2940 total

The command “clear locationcounters” resets all these counters back to 0.

Configuration Files

The configuration files in this example are ready for production use.

Node A

//         configuration taken ...
//         BalanceNG ...
hostname   NodeA
remark    "Location-Based Load Balancing with IPDB and LGRP"
interface eth0
vrrp      {
          vrid 9
          priority 200
          network 1
}
network   1 {
          name "local network"
          addr 172.16.1.0
          mask 255.255.255.0
          real 172.16.1.252
          interface eth0
}
register  network 1
enable    network 1
ipdb      "/opt/BalanceNG/ip-to-country.csv"
lgrp      {
          A "DE,AT,CH"
          B "*,!A"
}
server    1 {
          ipaddr 172.16.1.10
          targets 1,2,3,4
          port 80,443
          protocol tcp
          ipdb enable
          method agent
}
register  server 1
enable    server 1
target    1 {
          ipaddr 172.16.1.100
          lgrp A
          agent 439,2,5
          script "/usr/lib/mon/mon.d/http.monitor -p $port$ -u /index.html $ipaddr$",2,7
          dsr enable
}
target    2 {
          ipaddr 172.16.1.101
          lgrp A
          agent 439,2,5
          script "/usr/lib/mon/mon.d/http.monitor -p $port$ -u /index.html $ipaddr$",2,7
          dsr enable
}
target    3 {
          ipaddr 172.16.1.102
          lgrp B
          agent 439,2,5
          script "/usr/lib/mon/mon.d/http.monitor -p $port$ -u /index.html $ipaddr$",2,7
          dsr enable
}
target    4 {
          ipaddr 172.16.1.103
          lgrp B
          agent 439,2,5
          script "/usr/lib/mon/mon.d/http.monitor -p $port$ -u /index.html $ipaddr$",2,7
          dsr enable
}
register  targets 1,2,3,4
enable    targets 1,2,3,4
//        end of configuration

Node B

//        configuration taken ...
//        BalanceNG ...
hostname  NodeB
remark    "Location-Based Load Balancing with IPDB and LGRP"
interface eth0
vrrp      {
          vrid 9
          priority 100
          network 1
}
network 1 {
          name "local network"
          addr 172.16.1.0
          mask 255.255.255.0
          real 172.16.1.251
          interface eth0
}
register  network 1
enable    network 1
ipdb      "/opt/BalanceNG/ip-to-country.csv"
lgrp      {
          A "DE,AT,CH"
          B "*,!A"
}
server    1 {
          ipaddr 172.16.1.10
          targets 1,2,3,4
          port 80,443
          protocol tcp
          ipdb enable
          method agent
}
register  server 1
enable    server 1
target    1 {
          ipaddr 172.16.1.100
          lgrp A
          agent 439,2,5
          script "/usr/lib/mon/mon.d/http.monitor -p $port$ -u /index.html $ipaddr$",2,7
          dsr enable
}
target    2 {
          ipaddr 172.16.1.101
          lgrp A
          agent 439,2,5
          script "/usr/lib/mon/mon.d/http.monitor -p $port$ -u /index.html $ipaddr$",2,7
          dsr enable
}
target    3 {
          ipaddr 172.16.1.102
          lgrp B
          agent 439,2,5
          script "/usr/lib/mon/mon.d/http.monitor -p $port$ -u /index.html $ipaddr$",2,7
          dsr enable
}
target    4 {
          ipaddr 172.16.1.103
          lgrp B
          agent 439,2,5
          script "/usr/lib/mon/mon.d/http.monitor -p $port$ -u /index.html $ipaddr$",2,7
          dsr enable
}
register  targets 1,2,3,4
enable    targets 1,2,3,4
//        end of configuration