24

I have kube-dns running in a (bare metal) cluster. I know that it works, as I can interpolate a service to a name I've created and get a host entry:

$ host elk-service-headless.default.svc.cluster.local
elk-service-headless.default.svc.cluster.local has address 10.42.0.151
elk-service-headless.default.svc.cluster.local has address 10.42.0.152
elk-service-headless.default.svc.cluster.local has address 10.42.0.153
(...)

What I can't figure out how to do is to list all of the records that kube-dns is holding. I've tried the standard DNS tricks like dig and host -l and can't get them. But in any case, there must be a way to do this from Kubernetes itself. I tried inspecting ConfigMaps and didn't find what I'm looking for.

tedder42
  • 23,519
  • 13
  • 86
  • 102

2 Answers2

20

This post will help you find the internal DNS record of your K8s services on a cluster that runs kube-dns:

  1. Find the ClusterIP of the kube-dns service:

kubectl -n kube-system get svc kube-dns

enter image description here

Now we know the internal K8s DNS resolver IP is 172.20.0.10

  1. Find the application service endpoint IP:

kubectl -n fe get ep

enter image description here

  1. Exec into the application pod:

kubectl -n fe exec -it fe-app-575fdf6cb6-lt7t6 -- sh

  1. Get DNS service name:

enter image description here

  1. A script to list all the K8s SVC DNS records:
#!/bin/bash

echo =========== Create an ubuntu pod ==================
kubectl run ubuntu --image=ubuntu -- bash -c "while true; do echo hello; sleep 10;done"

# Wait for the pod "ubuntu" to contain the status condition of type "Ready"
kubectl wait --for=condition=Ready pod/ubuntu

# Save a sorted list of IPs of all of the k8s SVCs:
kubectl get svc -A|egrep -v 'CLUSTER-IP|None'|awk '{print $4}'|sort -V > ips

# Copy the ip list to owr Ubuntu pod:
kubectl cp ips ubuntu:/

echo =========== Installing dig tool into the pod ===============
kubectl exec -it ubuntu -- apt-get update
kubectl exec -it ubuntu -- apt install -y dnsutils

# Print 7 blank lines
yes '' | sed 7q
echo =========== Print all k8s SVC DNS records ====================
for ip in $(cat ips); do echo -n "$ip "; kubectl exec -it ubuntu -- dig -x $ip +short; done
echo ====== End of list =====================

echo ========= Cleanup  ===============
kubectl delete po ubuntu
rm ips
exit 0
AAber
  • 1,562
  • 10
  • 14
5

If you are using kube-dns, it use dnsmaq to cache DNS record, you can dump record by this answer.

If you are using coredns, it embed a cache plugin to cache DNS record, and I find no way to get data in this cache plugin. But I find coredns can use etcd as backend, so the DNS record can be cached in etcd, but this need to reconfig your coredns with this Corefile:

.:53 {
    etcd {
        path /skydns
        endpoint <etcd_endpoint>
        upstream /etc/resolv.conf
    }
    ...
}
menya
  • 1,459
  • 7
  • 8
  • I know the only component can access to etcd is kube-apiserver in the k8s design. Is it a good way that coredns pods may access to etcd? – 홍한석 Apr 25 '22 at 02:18