Velocizzare la risoluzione dei nomi con una cache DNS

From RVM Wiki
Jump to navigation Jump to search

Installando dnsmasq, si ottiene un servizio di DNS caching locale.

Qualcuno dice che funziona anche nscd, ma a mq non va.

Lo si fa un notebook, con interfaccia in DHCP

Installazione pacchetto

sudo apt-get install dnsmasq

Configurazione dnsmasq

Diciamo a dnsmasq che deve ascoltare solo in locale, e che deve loggare le query

sudoedit /etc/dnsmasq.conf

...
listen-address=127.0.0.1
...
log-queries
...

Configurazione del resolver

dnsmasq decide a quali dns forwardare le query (oltre che dai parametri che noi non abbiamo messo nel suo files di configurazione) leggendo /etc/resolv.conf. Naturalmente monitora questo file, in modo da rilevarne i cambiamenti on the fly.

Che bello.

Quindi basta dire al client dhclient locale di inserire come primo DNS 127.0.0.1 (che verrà usato come primo dns a cui le applicazioni si rivolgeranno) e di lasciare gli altri in /etc/resolv.conf (che invece verranno letti da dnsmasq, che ignorerà il 127.0.0.1).

Il trucco è semplice:

sudoedit /etc/dhcp3/dhclient.conf
...
prepend domain-name-servers 127.0.0.1;
...

Riavviamo il networking e verifichiamo che prima dei DNS dati in dhcp, ci sia 127.0.0.1:

sudo /etc/init.d/networking restart
...
cat /etc/resolv.conf 
...
nameserver 127.0.0.1
nameserver 62.4.0.1
nameserver 62.94.0.2
...

Riavviamo dnsmasq

sudo /etc/init.d/dnsmasq restart


Verifica funzionamento

Facciamo una query dns, e guardiamo il tempo:

dig pippo.com
...
;; Query time: 58 msec
;; SERVER: 127.0.0.1#53(127.0.0.1)
;; WHEN: Fri Dec  8 14:59:30 2006
;; MSG SIZE  rcvd: 124

Vediamo cosa dice il log:

sudo tail -f /var/log/syslog
...
Dec  8 14:59:30 rvmnot003 dnsmasq[10095]: query[A] pippo.com from 127.0.0.1
Dec  8 14:59:30 rvmnot003 dnsmasq[10095]: forwarded pippo.com to 62.4.0.1
Dec  8 14:59:30 rvmnot003 dnsmasq[10095]: forwarded pippo.com to 62.94.0.2
Dec  8 14:59:30 rvmnot003 dnsmasq[10095]: reply pippo.com is 69.39.88.70
...

Rifacciamo la query:

;; Query time: 2 msec
;; SERVER: 127.0.0.1#53(127.0.0.1)
;; WHEN: Fri Dec  8 15:01:30 2006
;; MSG SIZE  rcvd: 43

Notevole, solo 2ms, rispetto ai 58 di prima.

Inoltre dice che è cachato:

Dec  8 15:01:30 rvmnot003 dnsmasq[10095]: query[A] pippo.com from 127.0.0.1
Dec  8 15:01:30 rvmnot003 dnsmasq[10095]: cached pippo.com is 69.39.88.70

Per elencare il contenuto, sitantochè c'è il logging attivo:

sudo kill -SIGUSR1 `pidof dnsmasq`

Dec  8 15:03:36 rvmnot003 dnsmasq[10095]: time 1165586616, cache size 150, 0/8 cache insertions re-used unexpired cache entries.
Dec  8 15:03:36 rvmnot003 dnsmasq[10095]: Host                                     Address                        Flags     Expires
Dec  8 15:03:36 rvmnot003 dnsmasq[10095]: ip6-localhost                            ::1                            6FRI   H   
Dec  8 15:03:36 rvmnot003 dnsmasq[10095]: ip6-loopback                             ::1                            6F I   H   
Dec  8 15:03:36 rvmnot003 dnsmasq[10095]: ip6-allrouters                           ff02::2                        6FRI   H   
Dec  8 15:03:36 rvmnot003 dnsmasq[10095]: localhost                                127.0.0.1                      4FRI   H   
Dec  8 15:03:36 rvmnot003 dnsmasq[10095]: ip6-allnodes                             ff02::1                        6FRI   H   
Dec  8 15:03:36 rvmnot003 dnsmasq[10095]: ip6-localnet                             fe00::                         6FRI   H   
Dec  8 15:03:36 rvmnot003 dnsmasq[10095]: pippo.com                                69.39.88.70                    4F        Fri Dec  8 16:26:28 2006 
Dec  8 15:03:36 rvmnot003 dnsmasq[10095]: newsreader.mailgate.org                  master.mailgate.org            CF        Sat Dec  9 10:42:02 2006 
Dec  8 15:03:36 rvmnot003 dnsmasq[10095]: api.leoslyrics.com.galimberti.priv                                      6F   NX   Fri Dec  8 17:48:50 2006 
Dec  8 15:03:36 rvmnot003 dnsmasq[10095]: api.leoslyrics.com                       207.210.67.146                 4F        Fri Dec  8 18:58:02 2006 
Dec  8 15:03:36 rvmnot003 dnsmasq[10095]: api.leoslyrics.com                                                      6F   N    Fri Dec  8 17:48:50 2006 
Dec  8 15:03:36 rvmnot003 dnsmasq[10095]: ip6-mcastprefix                          ff00::                         6FRI   H   
Dec  8 15:03:36 rvmnot003 dnsmasq[10095]: ip6-allhosts                             ff02::3                        6FRI   H   
Dec  8 15:03:36 rvmnot003 dnsmasq[10095]: master.mailgate.org                      195.110.100.19                 4F        Sat Dec  9 10:42:07 2006 
Dec  8 15:03:36 rvmnot003 dnsmasq[10095]: master.mailgate.org                                                     6F   N    Fri Dec  8 16:42:04 2006 
Dec  8 15:03:36 rvmnot003 dnsmasq[10095]: rvmnot003                                127.0.1.1                      4FRI   H   

Rimuovere la direttiva log-queries

Riferimenti

  • man dnsmasq