Perform static DNS queries

This topic describes how to query the Consul DNS to look up nodes and services registered with Consul. Refer to Enable Dynamic DNS Queries for information about using prepared queries.

Introduction

Node lookups and service lookups are the fundamental types of queries you can perform using the Consul DNS. Node lookups interrogate the catalog for named Consul agents. Service lookups interrogate the catalog for services registered with Consul. Refer to DNS Usage Overivew for additional background information.

Requirements

All versions of Consul support DNS lookup features.

ACLs

If ACLs are enabled, you must present a token linked with the necessary policies. We recommend using a separate token in production deployments for querying the DNS. By default, Consul agents resolve DNS requests using the preconfigured tokens in order of precedence:

The agent’s default token The built-in anonymous token.

The following table describes the available DNS lookups and required policies when ACLs are enabled:

LookupTypeDescriptionACLs Required
.node.consulNodeAllows Consul to resolve DNS requests for the target node. Example: <target>.node.consulnode:read
.service.consul
.connect.consul
.ingress.consul
*.virtual.consul
Service: standardAllows Consul to resolve DNS requests for target service instances running on ACL-authorized nodes. Example: <target>.service.consulservice:read
node:read

Tutorials: For hands-on guidance on how to configure an appropriate token for DNS, refer to the tutorial for Production Environments and Development Environments.

Node lookups

Specify the name of the node, datacenter, and domain using the following FQDN syntax:

  1. <node>.node[.<datacenter>.dc].<domain>

The datacenter subdomain is optional. By default, the lookup queries the datacenter of the agent.

By default, the domain is consul. Refer to Configure DNS Behaviors for information about using alternate domains.

Node lookup results

Node lookups return A and AAAA records that contain the IP address and TXT records containing the node_meta values of the node.

By default, TXT record values match the node’s metadata key-value pairs according to RFC1464. If the metadata key starts with rfc1035-, the TXT record only includes the node’s metadata value.

The following example lookup queries the foo node in the default datacenter:

  1. $ dig @127.0.0.1 -p 8600 foo.node.consul ANY
  2. ; <<>> DiG 9.8.3-P1 <<>> @127.0.0.1 -p 8600 foo.node.consul ANY
  3. ; (1 server found)
  4. ;; global options: +cmd
  5. ;; Got answer:
  6. ;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 24355
  7. ;; flags: qr aa rd; QUERY: 1, ANSWER: 1, AUTHORITY: 1, ADDITIONAL: 0
  8. ;; WARNING: recursion requested but not available
  9. ;; QUESTION SECTION:
  10. ;foo.node.consul. IN ANY
  11. ;; ANSWER SECTION:
  12. foo.node.consul. 0 IN A 10.1.10.12
  13. foo.node.consul. 0 IN TXT "meta_key=meta_value"
  14. foo.node.consul. 0 IN TXT "value only"
  15. ;; AUTHORITY SECTION:
  16. consul. 0 IN SOA ns.consul. postmaster.consul. 1392836399 3600 600 86400 0

Node lookups for Consul Enterprise

Consul Enterprise includes the admin partition concept, which is an abstraction that lets you define isolated administrative network areas. Refer to Admin Partitions for additional information.

Consul nodes reside in admin partitions within a datacenter. By default, node lookups query the same partition and datacenter of the Consul agent that received the DNS query.

Use the following query format to specify a partition for a node lookup:

  1. <node>.node[.<partition>.ap][.<datacenter>.dc].<domain>

Consul server agents are in the default partition. If you send a DNS query to Consul server agents, you must explicitly specify the partition of the target node if it is not default.

Service lookups

You can query the network for service providers using either the standard lookup method or strict RFC 2782 lookup method.

By default, all SRV records are weighted equally in service lookup responses, but you can configure the weights using the Weights attribute of the service definition. Refer to Define Services for additional information.

The DNS protocol limits the size of requests, even when performing DNS TCP queries, which may affect your experience querying for services. For services with more than 500 instances, you may not be able to retrieve the complete list of instances for the service. Refer to RFC 1035, Domain Names - Implementation and Specification for additional information.

