HAProxy ALOHA Documentation 14.5

Geolocation-based load balancing

You can deliver content to users based on their geographic location.

For example, you can thus:

  • comply with regulations governing the location of data storage.

  • reduce latency.

  • deliver content that is tailored to users' country and native language.

In the following example, the example.com domain is hosted in two different datacenters located in two different regions, France and North America. HAProxy ALOHA with global server load balancing enabled returns the IP address from the closest healthy datacenter or server.

https://cdn.haproxy.com/documentation/aloha/latest/assets/gslb-geo-balancing-37ecf04212142203415ebb03519f578d5e01402acf47f86eda608d6e629413c9.png

Consider the user experience for a client in France and another in North America, outlined below.

France

North America

I

A client from France asks for the IP address associated with the www.example.com domain name.

A

A client from North America asks for the IP address associated with the www.example.com domain name.

II

HAProxy ALOHA detects the location of the client through the IP address originating the request. HAProxy ALOHA then returns the healthy IP address nearest to the client, selected from the pool of available addresses.

B

HAProxy ALOHA detects the location of the client through the IP address originating the request. HAProxy ALOHA then returns the healthy IP address nearest to the client, selected from the pool of available addresses.

III

The client from France is directed to the Paris datacenter.

C

The client from North America is directed to the Chicago datacenter.

Enable the GSLB service

  1. Mount a partition required for storing the geolocation database.

  2. In the Services tab, click gslb setup.

    https://cdn.haproxy.com/documentation/aloha/latest/assets/gslb_setup-5d94fea5c7b1117e1c2b70befe6438884c175a7df4d31f719c0ae5d0d8772359.png
  3. Add a listen directive for each IP address/network port pair at which the GSLB should listen for DNS queries. By default, it listens on all IP addresses (0.0.0.0) at port 53.

  4. To enable the service to start automatically when the appliance boots, delete the no autostart line, click OK and then Close.

  5. Copy your geolocation database to the /app/extra directory through scp on or Winscp. MaxMind GeoIP2 City databases are supported.

    $ scp GeoLite2-City.mmdb admin@172.16.24.238:/app/extra
  6. In the Services tab, click [advanced mode], then edit the gslb configuration.

    https://cdn.haproxy.com/documentation/aloha/latest/assets/gslb_service_advanced-778c09fbc8cb1d56fc76474de03dd5b9fab9e1e5d72eb2d3dab33a14f488b306.png
    1. Define the zone file to map your domain names to IP addresses. This can contain typical DNS zone file records including SOA, NS, MX, A, AAAA, CNAME.

      Include an additional record of type map to use your MaxMind GeoIP2 City database to change the DNS response to contain a datacenter's IP based on the client's location. The map record type refers to a geoip-map section with the same name.

      In the example below, we map the domain www.example.com to the mymap geoip-map section, which uses the geolocation database to return different IP addresses in response to DNS queries depending on whether the client is in France or the United States.

      zone example.com
        ttl 84600
        record @ ttl 900  SOA   ns1 hostmaster 1 7200 30M 3D 900
        record @          NS    ns1.example.com.
        record ns1        A     203.0.113.1   # nameserver: ALOHA IP address
        record alias      CNAME www
        record www        map   mymap
      
      geoip-map mymap
        location-base /app/extra/GeoIP2-City.mmdb
        location EU/FR/Paris DC1 DC2
        location NA/US/Chicago DC2 DC1
        network 198.51.100.0/24 DC1 DC2
        network 203.0.113.0/24 DC2 DC1

      The geoip-map section directives are as follows:

      geoip-map section directive

      Description

      Example

      location-base

      Absolute path to the geolocation database. You can supply several geolocation database names separated by spaces.

      location-base /app/extra/GeoIP2-City.mmdb

      location

      The first parameter is a hierarchical path to a geographic region in the order of the continent code, a country ISO code, then more specific regions like state and city name. Refer to the MaxMind reference guide and ISO-3166 for these codes. Note that GSLB will search deeper into the hierarchy if a match is not found at the current layer. For example, you could specify country and city name, but omit the state name between them.

      The second parameter is a space-separated list of answer-list section names (e.g. DC2).

      GSLB directs client requests sent from this location to the first healthy datacenter in the list.

      location NA/US/NY DC2

      network

      As an alternative to using location, which uses geolocation data to choose the datacenter, you can also specify a client IP range. Set a subnet value in CIDR notation followed by an ordered list of datacenters (separated by spaces).

      GSLB directs client requests sent from this subnet to the first healthy datacenter in the list.

      network 198.51.100.0/24 DC1

    2. Create new answer-list sections.

      The answer-list for DC1 contains a list of IP addresses for the datacenter in Europe, while the DC2 answer-list contains a list of IP addresses for the datacenter in North America.

      zone example.com
        ttl 84600
        record @ ttl 900  SOA   ns1 hostmaster 1 7200 30M 3D 900
        record @          NS    ns1.example.com.
        record ns1        A     203.0.113.1   # nameserver: ALOHA IP address
        record alias      CNAME www
        record www        map   mymap
      
      geoip-map mymap
        location-base /app/extra/GeoLite2-City.mmdb
        location EU/FR/Paris DC1 DC2
        location NA/US/Chicago DC2 DC1
        network 198.51.100.0/24 DC1 DC2
        network 203.0.113.0/24 DC2 DC1
      
      answer-list DC1
        up_threshold 0.5
        method single-rr
        option tcpchk fall 10 rise 10
        tcp-check connect port 80
        answer-record srv1 198.51.100.1 weight 20
        answer-record srv2 198.51.100.2 weight 20
        answer-record srv3 198.51.100.3 weight 10
        answer-record srv4 2001:db8::4001 weight 20
        answer-record srv5 2001:db8::4002 weight 20
        answer-record srv6 2001:db8::4003 weight 10
      
      answer-list DC2
        up_threshold 0.5
        method single-rr
        option httpchk
        http-check connect
        http-check send uri /health.html hdr host www.example.com
        http-check expect status 200,301,302
        answer-record srv1 203.0.113.10  weight 20
        answer-record srv2 203.0.113.11  weight 20
        answer-record srv3 203.0.113.12  weight 10

      ALOHA GSLB will send DNS responses based on the location of the client. It will only send either IPv4 or IPv6 addresses, depending on the type of IP addresses the client requests.

      Weights determine how often a particular IP address will be returned, with higher weights being chosen more often. The weight values apply only to the IPv4 or IPv6 pool of IP addresses. In the example, the IPV4 pool of servers and the IPv6 pool of servers have their own total weight sums.

      By specifying method single-rr, HAProxy ALOHA alternates which IP for a datacenter it sends to clients in order to distribute traffic across all servers. In that case, the odds of a server's IP being returned is server weight / sum of all server weights.

      You can set method multi-rr to return multiple IP addresses to the client. In that case, the odds of a server's IP being returned is server weight / max weight value.

      The up_threshold directive determines the percentage of servers that must be up. Otherwise, traffic is routed to a different datacenter altogether.

  7. Save your configuration, close the configuration editor, then apply your changes on the gslb line in the Services tab.

    https://cdn.haproxy.com/documentation/aloha/latest/assets/apply-service-gslb-d3f26a7ad971a89779e3d4ff6a86a323864b35122d4799da6377c04caf4d8d10.png

Next up

Logs and status