Community discussions

MikroTik App
 
Metzgermeister
just joined
Topic Author
Posts: 1
Joined: Sat Nov 27, 2021 6:37 am

Picking preferred DNS server

Sat Nov 27, 2021 6:50 am

I have set up a Pi-hole DNS and I am quite satisfied with it. My Mikrotik router (hEXr3) is configure to use peer DNS from my ISP, which I find useful for redundancy. While the Pi-hole is online, everything works as expected, DNS is routed via the Pi-hole and no ads are showing. However, if the Pi-hole temporarily loses connectivity (e.g. is rebooted), another DNS server is picked by the hEXr3 (from the dynamic servers) and the Pi-hole functionality is lost / circumvented.
I know that one solution would be to leave the Pi-hole as the sole DNS server on the network. However, my goal is to have redundant servers to fall back to. Is there an elegant way to pick the preferred DNS server which is used whenever it is on line, with automatic fallback in case of outage (and return to the preferred DNS as soon as it gets back on line)? I tried Tools / Netwatch, but the attempt was not successful.
Any ideas how to keep the preferred DNS while online (and have ample fallback solutions) would be appreciated.
 
ConnyMercier
Forum Veteran
Forum Veteran
Posts: 725
Joined: Tue Dec 17, 2019 1:08 pm

Re: Picking preferred DNS server

Tue Nov 30, 2021 6:02 am

Good Morning,

I did some quick Test and I can confirm your Problem....
I found two Solutions to your Problem...

Solution 1: Netwach
When Netwatch detect the Pihole-Server is back online, it will reset the Static-
DNS-Server and Flush all DNS-Cache entries. The Static-Server will be #1 again!!
In the following exemple the "Pihole-Server" has the IP-Address 10.11.12.253
/tool netwatch
add down-script="/log info message=\"Pihole Offline\"" host=10.11.12.253 up-script=\
    "/ip dns set servers=10.11.12.253\r\
    \n/ip dns cache flush\r\
    \n/log info message=\"Pihole Online\""

Solution 2: DHCP-Server
I quickly testet on a other Mikrotik-Device, Linux-PC and Windows...
If your DHCP-Server provides multiple DNS-Servers they will repect
the "sequence".
/ip dhcp-server network
add address=10.11.12.0/24 dns-server=10.11.12.253,192.168.178.254 gateway=10.11.12.254
 
holvoetn
Forum Guru
Forum Guru
Posts: 5480
Joined: Tue Apr 13, 2021 2:14 am
Location: Belgium

Re: Picking preferred DNS server

Tue Nov 30, 2021 4:20 pm

3th solution:
scheduler running every minute with script to test resolving capabilities of Pi-Hole (so this one also catches if Pi-Hole is still running but not responding anymore for whatever reason).
As long as it works, do nothing.
Once it goes down, change DNS settings on your router.

Caveat: for this to work smoothly, don't use too long lease times on your DHCP network. It does not make sense to check if your DNS server is active or not but keep the lease pending for a day, if you understand what I mean. I use 10 minutes. Could even be shorter.

The current version I use (constructed out of bits and pieces and some own changes. Not optimal, I am sure, but it works. E.g. the double DNS registration on router ánd DHCP-network, I still need to change that. But as long as it works as it is, I'm not touching it or they will kill me here at home :lol: )
:local currentDNS [/ip dns get server]
:local piholeDNS "192.168.2.14"
:local backupDNS "208.67.222.222,208.67.220.220"
:local testDomain "www.google.com"

:if ($currentDNS = $piholeDNS) do={
    :do {
       :resolve $testDomain server $piholeDNS
    } on-error={
        /ip dns set servers=$backupDNS
        /ip dhcp-server network set 0 dns-server=$backupDNS
        :log error "Pi-hole isn't working, using OpenDNS instead"
        /tool e-mail send to="info@<your mail address>" subject="Mikrotik DNS" body="Default DNS set, PiHole DOWN"
    }
} else={
    :do {
        :resolve $testDomain server $piholeDNS
        /ip dns set servers=$piholeDNS
        /ip dhcp-server network set 0 dns-server=$piholeDNS
        :log info "Pi-hole is working again,"
        /tool e-mail send to="info@<your mail address> " subject="Mikrotik DNS " body="PiHole operational again."
    } on-error={}
}
 
Shon3
just joined
Posts: 6
Joined: Thu Dec 08, 2022 7:35 pm

Re: Picking preferred DNS server

Sun Dec 18, 2022 3:25 pm

Thanks for the 3rd solution.

I also use Pi-Hole, but only for ad blocking. So I let MikroTik to be the single DNS server for all DHCP clients and only use your script to modify MikroTik's single DNS upstream server to be either Pi-Hole or Quad9+Google, and flush DNS cache on each change. No need to touch DHCP or its leases.

Who is online

Users browsing this forum: qwertykolea and 141 guests