May 31, 2022
4 min read time

Two-Minute Tech Tuesdays - ActiveDNS

2mtt activedns feature

This week's episode of Two-Minute Tech Tuesday is about ActiveDNS (vmod_activedns), a Varnish Enterprise VMOD that performs DNS updates at runtime. This is useful in combination with the UDO VMOD and allows you to use dynamic backends for loadbalancing.

 

 

ActiveDNS and UDO

ActiveDNS is a Varnish Enterprise VMOD that performs DNS updates at runtime. It circumvents a big limitation in Varnish: that hostnames are only resolved once when the VCL file is compiled. vmod_activedns does this at runtime for compatible VMODs such as vmod_udo. It works in the background and performs DNS resolutions with set intervals.

When changes occur, subscribed directors are signaled and new backends can be added or removed dynamically. A records, AAAA records and SRV records are supported. Here's some VCL code where we use ActiveDNS:

vcl 4.1;

import activedns;

sub vcl_init {
new group = activedns.dns_group();
group.set_host("example.com");
}

 

First, we import the VMOD and then we initialize it by creating a new DNS group. We'll assign a hostname to that group which is the hostname that will be resolved on set intervals. We can extract information from the DNS group by creating a monitor and subscribing to the DNS group.

By calling monitor.get_new_info(), we can extract information from that DNS group. The output is as follows:

sa4:172.24.2.11:80-pri:1-wgt:10

 

The output contains the socket address type, which, in this case, is IPv4, a socket address, which is a regular IPv4 IP address, a port, a priority, and a weight.

 

Dynamic loadbalancing

This information comes from DNS records, such as A records. When multiple results are returned, this information is used for dynamic loadbalancing. This also applies to SRV records where besides the target hostname, the port is also part of the record value and you can clearly see that the monitor output reflects the multiple hostnames.

You can use all that information to perform dynamic loadbalancing using vmod_udo.

vcl 4.1;

import activedns;
import udo;

backend default none;

sub vcl_init {
new group = activedns.dns_group();
group.set_host("example.com");

new udo_dir = udo.director();
udo_dir.set_type(random);
udo_dir.subscribe(group.get_tag());
}

sub vcl_backend_fetch {
set bereq.backend = udo_dir.backend();
}

 

When we look at the VCL code required to get this done, we first need to first import the UDO VMOD. We need to create an empty backend, create a UDO director object, assign a distribution type, and then subscribe to the DNS group, which will get updated once DNS changes apply.

In the end, it's just a matter of assigning the backend by calling the UDO director's .backend() function. The DNS update frequency comes from the TTL value of the DNS record. We can override this by setting our own TTL rule and switching it from the default value, which is abide, and changing it to force. This means, where the DNS update interval was initially an hour, now it is five seconds.

vcl 4.1;

import activedns;
import udo;

backend default none;

sub vcl_init {
new group = activedns.dns_group();
group.set_host("example.com");
group.set_ttl_rule(force);
group.set_ttl(5s);

new udo_dir = udo.director();
udo_dir.set_type(random);
udo_dir.subscribe(group.get_tag());
}

sub vcl_backend_fetch {
set bereq.backend = udo_dir.backend();
}

 

That was this week's episode about ActiveDNS. Have a look at last week's episode about UDO, because UDO and ActiveDNS are a really powerful combo. My personal preference also goes out to using SRV records because it gives you the flexibility to set your backend host and your port from within the DNS record. It also supports weights, which allows you to do weighted randomness and weighted hashing.

Join us next week for another technical Two-Minute Tuesday topic, presented to you in two minutes or less!

varnish_6_by_example_download