Skip to content

GeoDNS with PowerDNS — geographically aware responses

Routing DNS queries to different answers based on the resolver client subnet, using PowerDNS Lua records, geoip backend, or a Lua policy script.

A global service wants api.example.com to resolve to the nearest of three regional load balancers — Frankfurt for EU, Virginia for NA, Singapore for APAC. The classic answer is anycast at the IP layer; the cheaper answer is GeoDNS at the resolver. This article shows three PowerDNS approaches: Lua records (modern), the dedicated geoip backend, and Lua policy scripts in the recursor. The deliverable is a working GeoDNS setup with the operational caveats that bite.

How to verify

Before deploying, check what the current zone returns from different vantage points. A few public looking-glass servers help:

dig @1.1.1.1 api.example.com +short                     # Cloudflare (anycast)
dig @8.8.8.8 api.example.com +short                     # Google
dig @ns1.example.com api.example.com +subnet=203.0.113.0/24 +short  # ECS test
dig @ns1.example.com api.example.com +subnet=1.0.0.0/24 +short

If the answers differ based on +subnet=, the authoritative is GeoDNS-enabled. If they are identical, it is location-agnostic.

What’s happening

GeoDNS answers DNS queries differently based on where the query came from. The “where” is determined by:

  1. Resolver source IP. The authoritative sees the recursor’s IP, not the actual client. If the recursor is Cloudflare in Frankfurt and the client is in Los Angeles, the authoritative sees Frankfurt. Coarse but works.
  2. EDNS Client Subnet (ECS, RFC 7871). The recursor includes a /24 (or /48 for v6) of the client’s address in the query. The authoritative answers based on the client subnet. Much more accurate but requires both recursor and authoritative to support ECS, and increases cache fragmentation.

The decision is “use ECS if present, fall back to resolver IP.” Modern PowerDNS supports both natively.

PowerDNS offers three layers of GeoDNS:

  • Lua records — A or AAAA records whose value is the output of a Lua expression. The expression has access to client IP / ECS. Best for small, surgical GeoDNS on a few records.
  • geoip backend — a YAML-driven backend that maps GeoIP country/ASN/continent to record sets. Best for zone-wide geographic routing.
  • Lua policy in the Recursor — recursor-side decisions about which authoritative to query or which CNAME to rewrite. Best for very dynamic routing.

For most use cases, Lua records or the geoip backend on the authoritative is the right starting point.

The procedure (Lua records on PowerDNS Authoritative)

  1. Enable Lua records. In pdns.conf:

    enable-lua-records=yes
    lua-records-cache-size=1000
  2. Define the record via SQL or pdnsutil. A Lua record has type LUA plus the underlying type:

    sudo pdnsutil add-record example.com api LUA "A;ifportup(443, {'203.0.113.10','198.51.100.10','192.0.2.10'}, {selector='pickclosest'})"

    The Lua expression in the second arg is evaluated on every query. ifportup checks the listed IPs for port 443 reachability; selector='pickclosest' picks the geographically nearest from the up set.

  3. Configure GeoIP database. PowerDNS uses MaxMind GeoLite2 by default. On Ubuntu:

    sudo apt install -y mmdb-bin geoipupdate
    # configure /etc/GeoIP.conf with MaxMind license key, then:
    sudo geoipupdate -v
    ls /usr/share/GeoIP/

    Point PowerDNS at the database:

    # /etc/powerdns/pdns.conf
    geoip-database-files=/usr/share/GeoIP/GeoLite2-City.mmdb
  4. Verify the GeoDNS response. Use dig +subnet to simulate different clients:

    dig @ns1.example.com api.example.com +subnet=8.8.8.8/24 +short
    dig @ns1.example.com api.example.com +subnet=1.0.0.0/24 +short

    The first should return the NA endpoint (Google is US-based); the second, APAC.

The procedure (geoip backend)

For zone-wide routing, the geoip backend is cleaner:

  1. Install the backend:

    sudo apt install -y pdns-backend-geoip
  2. Configure the launch and zone:

    # /etc/powerdns/pdns.d/geoip.conf
    launch+=geoip
    geoip-database-files=/usr/share/GeoIP/GeoLite2-City.mmdb
    geoip-zones-file=/etc/powerdns/zones/example.com.yaml
  3. Write the YAML zone:

    domains:
      - domain: example.com
        ttl: 60
        records:
          "":
            - soa: "ns1.example.com hostmaster.example.com 1 7200 1800 1209600 60"
            - ns: ns1.example.com
            - ns: ns2.example.com
          api:
            - a: 203.0.113.10  # default
        services:
          api.example.com:
            default: [ "203.0.113.10" ]
            "%co.iso": [ "us=203.0.113.10", "de=198.51.100.10", "sg=192.0.2.10" ]
  4. Restart and test:

    sudo systemctl restart pdns
    dig @ns1.example.com api.example.com +short

Common pitfalls

  • GeoIP database staleness. MaxMind ships updates weekly. A stale DB will route based on yesterday’s ASN allocations. Schedule geoipupdate daily.
  • ECS opt-out by privacy-focused resolvers. Cloudflare’s 1.1.1.1 does not send ECS by default. Your GeoDNS will see Cloudflare’s edge IP and route based on that. Test from multiple recursors to understand the actual distribution.
  • Cache fragmentation from ECS. Each unique /24 in the response cache is a separate entry. A globally popular name with ECS-enabled responses can blow up recursor cache size. Tune cache size and TTLs accordingly.
  • DNSSEC and Lua records. Lua records change per query — they cannot be pre-signed. PowerDNS will sign them on-the-fly with the ZSK, which means the signing key is hot for every query. Plan CPU accordingly.
  • Hairpin/closed-network surprises. Internal clients accessing the public name may be geolocated to wherever their NAT egress lands. If api.example.com should always return the internal IP for internal clients, use split-horizon (or BIND views) rather than GeoDNS for that case.

In the engagements we run, GeoDNS is a cheaper alternative to full anycast for many workloads, but it requires monitoring from multiple geographic vantage points to confirm the routing is doing what the team expects. We codify the Lua records or YAML maps in configuration management, run synthetic checks from each region, and treat GeoIP DB updates as part of the routine maintenance cycle. See our multi-region environment service for how this fits into broader global deployments.