Skip to content

Deploy dnsdist as a DNS load balancer and traffic filter

Set up dnsdist, the DNS load balancer from the PowerDNS project, in front of two or more name servers. You will spread queries across healthy backends, rate limit abusive clients, cache answers and watch it all live from the dnsdist console.

Maybe two or three name servers share one service address, and round-robin A records (one name handing out several addresses in turn) do the balancing. dnsdist gives you real load balancing instead. It is a load balancer from the PowerDNS project that understands DNS, so it checks which backend servers are healthy and sends each query to one that is. It also caches answers and drops abusive traffic before any backend has to deal with it. You will start from an empty host and finish with a working dnsdist.conf. Along the way you add backend pools, health checks, routing rules, per-client rate limits, a packet cache, and a live console you can query while traffic flows.

What you’ll accomplish

  • Install dnsdist and get a configuration that survives --check-config before it ever touches port 53.
  • Declare backends with newServer, group them into named pools, and weight them so a bigger box takes more of the load.
  • Route queries to the right pool with addAction and rules matching on name, suffix, or source network — and understand why the order of those lines decides everything.
  • Cut backend load with a packet cache and cut abuse with a per-IP query rate limit.
  • Open the interactive console against a running instance and read showServers(), topQueries(), and cache counters without a restart.

Prerequisites

  • A Linux host that will own the service address clients query. One or two vCPUs handles a surprising amount of DNS; dnsdist is single-process with multiple threads and is rarely the bottleneck.
  • dnsdist 1.8 or newer. Debian and Ubuntu ship a dnsdist package, and Enterprise Linux has one in EPEL, but distribution versions lag. The PowerDNS project runs its own repositories at repo.powerdns.com with current builds, and that is what most people end up using. Every directive below works on 1.8 and later; recent 2.x releases add an optional YAML configuration format alongside the Lua one shown here.
  • At least two working backends. This guide assumes recursors at 10.0.0.11 and 10.0.0.12 and authoritative servers at 10.0.1.21 and 10.0.1.22. If you do not have recursors yet, our guide on Unbound as a recursive resolver builds them.
  • root or sudo on the dnsdist host, and nothing else already bound to port 53 — on most modern distributions, something is. See the troubleshooting section.
  • Basic familiarity with dig. Every verification step in this guide uses it.

How it works

dnsdist is not a generic TCP proxy that happens to forward port 53. It parses each query, so its rules can match on the question name, the query type, the EDNS Client Subnet option, or the source address, and its cache understands DNS semantics well enough to honour TTLs. A query walks a fixed sequence: access control, then your rule chain, then the packet cache, then a pool, then a load-balancing policy that picks one server inside that pool.

Diagram of a DNS query passing through dnsdist — client on the left, then the ACL check, rate limit, packet cache lookup, routing rules and load-balancing policy, feeding two backend pools on the right

The configuration file is Lua. That sounds heavier than it is: for most deployments you write a flat list of function calls and never define a function of your own. But it is real Lua, so a syntax error is a startup failure, not a warning — which is why --check-config earns its place in your deploy pipeline.

The single idea that causes the most confusion is rule ordering. addAction appends to an ordered chain, and dnsdist walks that chain top to bottom until an action terminates processing. A rule placed after a broader rule that already matched will never run.

Diagram of five dnsdist rules evaluated top to bottom, each branching to its outcome — dropped, sent to the auth pool, refused, or falling through to the default recursor pool

Health checks run independently of query traffic. dnsdist sends a periodic query of its own to each backend — by default an A lookup for a.root-servers.net. — and after a configurable number of failures marks that server down and stops sending it real traffic until it answers again.

Step-by-step

1. Install the package and confirm the version. On Debian or Ubuntu after adding the PowerDNS repository:

sudo apt-get install -y dnsdist && dnsdist --version
dnsdist 1.9.7 (Lua 5.3)
Enabled features: dns-over-tls(openssl) dns-over-https(nghttp2) dnscrypt ebpf fstrm ipcipher libedit libsodium protobuf re2 recvmmsg/sendmmsg snmp systemd

Read the feature list. If you plan to terminate DNS-over-TLS, dns-over-tls must appear there; a build without it will reject addTLSLocal at startup.

2. Declare your backends and pools. Open /etc/dnsdist/dnsdist.conf and start with the servers. Each newServer takes a table; pool puts the server in a named group, and weight matters only for the weighted policies.

-- Recursive backends: the default pool, named ""
newServer({address="10.0.0.11:53", name="rec1", pool="", weight=2})
newServer({address="10.0.0.12:53", name="rec2", pool="", weight=1})

-- Authoritative backends for internal zones
newServer({address="10.0.1.21:53", name="auth1", pool="auth"})
newServer({address="10.0.1.22:53", name="auth2", pool="auth"})

A server with no pool lands in the default pool, whose name is the empty string. That default pool is where any query that matches no routing rule ends up, so it should hold the backends that can answer anything.

