Customizing coredns on k3s to point a domain directly to the cluster loadbalancer

1/14/2021

I'm trying to add a rewrite to coredns to point a domain to the cluster loadbalancer (so that the request for that domain gets redirected back into the cluster). I can't seem to find a way to affect k3s' coredns configuration. Is there a way to change it?

(This is to work around https://github.com/jetstack/cert-manager/issues/1292#issuecomment-757283796 where a pod tries to contact another service in the cluster via a DNS name that points to the router's IP, which fails due to how NAT works.)

-- tibbe
coredns
k3s
kubernetes

1 Answer

1/15/2021

It is possible to configure CoreDNS to mapping one domain to another domain by adding rewrite rule. Suppose you have domain example.com and you want that domain to point to google.com domain.

To do this in CoreDNS, you can use the rewrite plugin.

Configuration of CoreDNS is stored in coredns ConfigMap in kube-system namespace. You can edit it using:<br>

root@kmaster:~# kubectl edit cm coredns -n kube-system

Just add one rewrite rule, like in the example below:<br>

apiVersion: v1
data:
  Corefile: |
    .:53 {
        errors
        health
        rewrite name example.com google.com # mapping example.com to google.com
        ready
...

Next you need to reload CoreDNS, to use new configuration. You may delete coredns Pod (coredns is deployed as Deployment, so new Pod will be created) or you can send it a SIGUSR1 to tell it to reload graceful.

Finally we can check how it works:

root@kmaster:~# kubectl run -it --rm --image=infoblox/dnstools:latest dnstools
dnstools# host -t A google.com
google.com has address 172.217.21.238
dnstools# host -t A example.com
example.com has address 172.217.21.238

You can find more information about rewrite plugin in Coredns rewrite documentation.

-- matt_j
Source: StackOverflow