Consul DNS

Using Consul DNS Interface

The DNS interface of Consul will help you to achieve service discovery without any personal-touch integration with the consul. The default port for listening a DNS server in consul agent is port 8600. Generally, we make a HTTP API request for name lookup but here we can use the DNS server directly like service.us-east .It is important to configure some details like client_addr, ports.DNS, recursors, domain, and, dns_config.

Prerequisite

  1. A Virtual Server instance
  2. Consul installed on the server
  3. A root user

Update System

We recommend you to upgrade all the available packages and repositories before performing any new operation. Execute the following command and it will do the job for you. On Linux

apt-get update
On RHEL/CentOS yum -y update

Node Lookups

You can query any DNS records such as host addresses, mail exchanges, and name servers on the command line by using dig tool. You can retrieve the address of any node by making a simple query. <node>.node[.datacenter].<domain> Suppose there is a node running named prome in the datacenter dc1 then you can look for that node using the following command.

prome.node.dc1.consul

If in case the node is running in the same datacenter as consul agent then you don’t need to specify the datacenter. you can look for that node using the following command.

 prome.node.consul

Service Lookups

Service lookup is a query to get information about the service provider. The format of standard service lookup is as follows: [tag.]<service>.service[.datacenter].<domain> tag is completely optional in the above command. Suppose you want to lookup for a service named prome in the local datacenter then you don’t need to specify the datacenter also.

prome.service.consul

You can also use the tag to filter the results of a query. Suppose, you wish to find the MariaDB primary in a specific datacenter then use the following command.

primary.mariadb.service.dc5.consul

Using Consul DNS

In this guide, we will see different ways to use the consul DNS interface. There are mainly 3 ways to use Consul DNS: 1. Employ a custom DNS resolver library 2. Appoint Consul as the DNS server for a instance 3. Forward Queries for Consul TLD From a DNS Server

Employ a custom DNS resolver library

One way of using consul DNS interface is to use a DNS resolver library for your language of choice and your code will query the interface directly. Apply custom logic to your code otherwise, you’ll be limited to IP address of the service. Otherwise, if you use a DNS interface then you will need to run the service on a specific port and your client should also know that port to access it.

Appoint Consul as the DNS server for a instance

You can use the consul DNS server for a node by configuring the host to deliver DNS queries directly to the local Consul agent’s DNS server. To do so, you will need to modify both the system and the Consul agent configuration.

First, you will have to change the system configuration. You will need to modify the resolv.conf file on the system using any text editor. Here we are using nano text editor.

nano resolv.conf

The file should look like this:

search YourDomain.com
nameserver 127.0.0.1

Please don’t forget to replace YourDomain.com with your actual domain.

Next, your consul agent configuration should look like this:

{
"datacenter": "dc1",
"data_dir": "/var/consul",
"recursors" : [ "8.8.8.8" ],
"ports" : {
"dns" : 53
},
"retry_join": [ "35.75.10.85""35.75.10.111", "35.75.10.123" ]
}

The Consul agent will continue to be able to deal with records for records outside of the consul TLD even if the server cluster is down or unavailable.

Forward Queries for Consul TLD From a DNS Server

You can use consul DNS to forward all the queries to consul agent from existing DNS server. We recommend you to use various BIND servers and run consul agent locally on all the BIND servers. So that whenever a query is accepted by a BIND server then it will be automatically shipped to its consul DNS server.

Conclusion

In this guide, you have learned using the Consul DNS interface in your consul cluster. You have also learned using the type of queries like Node lookup and service lookup.

About the author

Avatar

Sajid Qureshi

I am Sajid Qureshi, lives in India, working as DevOps Engineer and Technical writer. A passionate engineer who is still learning something new everyday. I enjoy exploring the world of Linux system administration, Installing and configuring and managing open source applications technology.