3. Set the listening addresses and the ACL. dnsdist listens on 127.0.0.1:53 out of the box and its default ACL allows only private ranges, which is the correct default and also the reason a fresh install appears to ignore your clients.

setLocal("192.0.2.10:53")
addLocal("[2001:db8::10]:53")
setACL({"10.0.0.0/8", "192.168.0.0/16"})

4. Tune health checks. The defaults are reasonable but slow to notice a failure. These options make dnsdist probe every second and require two consecutive successes before a recovered server is trusted with traffic again.

newServer({
  address="10.0.1.21:53", name="auth1", pool="auth",
  checkName="internal.example.com.", checkType="SOA",
  checkInterval=1, maxCheckFailures=2, rise=2
})

Point checkName at something the backend genuinely serves. An authoritative server that only knows internal.example.com will return REFUSED for the default a.root-servers.net. check and be marked down while answering production queries perfectly.

5. Choose a load-balancing policy. leastOutstanding is the default and is usually right: it sends each query to the server with the fewest queries in flight, breaking ties by weight and order, which naturally routes around a backend that has become slow rather than dead.

setServerPolicy(leastOutstanding)
setPoolServerPolicy(chashed, "auth")

The four you will actually pick between: wrandom spreads by weight at random; whashed hashes the query name so a given name always lands on the same backend, which is good for backend cache locality; chashed does the same with consistent hashing so adding or removing a server reshuffles a small fraction of names instead of all of them; and leastOutstanding ignores names entirely and follows real-time load.

6. Add the packet cache. This is the single change with the largest effect on backend load. The cache is created once and attached to a pool.

local pc = newPacketCache(100000, {maxTTL=86400, minTTL=0, temporaryFailureTTL=60, staleTTL=60})
getPool(""):setCache(pc)
getPool("auth"):setCache(pc)

maxTTL caps how long dnsdist keeps an answer regardless of what the backend said, and temporaryFailureTTL briefly caches SERVFAIL so one broken zone cannot turn into a retry storm.

7. Write the rule chain. Order these deliberately: cheap rejections first, routing last.

local deny = newNMG()
deny:addMask("198.51.100.0/24")
addAction(NetmaskGroupRule(deny), DropAction())

addAction(MaxQPSIPRule(50), DropAction())

addAction(QNameRule("bad.example.com."), RCodeAction(DNSRCode.REFUSED))

local smn = newSuffixMatchNode()
smn:add(newDNSName("internal.example.com"))
addAction(SuffixMatchNodeRule(smn), PoolAction("auth"))

MaxQPSIPRule(50) allows fifty queries per second per source IP address. Start higher than you think you need and watch the drop counters before tightening — a single busy NAT gateway or a Kubernetes node can look like an attacker.

8. Enable the console. controlSocket opens a control port and setKey supplies the shared secret both ends use to encrypt that channel. The key is 32 random bytes in base64 — makeKey() prints a ready-made setKey line once you have a console, and openssl gets you the first one:

openssl rand -base64 32
wA0Ns4ZmyNzMTNBHb0PWZbAbJXHXKPr7MpjLZpZ+ZTQ=

Put it in the configuration alongside the socket:

controlSocket("127.0.0.1:5199")
setKey("wA0Ns4ZmyNzMTNBHb0PWZbAbJXHXKPr7MpjLZpZ+ZTQ=")
setConsoleACL({"127.0.0.1"})

9. Optionally terminate DNS-over-TLS. dnsdist can accept DoT from clients and forward plain DNS to backends, which is a common way to add encrypted transport without touching the resolvers.

addTLSLocal("192.0.2.10:853",
  "/etc/letsencrypt/live/dns.example.com/fullchain.pem",
  "/etc/letsencrypt/live/dns.example.com/privkey.pem")

The dnsdist process must be able to read the private key after it drops privileges — on Debian it runs as _dnsdist, so the certificate directory needs a group that user belongs to.

10. Check the configuration, then start. Never skip this. A Lua typo means a dead service, and --check-config catches it in a second.

sudo dnsdist --check-config
Configuration '/etc/dnsdist/dnsdist.conf' OK!
sudo systemctl enable --now dnsdist && sudo systemctl status dnsdist --no-pager
● dnsdist.service - DNS Loadbalancer
     Active: active (running) since Sun 2026-08-02 09:14:02 UTC; 3s ago
   Main PID: 21884 (dnsdist)

Validate it works

Query the service address directly and confirm you get an answer, not a timeout:

dig @192.0.2.10 example.com A +short
93.184.215.14

Confirm the routing rule sends internal names to the authoritative pool rather than the recursors:

dig @192.0.2.10 db.internal.example.com A +short
10.0.1.50

Now open the console and look at the backends. Run it on the dnsdist host with no arguments — it reads the socket and key from the same configuration file:

