Resolve Consul DNS requests in Kubernetes
This topic describes how to configure Consul DNS in Kubernetes using a stub-domain configuration if using KubeDNS or a proxy configuration if using CoreDNS.
Once configured, DNS requests in the form <consul-service-name>.service.consul
will
resolve for services in Consul. This works from all Kubernetes namespaces.
Note: If you want requests to just <consul-service-name>
(without the .service.consul
) to resolve, then you'll need
to turn on Consul to Kubernetes Service Sync.
Consul DNS Cluster IP
To configure KubeDNS or CoreDNS you'll first need the ClusterIP
of the Consul
DNS service created by the Helm chart.
The default name of the Consul DNS service will be consul-dns
. Use
that name to get the ClusterIP
:
For this installation the ClusterIP
is 10.35.240.78
.
Note: If you've installed Consul using a different helm release name than consul
then the DNS service name will be <release-name>-consul-dns
.
KubeDNS
If using KubeDNS, you need to create a ConfigMap
that tells KubeDNS
to use the Consul DNS service to resolve all domains ending with .consul
:
Export the Consul DNS IP as an environment variable:
And create the ConfigMap
:
Ensure that the ConfigMap
was created successfully:
Note: The stubDomain
can only point to a static IP. If the cluster IP
of the Consul DNS service changes, then it must be updated in the config map to
match the new service IP for this to continue
working. This can happen if the service is deleted and recreated, such as
in full cluster rebuilds.
Note: If using a different zone than .consul
, change the stub domain to
that zone.
Now skip ahead to the Verifying DNS Works section.
CoreDNS Configuration
If using CoreDNS instead of KubeDNS in your Kubernetes cluster, you will
need to update your existing coredns
ConfigMap in the kube-system
namespace to
include a forward
definition for consul
that points to the cluster IP of the
Consul DNS service.
Edit the ConfigMap
:
And add the consul
block below the default .:53
block and replace
<consul-dns-service-cluster-ip>
with the DNS Service's IP address you
found previously.
Note: The consul proxy can only point to a static IP. If the cluster IP
of the consul-dns
service changes, then it must be updated to the new IP to continue
working. This can happen if the service is deleted and recreated, such as
in full cluster rebuilds.
Note: If using a different zone than .consul
, change the key accordingly.
OpenShift DNS Operator
Note: OpenShift CLI oc
is utilized below complete the following steps. You can find more details on how to install OpenShift CLI from Getting started with OpenShift CLI.
You can use DNS forwarding to override the default forwarding configuration in the /etc/resolv.conf
file by specifying
the consul-dns
service for the consul
subdomain (zone).
Find consul-dns
service clusterIP:
Edit the default
DNS Operator:
Append the following servers
section entry to the spec
section of the DNS Operator configuration:
Save the configuration changes and verify the dns-default
configmap has been updated:
Example output with updated consul
forwarding zone:
Verifying DNS Works
To verify DNS works, run a simple job to query DNS. Save the following
job to the file job.yaml
and run it:
Then query the pod name for the job and check the logs. You should see output similar to the following showing a successful DNS query. If you see any errors, then DNS is not configured properly.