如何列出所有kubernetes DNS记录?

ted*_*r42 5 kubernetes

我有在(裸机)群集中运行的kube-dns。我知道它可以正常工作,因为我可以将服务插入到我创建的名称中并获得主机条目:

$ 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
(...)
Run Code Online (Sandbox Code Playgroud)

我不知道该怎么做是列出kube-dns持有的所有记录。我已经尝试过像dig这样的标准DNS技巧host -l,但无法获得它们。但是无论如何,必须有一种方法可以通过Kubernetes本身来实现。我尝试检查ConfigMap,但未找到所需的内容。

AAb*_*er 25

这篇文章将帮助您在运行 kube-dns 的集群上找到 K8s 服务的内部 DNS 记录:

  1. 找到kube-dns服务的ClusterIP:

kubectl -n kube-system get svc kube-dns

在此输入图像描述

现在我们知道内部 K8s DNS 解析器 IP 是 172.20.0.10

  1. 查找应用程序服务端点 IP:

kubectl -n fe get ep

在此输入图像描述

  1. 执行到应用程序 pod 中:

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

  1. 获取 DNS 服务名称:

在此输入图像描述

  1. 用于列出所有 K8s SVC DNS 记录的脚本:
#!/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
Run Code Online (Sandbox Code Playgroud)

  • 这应该是公认的答案,并且将 bash 脚本添加到每个人的 /usr/local/bin 中......非常有用,TY!✌️ (2认同)

men*_*nya 5

如果您使用kube-dns,它使用 dnsmaq 来缓存 DNS 记录,您可以通过此答案转储记录。

如果你使用的是coredns,它嵌入了一个缓存插件来缓存 DNS 记录,我发现没有办法获取这个缓存插件中的数据。但我发现 coredns 可以使用 etcd 作为后端,因此 DNS 记录可以缓存在 etcd 中,但这需要使用以下 Corefile 重新配置您的 coredns:

.:53 {
    etcd {
        path /skydns
        endpoint <etcd_endpoint>
        upstream /etc/resolv.conf
    }
    ...
}
Run Code Online (Sandbox Code Playgroud)