sudo dnsdist -c
> showServers()
#   Name    Address        State   Qps  Qlim Ord Wt   Queries  Drops Drate  Lat Outstanding Pools
0   rec1    10.0.0.11:53      up  12.4     0   1  2     41273      0   0.0  1.4           0
1   rec2    10.0.0.12:53      up   6.1     0   1  1     20655      0   0.0  1.5           0
2   auth1   10.0.1.21:53      up   2.0     0   1  1      8012      0   0.0  0.7           0 auth
3   auth2   10.0.1.22:53    down   0.0     0   1  1         0      0   0.0  0.0           0 auth
All                               20.5               69940      0

The 2:1 split between rec1 and rec2 in the Queries column is the weight working. auth2 being down is the health check working.

Check that the cache is actually being used — a hit ratio near zero after a few minutes of real traffic means the cache is not attached to the pool that is serving it:

sudo dnsdist -c -e 'getPool(""):getCache():printStats()'
Packet cache size: 100000, entries: 4213, hits: 39125, misses: 12844, deferred inserts: 0, deferred lookups: 0, lookup collisions: 0, insert collisions: 0, ttl too shorts: 12

And see what your clients are asking for, which is often the first genuinely useful thing this deployment gives you:

sudo dnsdist -c -e 'topQueries(5)'
1   example.com.                     4131   12.0%
2   internal.example.com.            2210    6.4%
3   metrics.example.com.             1877    5.5%
4   api.partner.example.            1502    4.4%
5   ntp.example.com.                  944    2.7%
Rest                                24276   69.0%

Troubleshooting

A backend is permanently down but answers dig fine. The health check is failing, not the server. dnsdist checks with an A query for a.root-servers.net. by default, and an authoritative-only server correctly refuses that. Set checkName and checkType to something in a zone it serves, as in step 4. To confirm the diagnosis quickly, force the server up from the console with getServer(3):setUp() — if traffic then flows normally, the check was the problem. Return it to automatic supervision with setAuto() afterwards; a server pinned up will keep receiving traffic even when it is genuinely dead.

dnsdist will not start: Unable to bind to 192.0.2.10:53. Something already owns port 53, and on most modern distributions that something is systemd-resolved. Confirm with sudo ss -ulpn 'sport = :53', then set DNSStubListener=no in /etc/systemd/resolved.conf and restart it. Our guide to configuring systemd-resolved covers the stub listener and what else changes when you disable it.

The console refuses to connect or prints garbage. Two distinct causes. Unable to connect to 127.0.0.1:5199 means no controlSocket line took effect — check that the running process actually loaded the config you edited. A message about being unable to decrypt the response means the key the client is using does not match the key the server started with, which happens when you edit setKey and forget to restart, or when you run dnsdist -c as a user who cannot read the configuration file and it silently falls back.

A rule never fires. Print the chain with showRules() from the console; it shows the rules numbered in evaluation order with a match counter for each. A rule with zero matches sitting below a broader rule that has millions is the classic ordering mistake. You can reorder live with mvRule(4, 1) to test a hypothesis, but write the fix into the configuration file — console changes do not survive a restart.

Stale or wrong answers after a zone change. The packet cache honours TTLs, so a record with a one-hour TTL stays cached for an hour, and lowering the TTL at the origin does nothing for entries already stored. Flush one name rather than the whole cache:

sudo dnsdist -c -e 'getPool(""):getCache():expungeByName(newDNSName("www.example.com."), QType.ANY, true)'

If this bites you regularly, cap it with maxTTL in newPacketCache — many operators set 3600 or lower on a front-end cache regardless of what the authoritative servers publish.

Legitimate clients are being dropped. Almost always MaxQPSIPRule applied to a shared source address. Check the Drops column in showServers() and the rule counters in showRules(), then either raise the limit or exempt the address with a NetmaskGroupRule matched to AllowAction() placed above the rate limit line.

When this is the wrong approach

If you have one name server, dnsdist adds a second thing that can break in front of the first. The cache and the rate limiting are genuinely useful on their own, but a single recursor with its own cache already has most of that; the case for dnsdist starts at two backends.

If what you actually need is anycast across sites, dnsdist is the wrong layer. Anycast is a routing decision made by BGP long before a packet reaches any load balancer, and dnsdist running on each anycast node is a complement to that design, not a replacement for it.

If your resolvers live inside Kubernetes and you are trying to balance CoreDNS, use the platform’s service mechanism. Putting dnsdist in front of a Service that is already load balancing gives you two layers of policy disagreeing about which pod is healthy.

And if your goal is filtering by domain reputation with a feed of tens of thousands of names, dnsdist’s rule chain is the wrong tool — that many rules is a linear scan on every query. Response Policy Zones on the recursor itself are built for that shape of problem.

The reference for every directive here is the dnsdist documentation, which lists each rule, action, and server option with the version it landed in.

If this is the kind of work you’d rather never own again, it’s the daily job of Managed Cloud Ops — book an infrastructure review.