Powerdns GeoIP resolution
When managing global infrastructure, steering client traffic to the geographically nearest datacenter is critical for minimizing latency and optimizing performance. PowerDNS provides a powerful native feature: the GeoIP Backend (also known as the YAML backend).
This backend reads zone definitions and dynamic routing rules from structured YAML files, mapping client IP addresses directly to regional server endpoints based on continent, country, or custom IP ranges.
In this guide, we will set up Geo-DNS traffic management for api.gslb.roshankhatri08.com.np.
Architectural Goal
We want the target record api.gslb.roshankhatri08.com.np to resolve dynamically depending on the user's location:
- Asia (AS): Resolves to
203.0.113.10 - Europe (EU): Resolves to
198.51.100.20 - North America (NA): Resolves to
192.0.2.30 - Default / Fallback (Other regions): Resolves to
203.0.113.10
Step 1: Install Required Packages
First, ensure PowerDNS and the GeoIP backend extension along with the MaxMind GeoIP database are installed.
Step 2: Configure PowerDNS Core (/etc/powerdns/pdns.conf)
Edit your PowerDNS configuration file to load the geoip backend and point to your YAML zone file:
# /etc/powerdns/pdns.conf
launch=geoip
# Specify paths to your GeoIP databases (MaxMind MMDB or legacy GeoIP)
geoip-database-files=/usr/share/GeoIP/GeoLite2-City.mmdb /usr/share/GeoIP/GeoLite2-Country.mmdb
# Path to the main YAML zone definition file
geoip-zones-file=/etc/powerdns/zones.yaml
# Enable EDNS Client Subnet to resolve based on user location rather than resolver location
edns-subnet-processing=yes
local-address=0.0.0.0
local-port=53
Step 3: Create the YAML Zone File (/etc/powerdns/zones.yaml)
PowerDNS uses YAML format to map macro placeholders like %cn (Continent Code) or %cc (Country Code) to backend service definitions.
Create /etc/powerdns/zones.yaml:
domains:
- domain: api.gslb.roshankhatri08.com.np
ttl: 30
# Enable continent-based lookup format
# %cn expands to 2-letter continent code (e.g., as, eu, na)
services:
api.gslb.roshankhatri08.com.np: ["%cn.api.gslb.roshankhatri08.com.np", "default.api.gslb.roshankhatri08.com.np"]
records:
# Apex SOA & NS Records
api.gslb.roshankhatri08.com.np:
- soa: ns1.gslb.roshankhatri08.com.np hostmaster.roshankhatri08.com.np 2026090701 7200 3600 1209600 3600
- ns: ns1.gslb.roshankhatri08.com.np
ns1.gslb.roshankhatri08.com.np:
- a: 192.0.2.53
# --- Regional Routing Endpoints ---
# Asia Region Endpoint (%cn = as)
as.api.gslb.roshankhatri08.com.np:
- a: 203.0.113.10
# Europe Region Endpoint (%cn = eu)
eu.api.gslb.roshankhatri08.com.np:
- a: 198.51.100.20
# North America Region Endpoint (%cn = na)
na.api.gslb.roshankhatri08.com.np:
- a: 192.0.2.30
# Default Fallback (Triggered if continent record is not found)
default.api.gslb.roshankhatri08.com.np:
- a: 203.0.113.10
How the YAML services Mapping Works:
-
When a client requests api.gslb.roshankhatri08.com.np, PowerDNS evaluates the services array from left to right.
-
%cn interpolates the client's 2-letter continent code (e.g., as for Asia).
-
It checks if as.api.gslb.roshankhatri08.com.np exists under records. If matched, it returns that IP.
-
If no continent match is found (e.g., queries from Africa %cn = af), PowerDNS falls back to the next entry in the array: default.api.gslb.roshankhatri08.com.np.
Step 4: Validate and Restart PowerDNS
Test your YAML syntax and restart PowerDNS service:
# Restart PowerDNS server
sudo systemctl restart pdns
# Check service status
sudo systemctl status pdns
Step 5: Verification & Testing
You can simulate queries from different geographic locations using EDNS Client Subnet (ECS) flags via dig:
- Simulate Query from an Asian IP (e.g., 1.1.1.1):
-
Simulate Query from a European IP (e.g., 185.230.124.1):
Expected Response: 198.51.100.20 -
Simulate Query from a North American IP (e.g., 8.8.8.8):
Bonus: Useful Placeholders ReferencePowerDNS YAML backend supports several format variables you can use in your services:
- %cn 2-letter Continent Code as, eu, na, sa
- %cc 2-letter Country Codenp, us, gb, in
- %as Autonomous System Number (ASN)13335, 15169
- %ip4/%ip6 Client IPv4 or IPv6 Address192.0.2.1