Consul randomizes DNS SRV records and ignores weights specified in service configurations when printing responses. If records are truncated, each client using weighted SRV responses may have partial and inconsistent views of instance weights. As a result, the request distribution may be skewed from the intended weights. We recommend calling the /catalog/nodes API endpoint to retrieve the complete list of nodes. You can apply query parameters to API calls to sort and filter the results.

Standard lookups

To perform standard service lookups, specify tags, the name of the service, datacenter, cluster peer, and domain using the following syntax to query for service providers:

  1. [<tag>.]<service>.service[.<datacenter>.dc][.<cluster-peer>.peer].<domain>

The tag subdomain is optional. It filters responses so that only service providers containing the tag appear.

The datacenter subdomain is optional. By default, Consul interrogates the querying agent’s datacenter.

The cluster-peer name is optional, and specifies the cluster peer whose exported services should be the target of the query.

By default, the lookups query in the consul domain. Refer to Configure DNS Behaviors for information about using alternate domains.

Standard lookup results

Standard services queries return A and SRV records. SRV records include the port that the service is registered on. SRV records are only served if the client specifically requests them.

Services that fail their health check or that fail a node system check are omitted from the results. As a load balancing measure, Consul randomizes the set of nodes returned in the response. These mechanisms help you use DNS with application-level retries as the foundation for a self-healing service-oriented architecture.

The following example retrieves the SRV records for any redis service registered in Consul.

  1. $ dig @127.0.0.1 -p 8600 consul.service.consul SRV
  2. ; <<>> DiG 9.8.3-P1 <<>> @127.0.0.1 -p 8600 consul.service.consul ANY
  3. ; (1 server found)
  4. ;; global options: +cmd
  5. ;; Got answer:
  6. ;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 50483
  7. ;; flags: qr aa rd; QUERY: 1, ANSWER: 3, AUTHORITY: 1, ADDITIONAL: 1
  8. ;; WARNING: recursion requested but not available
  9. ;; QUESTION SECTION:
  10. ;consul.service.consul. IN SRV
  11. ;; ANSWER SECTION:
  12. consul.service.consul. 0 IN SRV 1 1 8300 foobar.node.dc1.consul.
  13. ;; ADDITIONAL SECTION:
  14. foobar.node.dc1.consul. 0 IN A 10.1.10.12

The following example command and FQDN retrieves the SRV records for the primary Postgres service in the secondary datacenter:

  1. $ dig @127.0.0.1 -p 8600 primary.postgresql.service.dc2.consul SRV
  2. ; <<>> DiG 9.8.3-P1 <<>> @127.0.0.1 -p 8600 primary.postgresql.service.dc2.consul ANY
  3. ; (1 server found)
  4. ;; global options: +cmd
  5. ;; Got answer:
  6. ;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 50483
  7. ;; flags: qr aa rd; QUERY: 1, ANSWER: 3, AUTHORITY: 1, ADDITIONAL: 1
  8. ;; WARNING: recursion requested but not available
  9. ;; QUESTION SECTION:
  10. ;consul.service.consul. IN SRV
  11. ;; ANSWER SECTION:
  12. consul.service.consul. 0 IN SRV 1 1 5432 primary.postgresql.service.dc2.consul.
  13. ;; ADDITIONAL SECTION:
  14. primary.postgresql.service.dc2.consul. 0 IN A 10.1.10.12

RFC 2782 lookup

Per RFC 2782, SRV queries must prepend service and protocol values with an underscore (_) to prevent DNS collisions. Use the following syntax to perform RFC 2782 lookups:

  1. _<service>._<protocol>[.service][.<datacenter>].<domain>

You can create lookups that filter results by placing service tags in the protocol field. Use the following syntax to create RFC 2782 lookups that filter results based on service tags:

  1. _<service>._<tag>[.service][.<datacenter>].<domain>

