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

     1  ---
     2  layout: "docs"
     3  page_title: "DNS Interface"
     4  sidebar_current: "docs-agent-dns"
     5  description: |-
     6    One of the primary query interfaces for Consul is DNS. The DNS interface allows applications to make use of service discovery without any high-touch integration with Consul.
     7  ---
     8  
     9  # DNS Interface
    10  
    11  One of the primary query interfaces for Consul is DNS.
    12  The DNS interface allows applications to make use of service
    13  discovery without any high-touch integration with Consul.
    14  
    15  For example, instead of making HTTP API requests to Consul,
    16  a host can use the DNS server directly via name lookups
    17  like `redis.service.us-east-1.consul`. This query automatically
    18  translates to a lookup of nodes that provide the `redis` service,
    19  are located in the `us-east-1` datacenter, and have no failing health checks.
    20  It's that simple!
    21  
    22  There are a number of configuration options that are important for the DNS interface,
    23  specifically [`client_addr`](/docs/agent/options.html#client_addr),
    24  [`ports.dns`](/docs/agent/options.html#dns_port), [`recursors`](/docs/agent/options.html#recursors),
    25  [`domain`](/docs/agent/options.html#domain), and [`dns_config`](/docs/agent/options.html#dns_config).
    26  By default, Consul will listen on 127.0.0.1:8600 for DNS queries in the `consul.`
    27  domain, without support for further DNS recursion. Please consult the
    28  [documentation on configuration options](/docs/agent/options.html),
    29  specifically the configuration items linked above, for more details.
    30  
    31  There are a few ways to use the DNS interface. One option is to use a custom
    32  DNS resolver library and point it at Consul. Another option is to set Consul
    33  as the DNS server for a node and provide a
    34  [`recursors`](/docs/agent/options.html#recursors) configuration so that non-Consul queries
    35  can also be resolved. The last method is to forward all queries for the "consul."
    36  domain to a Consul agent from the existing DNS server.
    37  
    38  You can experiment with Consul's DNS server on the command line using tools such as `dig`:
    39  
    40      $ dig @127.0.0.1 -p 8600 redis.service.dc1.consul. ANY
    41  
    42  -> **Note:** In DNS, all queries are case-insensitive. A lookup of `PostgreSQL.node.dc1.consul` will find all nodes named `postgresql`.
    43  
    44  ## Node Lookups
    45  
    46  To resolve names, Consul relies on a very specific format for queries.
    47  There are fundamentally two types of queries: node lookups and service lookups.
    48  A node lookup, a simple query for the address of a named node, looks like this:
    49  
    50      <node>.node[.datacenter].<domain>
    51  
    52  For example, if we have a `foo` node with default settings, we could
    53  look for `foo.node.dc1.consul.` The datacenter is an optional part of
    54  the FQDN: if not provided, it defaults to the datacenter of the agent.
    55  If we know `foo` is running in the same datacenter as our local agent,
    56  we can instead use `foo.node.consul.` This convention allows for terse
    57  syntax where appropriate while supporting queries of nodes in remote
    58  datacenters as necessary.
    59  
    60  For a node lookup, the only records returned are A and AAAA records
    61  containing the IP address, and TXT records containing the
    62  `node_meta` values of the node.
    63  
    64  ```text
    65  $ dig @127.0.0.1 -p 8600 foo.node.consul ANY
    66  
    67  ; <<>> DiG 9.8.3-P1 <<>> @127.0.0.1 -p 8600 foo.node.consul ANY
    68  ; (1 server found)
    69  ;; global options: +cmd
    70  ;; Got answer:
    71  ;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 24355
    72  ;; flags: qr aa rd; QUERY: 1, ANSWER: 1, AUTHORITY: 1, ADDITIONAL: 0
    73  ;; WARNING: recursion requested but not available
    74  
    75  ;; QUESTION SECTION:
    76  ;foo.node.consul.		IN	ANY
    77  
    78  ;; ANSWER SECTION:
    79  foo.node.consul.	0	IN	A	10.1.10.12
    80  foo.node.consul.	0	IN	TXT	"meta_key=meta_value"
    81  foo.node.consul.	0	IN	TXT	"value only"
    82  
    83  
    84  ;; AUTHORITY SECTION:
    85  consul.			0	IN	SOA	ns.consul. postmaster.consul. 1392836399 3600 600 86400 0
    86  ```
    87  
    88  By default the TXT records value will match the node's metadata key-value
    89  pairs according to [RFC1464](https://www.ietf.org/rfc/rfc1464.txt).
    90  Alternatively, the TXT record will only include the node's metadata value when the
    91  node's metadata key starts with `rfc1035-`.
    92  
    93  ## Service Lookups
    94  
    95  A service lookup is used to query for service providers. Service queries support
    96  two lookup methods: standard and strict [RFC 2782](https://tools.ietf.org/html/rfc2782).
    97  
    98  By default, SRV weights are all set at 1, but changing weights is supported using the
    99  `Weights` attribute of the [service definition](/docs/agent/services.html).
   100  
   101  Note that DNS is limited in size per request, even when performing DNS TCP
   102  queries.
   103  
   104  For services having many instances (more than 500), it might not be possible to 
   105  retrieve the complete list of instances for the service.
   106  
   107  When DNS SRV response are sent, order is randomized, but weights are not
   108  taken into account. In the case of truncation different clients using weighted SRV 
   109  responses will have partial and inconsistent views of instances weights so the 
   110  request distribution could be skewed from the intended weights. In that case, 
   111  it is recommended to use the HTTP API to retrieve the list of nodes.
   112  
   113  ### Standard Lookup
   114  
   115  The format of a standard service lookup is:
   116  
   117      [tag.]<service>.service[.datacenter].<domain>
   118  
   119  The `tag` is optional, and, as with node lookups, the `datacenter` is as
   120  well. If no tag is provided, no filtering is done on tag. If no
   121  datacenter is provided, the datacenter of this Consul agent is assumed.
   122  
   123  If we want to find any redis service providers in our local datacenter,
   124  we could query `redis.service.consul.` If we want to find the PostgreSQL
   125  primary in a particular datacenter, we could query
   126  `primary.postgresql.service.dc2.consul.`
   127  
   128  The DNS query system makes use of health check information to prevent routing
   129  to unhealthy nodes. When a service query is made, any services failing their health
   130  check or failing a node system check will be omitted from the results. To allow
   131  for simple load balancing, the set of nodes returned is also randomized each time.
   132  These mechanisms make it easy to use DNS along with application-level retries
   133  as the foundation for an auto-healing service oriented architecture.
   134  
   135  For standard services queries, both A and SRV records are supported. SRV records
   136  provide the port that a service is registered on, enabling clients to avoid relying
   137  on well-known ports. SRV records are only served if the client specifically requests
   138  them, like so:
   139  
   140  ```text
   141  $ dig @127.0.0.1 -p 8600 consul.service.consul SRV
   142  
   143  ; <<>> DiG 9.8.3-P1 <<>> @127.0.0.1 -p 8600 consul.service.consul ANY
   144  ; (1 server found)
   145  ;; global options: +cmd
   146  ;; Got answer:
   147  ;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 50483
   148  ;; flags: qr aa rd; QUERY: 1, ANSWER: 3, AUTHORITY: 1, ADDITIONAL: 1
   149  ;; WARNING: recursion requested but not available
   150  
   151  ;; QUESTION SECTION:
   152  ;consul.service.consul.		IN	SRV
   153  
   154  ;; ANSWER SECTION:
   155  consul.service.consul.	0	IN	SRV	1 1 8300 foobar.node.dc1.consul.
   156  
   157  ;; ADDITIONAL SECTION:
   158  foobar.node.dc1.consul.	0	IN	A	10.1.10.12
   159  ```
   160  
   161  ### RFC 2782 Lookup
   162  
   163  The format for RFC 2782 SRV lookups is:
   164  
   165      _<service>._<protocol>[.service][.datacenter][.domain]
   166  
   167  Per [RFC 2782](https://tools.ietf.org/html/rfc2782), SRV queries should use
   168  underscores, `_`, as a prefix to the `service` and `protocol` values in a query to
   169  prevent DNS collisions. The `protocol` value can be any of the tags for a
   170  service. If the service has no tags, `tcp` should be used. If `tcp`
   171  is specified as the protocol, the query will not perform any tag filtering.
   172  
   173  Other than the query format and default `tcp` protocol/tag value, the behavior
   174  of the RFC style lookup is the same as the standard style of lookup.
   175  
   176  If you registered the service `rabbitmq` on port 5672 and tagged it with `amqp`,
   177  you could make an RFC 2782 query for its SRV record as `_rabbitmq._amqp.service.consul`:
   178  
   179  ```text
   180  $ dig @127.0.0.1 -p 8600 _rabbitmq._amqp.service.consul SRV
   181  
   182  ; <<>> DiG 9.8.3-P1 <<>> @127.0.0.1 -p 8600 _rabbitmq._amqp.service.consul ANY
   183  ; (1 server found)
   184  ;; global options: +cmd
   185  ;; Got answer:
   186  ;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 52838
   187  ;; flags: qr aa rd; QUERY: 1, ANSWER: 1, AUTHORITY: 0, ADDITIONAL: 1
   188  ;; WARNING: recursion requested but not available
   189  
   190  ;; QUESTION SECTION:
   191  ;_rabbitmq._amqp.service.consul.	IN	SRV
   192  
   193  ;; ANSWER SECTION:
   194  _rabbitmq._amqp.service.consul.	0	IN	SRV	1 1 5672 rabbitmq.node1.dc1.consul.
   195  
   196  ;; ADDITIONAL SECTION:
   197  rabbitmq.node1.dc1.consul.	0	IN	A	10.1.11.20
   198  ```
   199  
   200  Again, note that the SRV record returns the port of the service as well as its IP.
   201  
   202  ### Prepared Query Lookups
   203  
   204  The format of a prepared query lookup is:
   205  
   206      <query or name>.query[.datacenter].<domain>
   207  
   208  The `datacenter` is optional, and if not provided, the datacenter of this Consul
   209  agent is assumed.
   210  
   211  The `query or name` is the ID or given name of an existing
   212  [Prepared Query](/api/query.html). These behave like standard service
   213  queries but provide a much richer set of features, such as filtering by multiple
   214  tags and automatically failing over to look for services in remote datacenters if
   215  no healthy nodes are available in the local datacenter. Consul 0.6.4 and later also
   216  added support for [prepared query templates](/api/query.html#templates)
   217  which can match names using a prefix match, allowing one template to apply to
   218  potentially many services.
   219  
   220  To allow for simple load balancing, the set of nodes returned is randomized each time.
   221  Both A and SRV records are supported. SRV records provide the port that a service is
   222  registered on, enabling clients to avoid relying on well-known ports. SRV records are
   223  only served if the client specifically requests them.
   224  
   225  ### Connect-Capable Service Lookups
   226  
   227  To find Connect-capable services:
   228  
   229      <service>.connect.<domain>
   230  
   231  This will find all [Connect-capable](/docs/connect/index.html)
   232  endpoints for the given `service`. A Connect-capable endpoint may be
   233  both a proxy for a service or a natively integrated Connect application.
   234  The DNS interface does not differentiate the two.
   235  
   236  Most services will use a [proxy](/docs/connect/proxies.html) that handles
   237  service discovery automatically and therefore won't use this DNS format.
   238  This DNS format is primarily useful for [Connect-native](/docs/connect/native.html)
   239  applications.
   240  
   241  This endpoint currently only finds services within the same datacenter
   242  and doesn't support tags. This DNS interface will be expanded over time.
   243  If you need more complex behavior, please use the
   244  [catalog API](/api/catalog.html).
   245  
   246  ### UDP Based DNS Queries
   247  
   248  When the DNS query is performed using UDP, Consul will truncate the results
   249  without setting the truncate bit. This is to prevent a redundant lookup over
   250  TCP that generates additional load. If the lookup is done over TCP, the results
   251  are not truncated.
   252  
   253  ## Caching
   254  
   255  By default, all DNS results served by Consul set a 0 TTL value. This disables
   256  caching of DNS results. However, there are many situations in which caching is
   257  desirable for performance and scalability. This is discussed more in the guide
   258  for [DNS Caching](/docs/guides/dns-cache.html).
   259  
   260  ## WAN Address Translation
   261  
   262  By default, Consul DNS queries will return a node's local address, even when
   263  being queried from a remote datacenter. If you need to use a different address
   264  to reach a node from outside its datacenter, you can configure this behavior
   265  using the [`advertise-wan`](/docs/agent/options.html#_advertise-wan) and
   266  [`translate_wan_addrs`](/docs/agent/options.html#translate_wan_addrs) configuration
   267  options.