Add a Custom Host to Kubernetes

In this post I will show you how to add custom hosts to kubernetes.

Parts of the Kubernetes series

CoreDNS is the DNS server in kubernetes. I some situation I need to add custom hosts to be resolvable in the kubernetes netwok.

First, edit the ConfigMap of the coredns using the following command:

kubectl edit cm -n kube-system coredns
# or
kubectl edit cm -n kube-system rke2-coredns-rke2-coredns
apiVersion: v1
kind: ConfigMap
data:
  Corefile: |
    .:53 {
        errors
        health {
          lameduck 5s
        }
        ready
        kubernetes cluster.local in-addr.arpa ip6.arpa {
          pods insecure
          fallthrough in-addr.arpa ip6.arpa
        }
        prometheus :9153
        forward . 8.8.8.8 8.8.4.4
        cache 30
        loop
        reload
        loadbalance
        hosts /etc/coredns/customdomains.db k8s.intra {
          172.17.14.10 rancher.k8s.intra
          172.17.14.10 hubble.k8s.intra
          172.17.14.10 grafana.k8s.intra
          172.17.14.10 alertmanager.k8s.intra
          172.17.14.10 prometheus.k8s.intra
          172.17.14.10 sso.k8s.intra
          fallthrough
        }
    }    

Delete coredens pods:

kubectl get pod -n kube-system | grep dns
kubectl delete pod -n kube-system core-dns-#########

It’s done! You can now reach that custom host from inside any pod on the cluster.