The following example queries the rabbitmq service tagged with amqp, which returns an instance at rabbitmq.node1.dc1.consul on port 5672:

  1. $ dig @127.0.0.1 -p 8600 _rabbitmq._amqp.service.consul SRV
  2. ; <<>> DiG 9.8.3-P1 <<>> @127.0.0.1 -p 8600 _rabbitmq._amqp.service.consul ANY
  3. ; (1 server found)
  4. ;; global options: +cmd
  5. ;; Got answer:
  6. ;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 52838
  7. ;; flags: qr aa rd; QUERY: 1, ANSWER: 1, AUTHORITY: 0, ADDITIONAL: 1
  8. ;; WARNING: recursion requested but not available
  9. ;; QUESTION SECTION:
  10. ;_rabbitmq._amqp.service.consul. IN SRV
  11. ;; ANSWER SECTION:
  12. _rabbitmq._amqp.service.consul. 0 IN SRV 1 1 5672 rabbitmq.node1.dc1.consul.
  13. ;; ADDITIONAL SECTION:
  14. rabbitmq.node1.dc1.consul. 0 IN A 10.1.11.20

You can also perform RFC 2782 lookups that target a specific cluster peer or datacenter by including <datacenter>.dc or <cluster-peer>.peer in the query labels:

  1. _<service>._<tag>[.service][.<datacenter>.dc][.<cluster-peer>.peer].<domain>

The following example queries the redis service tagged with tcp for the cluster peer phx1, which returns two instances, one at 10.1.11.83:29081 and one at 10.1.11.86:29142:

  1. dig @127.0.0.1 -p 8600 _redis._tcp.service.phx1.peer.consul SRV
  2. ; <<>> DiG 9.18.15 <<>> @127.0.0.1 -p 8600 _redis._tcp.service.phx1.peer.consul SRV
  3. ;; global options: +cmd
  4. ;; Got answer:
  5. ;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 40572
  6. ;; flags: qr aa rd ra; QUERY: 1, ANSWER: 2, AUTHORITY: 0, ADDITIONAL: 2
  7. ;; OPT PSEUDOSECTION:
  8. ; EDNS: version: 0, flags:; udp: 1232
  9. ;; QUESTION SECTION:
  10. ;_redis._tcp.service.phx1.peer.consul. IN SRV
  11. ;; ANSWER SECTION:
  12. _redis._tcp.service.phx1.peer.consul. 0 IN SRV 1 1 29081 0a000d53.addr.consul.
  13. _redis._tcp.service.phx1.peer.consul. 0 IN SRV 1 1 29142 0a010d56.addr.consul.
  14. ;; ADDITIONAL SECTION:
  15. 0a000d53.addr.consul. 0 IN A 10.1.11.83
  16. 0a010d56.addr.consul. 0 IN A 10.1.11.86

SRV responses for hosts in the .addr subdomain

If a service registered with Consul is configured with an explicit IP address or addresses in the address or tagged_address parameter, then Consul returns the hostname in the target field of the answer section for the DNS SRV query according to the following format:

  1. <hexadecimal-encoded IP>.addr.<datacenter>.consul`.

In the following example, the rabbitmq service is registered with an explicit IPv4 address of 192.0.2.10.

  1. node_name = "node1"
  2. services {
  3. name = "rabbitmq"
  4. address = "192.0.2.10"
  5. port = 5672
  6. }
  7. {
  8. "node_name": "node1",
  9. "services": [
  10. {
  11. "name": "rabbitmq",
  12. "address": "192.0.2.10",
  13. "port": 5672
  14. }
  15. ]
  16. }

The following example SRV query response contains a single record with a hostname written as a hexadecimal value:

  1. $ dig @127.0.0.1 -p 8600 -t srv _rabbitmq._tcp.service.consul +short
  2. 1 1 5672 c000020a.addr.dc1.consul.

You can convert hex octets to decimals to reveal the IP address. The following example command converts the hostname expressed as c000020a into the IPv4 address specified in the service registration.

  1. $ echo -n "c000020a" | perl -ne 'printf("%vd\n", pack("H*", $_))'
  2. 192.0.2.10

In the following example, the rabbitmq service is registered with an explicit IPv6 address of 2001:db8:1:2:cafe::1337.

  1. node_name = "node1"
  2. services {
  3. name = "rabbitmq"
  4. address = "2001:db8:1:2:cafe::1337"
  5. port = 5672
  6. }
  7. {
  8. "node_name": "node1",
  9. "services": [
  10. {
  11. "name": "rabbitmq",
  12. "address": "2001:db8:1:2:cafe::1337",
  13. "port": 5672
  14. }
  15. ]
  16. }

The following example SRV query response contains a single record with a hostname written as a hexadecimal value:

  1. $ dig @127.0.0.1 -p 8600 -t SRV _rabbitmq._tcp.service.consul +short
  2. 1 1 5672 20010db800010002cafe000000001337.addr.dc1.consul.

The response contains the fully-expanded IPv6 address with colon separators removed. The following command re-adds the colon separators to display the fully expanded IPv6 address that was specified in the service registration.

  1. $ echo -n "20010db800010002cafe000000001337" | perl -ne 'printf join(":", unpack("(A4)*", $_))."\n"'
  2. 2001:0db8:0001:0002:cafe:0000:0000:1337

Service lookups for Consul Enterprise

You can perform the following types of service lookups to query for services in another namespace, partition, and datacenter:

Use the following query format to specify namespace, partition, or datacenter:

  1. [<tag>.]<service>.service[.<namespace>.ns][.<partition>.ap][.<datacenter>.dc]<domain>

The namespace, partition, and datacenter are optional. By default, all service lookups use the default namespace within the partition and datacenter of the Consul agent that received the DNS query.

Consul server agents reside in the default partition. If DNS queries are addressed to Consul server agents, you must explicitly specify the partition of the target service when querying for services in partitions other than default.

To lookup services imported from a cluster peer, refer to Service virtual IP lookups for Consul Enterprise.

Alternative formats for specifying namespace

Although we recommend using the format described in Service lookups for Consul Enterprise for readability, you can use the alternate query format to specify namespaces but not partitions:

  1. [<tag>.]<service>.service.<namespace>.<datacenter>.<domain>

Service mesh-enabled service lookups

Add the .connect subdomain to query for service mesh-enabled services:

  1. <service>.connect.<domain>

This finds all service mesh-capable endpoints for the service. A service mesh-capable endpoint may be a proxy for a service or a natively integrated service mesh application. The DNS interface does not differentiate the two.

Many services use a proxy that handles service discovery automatically. As a result, they may not use the DNS format, which is primarily for service mesh-native applications. This endpoint only finds services within the same datacenter and does not support tags. Refer to the catalog API endpoint for more complex behaviors.

Service virtual IP lookups

Add the .virtual subdomain to queries to find the unique virtual IP allocated for a service:

  1. <service>.virtual[.<peer>].<domain>

This returns the unique virtual IP for any service mesh-capable service. Each service mesh service has a virtual IP assigned to it by Consul. Sidecar proxies use the virtual IP to enable the transparent proxy feature.

The peer name is an optional. The DNS uses it to query for the virtual IP of a service imported from the specified peer.

Consul adds virtual IPs to the tagged_addresses field in the service definition under the consul-virtual tag.

Service virtual IP lookups for Consul Enterprise

By default, a service virtual IP lookup checks the default namespace within the partition and datacenter of the Consul agent that received the DNS query. To lookup services imported from a partition in another cluster peered to the querying cluster or open-source datacenter, specify the namespace and peer name in the lookup:

  1. <service>.virtual[.<namespace>].<peer>.<domain>

To lookup services in a cluster peer that have not been imported, refer to Service lookups for Consul Enterprise.

Ingress Service Lookups

Add the .ingress subdomain to your DNS FQDN to find ingress-enabled services:

  1. <service>.ingress.<domain>

This finds all ingress gateway endpoints for the service.

This endpoint finds services within the same datacenter and does not support tags. Refer to the catalog API endpoint for more complex behaviors.

UDP-based DNS queries

When the DNS query is performed using UDP, Consul truncateß the results without setting the truncate bit. This prevents a redundant lookup over TCP that generates additional load. If the lookup is done over TCP, the results are not truncated.