Search code examples
kubernetescoredns

DNS server in kubernetes for translate LAN hosts


I am using a baremetal cluster of 1 master and 2 nodes on premise in my home lab with istio, metallb and calico.

I want to create a DNS server in kubernetes that translates IPs for the hosts on the LAN.

Is it possible to use the coreDNS already installed in k8s?


Solution

  • Yes, it's possible but there are some points to consider when doing that. Most of them are described in the Stackoverflow answer below:

    For example: The DNS server would be resolving the queries that are internal to the Kubernetes cluster (like nslookup kubernetes.default.svc.cluster.local).


    I've included the example on how you can expose your CoreDNS to external sources and add a Service that would be pointing to some IP address

    Steps:

    • Modify the CoreDNS Service to be available outside.
    • Modify the configMap of your CoreDNS accordingly to:
    • Create a Service that is pointing to external device.
    • Test

    Modify the CoreDNS Service to be available outside.

    As you are new to Kubernetes you are probably aware on how Services work and which can be made available outside. You will need to change your CoreDNS Service from ClusterIP to either NodePort or LoadBalancer (I'd reckon LoadBalancer would be a better idea considering the metallb is used and you will access the DNS server on a port: 53)

    • $ kubectl edit --namespace=kube-system service/coredns (or kube-dns)

    A side note!

    CoreDNS is using TCP and UDP simultaneously, it could be an issue when creating a LoadBalancer. Here you can find more information on it:


    Modify the configMap of your CoreDNS

    If you would like to resolve domain like for example: example.org you will need to edit the configMap of CoreDNS in a following way:

    • $ kubectl edit configmap --namespace=kube-system coredns

    Add the line to the Corefile:

            k8s_external example.org
    

    This plugin allows an additional zone to resolve the external IP address(es) of a Kubernetes service. This plugin is only useful if the kubernetes plugin is also loaded.

    The plugin uses an external zone to resolve in-cluster IP addresses. It only handles queries for A, AAAA and SRV records; all others result in NODATA responses. To make it a proper DNS zone, it handles SOA and NS queries for the apex of the zone.

    -- CoreDNS.io: Plugins: K8s_external


    Create a Service that is pointing to external device.

    Following on the link that I've included, you can now create a Service that will point to an IP address:

    apiVersion: v1
    kind: Service
    metadata:
     name: test
     namespace: default
    spec:
     clusterIP: None
     externalIPs:
     - 192.168.200.123
     type: ClusterIP
    

    Test

    I've used minikube with --driver=docker (with NodePort) but I'd reckon your can use the ExternalIP of your LoadBalancer to check it:

    • dig @192.168.49.2 test.default.example.org -p 32261 +short
    192.168.200.123
    

    where:

    • @192.168.49.2 - IP address of minikube
    • test.default.example.org - service-name.namespace.k8s_external_domain
    • -p 32261 - NodePort port
    • +short - to limit the output

    Additional resources: