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