github.com/outbrain/consul@v1.4.5/website/source/docs/guides/forwarding.html.md (about)

     1  ---
     2  layout: "docs"
     3  page_title: "Forwarding"
     4  sidebar_current: "docs-guides-forwarding"
     5  description: |-
     6    By default, DNS is served from port 53. On most operating systems, this requires elevated privileges. Instead of running Consul with an administrative or root account, it is possible to instead forward appropriate queries to Consul, running on an unprivileged port, from another DNS server or port redirect.
     7  ---
     8  
     9  # Forwarding DNS
    10  
    11  By default, DNS is served from port 53. On most operating systems, this
    12  requires elevated privileges. Instead of running Consul with an administrative
    13  or root account, it is possible to instead forward appropriate queries to Consul,
    14  running on an unprivileged port, from another DNS server or port redirect.
    15  
    16  In this guide, we will demonstrate forwarding from
    17  [BIND](https://www.isc.org/downloads/bind/) as well as
    18  [dnsmasq](http://www.thekelleys.org.uk/dnsmasq/doc.html),
    19  [Unbound](https://www.unbound.net/),
    20  [systemd-resolved](https://www.freedesktop.org/wiki/Software/systemd/resolved/), and [iptables](http://www.netfilter.org/).
    21  For the sake of simplicity, BIND and Consul are running on the same machine in 
    22  this example. For iptables the rules must be set on the same host as the Consul
    23  instance and relay hosts should not be on the same host or the redirects will 
    24  intercept the traffic.
    25  
    26  It is worth mentioning that, by default, Consul does not resolve DNS
    27  records outside the `.consul.` zone unless the
    28  [recursors](/docs/agent/options.html#recursors) configuration option
    29  has been set. As an example of how this changes Consul's behavior,
    30  suppose a Consul DNS reply includes a CNAME record pointing outside
    31  the `.consul` TLD. The DNS reply will only include CNAME records by
    32  default. By contrast, when `recursors` is set and the upstream resolver is
    33  functioning correctly, Consul will try to resolve CNAMEs and include
    34  any records (e.g. A, AAAA, PTR) for them in its DNS reply.
    35  
    36  You can either do one of the following:
    37  
    38  ### BIND Setup
    39  
    40  First, you have to disable DNSSEC so that Consul and BIND can communicate.
    41  Here is an example of such a configuration:
    42  
    43  ```text
    44  options {
    45    listen-on port 53 { 127.0.0.1; };
    46    listen-on-v6 port 53 { ::1; };
    47    directory       "/var/named";
    48    dump-file       "/var/named/data/cache_dump.db";
    49    statistics-file "/var/named/data/named_stats.txt";
    50    memstatistics-file "/var/named/data/named_mem_stats.txt";
    51    allow-query     { localhost; };
    52    recursion yes;
    53  
    54    dnssec-enable no;
    55    dnssec-validation no;
    56  
    57    /* Path to ISC DLV key */
    58    bindkeys-file "/etc/named.iscdlv.key";
    59  
    60    managed-keys-directory "/var/named/dynamic";
    61  };
    62  
    63  include "/etc/named/consul.conf";
    64  ```
    65  
    66  ### Zone File
    67  
    68  Then we set up a zone for our Consul managed records in `consul.conf`:
    69  
    70  ```text
    71  zone "consul" IN {
    72    type forward;
    73    forward only;
    74    forwarders { 127.0.0.1 port 8600; };
    75  };
    76  ```
    77  
    78  Here we assume Consul is running with default settings and is serving
    79  DNS on port 8600.
    80  
    81  ### Dnsmasq Setup
    82  
    83  Dnsmasq is typically configured via a `dnsmasq.conf` or a series of files in
    84  the `/etc/dnsmasq.d` directory. In Dnsmasq's configuration file
    85  (e.g. `/etc/dnsmasq.d/10-consul`), add the following:
    86  
    87  ```text
    88  # Enable forward lookup of the 'consul' domain:
    89  server=/consul/127.0.0.1#8600
    90  
    91  # Uncomment and modify as appropriate to enable reverse DNS lookups for
    92  # common netblocks found in RFC 1918, 5735, and 6598:
    93  #rev-server=0.0.0.0/8,127.0.0.1#8600
    94  #rev-server=10.0.0.0/8,127.0.0.1#8600
    95  #rev-server=100.64.0.0/10,127.0.0.1#8600
    96  #rev-server=127.0.0.1/8,127.0.0.1#8600
    97  #rev-server=169.254.0.0/16,127.0.0.1#8600
    98  #rev-server=172.16.0.0/12,127.0.0.1#8600
    99  #rev-server=192.168.0.0/16,127.0.0.1#8600
   100  #rev-server=224.0.0.0/4,127.0.0.1#8600
   101  #rev-server=240.0.0.0/4,127.0.0.1#8600
   102  ```
   103  
   104  Once that configuration is created, restart the `dnsmasq` service.
   105  
   106  Additional useful settings in `dnsmasq` to consider include (see
   107  [`dnsmasq(8)`](http://www.thekelleys.org.uk/dnsmasq/docs/dnsmasq-man.html)
   108  for additional details):
   109  
   110  ```
   111  # Accept DNS queries only from hosts whose address is on a local subnet.
   112  #local-service
   113  
   114  # Don't poll /etc/resolv.conf for changes.
   115  #no-poll
   116  
   117  # Don't read /etc/resolv.conf. Get upstream servers only from the command
   118  # line or the dnsmasq configuration file (see the "server" directive below).
   119  #no-resolv
   120  
   121  # Specify IP address(es) of other DNS servers for queries not handled
   122  # directly by consul. There is normally one 'server' entry set for every
   123  # 'nameserver' parameter found in '/etc/resolv.conf'. See dnsmasq(8)'s
   124  # 'server' configuration option for details.
   125  #server=1.2.3.4
   126  #server=208.67.222.222
   127  #server=8.8.8.8
   128  
   129  # Set the size of dnsmasq's cache. The default is 150 names. Setting the
   130  # cache size to zero disables caching.
   131  #cache-size=65536
   132  ```
   133  
   134  ### Unbound Setup
   135  
   136  Unbound is typically configured via a `unbound.conf` or a series of files in
   137  the `/etc/unbound/unbound.conf.d` directory. In an Unbound configuration file
   138  (e.g. `/etc/unbound/unbound.conf.d/consul.conf`), add the following:
   139  
   140  ```text
   141  #Allow insecure queries to local resolvers
   142  server:
   143    do-not-query-localhost: no
   144    domain-insecure: "consul"
   145  
   146  #Add consul as a stub-zone
   147  stub-zone:
   148    name: "consul"
   149    stub-addr: 127.0.0.1@8600
   150  ```
   151  
   152  You may have to add the following line to the bottom of your 
   153  `/etc/unbound/unbound.conf` file for the new configuration to be included:
   154  
   155  ```text
   156  include: "/etc/unbound/unbound.conf.d/*.conf"
   157  ```
   158  
   159  ### systemd-resolved Setup
   160  
   161  `systemd-resolved` is typically configured with `/etc/systemd/resolved.conf`. 
   162  To configure systemd-resolved to send queries for the consul domain to
   163  Consul, configure resolved.conf to contain the following:
   164  
   165  ```
   166  DNS=127.0.0.1
   167  Domains=~consul
   168  ```
   169  
   170  The main limitation with this configuration is that the DNS field
   171  cannot contain ports. So for this to work either Consul must be
   172  [configured to listen on port 53](https://www.consul.io/docs/agent/options.html#dns_port)
   173  instead of 8600 or you can use iptables to map port 53 to 8600. 
   174  The following iptables commands are sufficient to do the port
   175  mapping.
   176  
   177  ```
   178  [root@localhost ~]# iptables -t nat -A OUTPUT -d localhost -p udp -m udp --dport 53 -j REDIRECT --to-ports 8600
   179  [root@localhost ~]# iptables -t nat -A OUTPUT -d localhost -p tcp -m tcp --dport 53 -j REDIRECT --to-ports 8600
   180  ```
   181  
   182  Binding to port 53 will usually require running either as a privileged user (or on Linux running with the 
   183  CAP_NET_BIND_SERVICE capability). If using the Consul docker image you will need to add the following to the
   184  environment to allow Consul to use the port: `CONSUL_ALLOW_PRIVILEGED_PORTS=yes` 
   185  
   186  Note: With this setup, PTR record queries will still be sent out
   187  to the other configured resolvers in addition to Consul. 
   188  
   189  ### iptables Setup
   190  
   191  On Linux systems that support it, incoming requests and requests to
   192  the local host can use `iptables` to forward ports on the same machine
   193  without a secondary service. Since Consul, by default, only resolves
   194  the `.consul` TLD, it is especially important to use the `recursors`
   195  option if you wish the `iptables` setup to resolve for other domains.
   196  The recursors should not include the local host as the redirects would
   197  just intercept the requests.
   198  
   199  The iptables method is suited for situations where an external DNS
   200  service is already running in your infrastructure and is used as the
   201  recursor or if you want to use an existing DNS server as your query
   202  endpoint and forward requests for the consul domain to the Consul
   203  server. In both of those cases you may want to query the Consul server
   204  but not need the overhead of a separate service on the Consul host.
   205  
   206  ```
   207  [root@localhost ~]# iptables -t nat -A PREROUTING -p udp -m udp --dport 53 -j REDIRECT --to-ports 8600
   208  [root@localhost ~]# iptables -t nat -A PREROUTING -p tcp -m tcp --dport 53 -j REDIRECT --to-ports 8600
   209  [root@localhost ~]# iptables -t nat -A OUTPUT -d localhost -p udp -m udp --dport 53 -j REDIRECT --to-ports 8600
   210  [root@localhost ~]# iptables -t nat -A OUTPUT -d localhost -p tcp -m tcp --dport 53 -j REDIRECT --to-ports 8600
   211  ```
   212  
   213  ### macOS Setup
   214  
   215  On macOS systems, you can use the macOS system resolver to point all .consul requests to consul.
   216  Just add a resolver entry in /etc/resolver/ to point at consul. 
   217  documentation for this feature is available via: ```man5 resolver```.
   218  To setup create a new file ```/etc/resolver/consul``` (you will need sudo/root access) and put in the file:
   219  
   220  ```
   221  nameserver 127.0.0.1
   222  port 8600
   223  ```
   224  
   225  This is telling the macOS resolver daemon for all .consul TLD requests, ask 127.0.0.1 on port 8600.
   226  
   227  ### Testing
   228  
   229  First, perform a DNS query against Consul directly to be sure that the record exists:
   230  
   231  ```text
   232  [root@localhost ~]# dig @localhost -p 8600 primary.redis.service.dc-1.consul. A
   233  
   234  ; <<>> DiG 9.8.2rc1-RedHat-9.8.2-0.23.rc1.32.amzn1 <<>> @localhost primary.redis.service.dc-1.consul. A
   235  ; (1 server found)
   236  ;; global options: +cmd
   237  ;; Got answer:
   238  ;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 11536
   239  ;; flags: qr rd ra; QUERY: 1, ANSWER: 1, AUTHORITY: 0, ADDITIONAL: 0
   240  
   241  ;; QUESTION SECTION:
   242  ;primary.redis.service.dc-1.consul. IN A
   243  
   244  ;; ANSWER SECTION:
   245  primary.redis.service.dc-1.consul. 0 IN A 172.31.3.234
   246  
   247  ;; Query time: 4 msec
   248  ;; SERVER: 127.0.0.1#53(127.0.0.1)
   249  ;; WHEN: Wed Apr  9 17:36:12 2014
   250  ;; MSG SIZE  rcvd: 76
   251  ```
   252  
   253  Then run the same query against your BIND instance and make sure you get a
   254  valid result:
   255  
   256  ```text
   257  [root@localhost ~]# dig @localhost -p 53 primary.redis.service.dc-1.consul. A
   258  
   259  ; <<>> DiG 9.8.2rc1-RedHat-9.8.2-0.23.rc1.32.amzn1 <<>> @localhost primary.redis.service.dc-1.consul. A
   260  ; (1 server found)
   261  ;; global options: +cmd
   262  ;; Got answer:
   263  ;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 11536
   264  ;; flags: qr rd ra; QUERY: 1, ANSWER: 1, AUTHORITY: 0, ADDITIONAL: 0
   265  
   266  ;; QUESTION SECTION:
   267  ;primary.redis.service.dc-1.consul. IN A
   268  
   269  ;; ANSWER SECTION:
   270  primary.redis.service.dc-1.consul. 0 IN A 172.31.3.234
   271  
   272  ;; Query time: 4 msec
   273  ;; SERVER: 127.0.0.1#53(127.0.0.1)
   274  ;; WHEN: Wed Apr  9 17:36:12 2014
   275  ;; MSG SIZE  rcvd: 76
   276  ```
   277  
   278  If desired, verify reverse DNS using the same methodology:
   279  
   280  ```text
   281  [root@localhost ~]# dig @127.0.0.1 -p 8600 133.139.16.172.in-addr.arpa. PTR
   282  
   283  ; <<>> DiG 9.10.3-P3 <<>> @127.0.0.1 -p 8600 133.139.16.172.in-addr.arpa. PTR
   284  ; (1 server found)
   285  ;; global options: +cmd
   286  ;; Got answer:
   287  ;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 3713
   288  ;; flags: qr aa rd; QUERY: 1, ANSWER: 1, AUTHORITY: 0, ADDITIONAL: 0
   289  ;; WARNING: recursion requested but not available
   290  
   291  ;; QUESTION SECTION:
   292  ;133.139.16.172.in-addr.arpa.   IN  PTR
   293  
   294  ;; ANSWER SECTION:
   295  133.139.16.172.in-addr.arpa. 0  IN  PTR consul1.node.dc1.consul.
   296  
   297  ;; Query time: 3 msec
   298  ;; SERVER: 127.0.0.1#8600(127.0.0.1)
   299  ;; WHEN: Sun Jan 31 04:25:39 UTC 2016
   300  ;; MSG SIZE  rcvd: 109
   301  [root@localhost ~]# dig @127.0.0.1 +short -x 172.16.139.133
   302  consul1.node.dc1.consul.
   303  ```
   304  
   305  ### Troubleshooting
   306  
   307  If you don't get an answer from your DNS server (e.g. BIND, Dnsmasq) but you
   308  do get an answer from Consul, your best bet is to turn on your DNS server's
   309  query log to see what's happening.
   310  
   311  For BIND:
   312  
   313  ```text
   314  [root@localhost ~]# rndc querylog
   315  [root@localhost ~]# tail -f /var/log/messages
   316  ```
   317  
   318  The log may show errors like this:
   319  
   320  ```text
   321  error (no valid RRSIG) resolving
   322  error (no valid DS) resolving
   323  ```
   324  
   325  This indicates that DNSSEC is not disabled properly.
   326  
   327  If you see errors about network connections, verify that there are no firewall
   328  or routing problems between the servers running BIND and Consul.
   329  
   330  For Dnsmasq, see the `log-queries` configuration option and the `USR1`
   331  signal.