ping 可以解析主机名,而 dig 无法解析相同的主机名?

ping 可以解析主机名,而 dig 无法解析相同的主机名?

ping 可以解析主机名,而 dig 无法解析相同的主机名?

我遇到一个奇怪的问题,哪里ping能够解析某些 DNS 主机名,而dig不能。

我尝试dig +search使用 中的搜索条目/etc/resolv.conf,或dig @<nameserver>显式设置名称服务器,但这没有帮助。

我如何理解为什么ping解析主机名却dig无法解析?

kube@ctf-k8s-deploy-647d66b697-lxqkl:~$ cat /etc/resolv.conf
nameserver 100.64.0.10
search default.svc.cluster.local svc.cluster.local cluster.local eu-central-1.compute.internal
options ndots:5
kube@ctf-k8s-deploy-647d66b697-lxqkl:~$ dig +search ctf-k8s-deploy-647d66b697-lxqkl

; <<>> DiG 9.11.5-P4-5.1-Debian <<>> +search ctf-k8s-deploy-647d66b697-lxqkl
;; global options: +cmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NXDOMAIN, id: 52708
;; flags: qr rd ra; QUERY: 1, ANSWER: 0, AUTHORITY: 0, ADDITIONAL: 1

;; OPT PSEUDOSECTION:
; EDNS: version: 0, flags:; udp: 4096
;; QUESTION SECTION:
;ctf-k8s-deploy-647d66b697-lxqkl. IN    A

;; Query time: 0 msec
;; SERVER: 100.64.0.10#53(100.64.0.10)
;; WHEN: Mon Mar 23 14:46:46 UTC 2020
;; MSG SIZE  rcvd: 60

kube@ctf-k8s-deploy-647d66b697-lxqkl:~$ dig @100.64.0.10 +search ctf-k8s-deploy-647d66b697-lxqkl

; <<>> DiG 9.11.5-P4-5.1-Debian <<>> @100.64.0.10 +search ctf-k8s-deploy-647d66b697-lxqkl
; (1 server found)
;; global options: +cmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NXDOMAIN, id: 47568
;; flags: qr rd ra; QUERY: 1, ANSWER: 0, AUTHORITY: 0, ADDITIONAL: 1

;; OPT PSEUDOSECTION:
; EDNS: version: 0, flags:; udp: 4096
;; QUESTION SECTION:
;ctf-k8s-deploy-647d66b697-lxqkl. IN    A

;; Query time: 0 msec
;; SERVER: 100.64.0.10#53(100.64.0.10)
;; WHEN: Mon Mar 23 14:46:53 UTC 2020
;; MSG SIZE  rcvd: 60

kube@ctf-k8s-deploy-647d66b697-lxqkl:~$ ping ctf-k8s-deploy-647d66b697-lxqkl
PING ctf-k8s-deploy-647d66b697-lxqkl (100.96.1.8) 56(84) bytes of data.
64 bytes from ctf-k8s-deploy-647d66b697-lxqkl (100.96.1.8): icmp_seq=1 ttl=64 time=0.019 ms
64 bytes from ctf-k8s-deploy-647d66b697-lxqkl (100.96.1.8): icmp_seq=2 ttl=64 time=0.019 ms
64 bytes from ctf-k8s-deploy-647d66b697-lxqkl (100.96.1.8): icmp_seq=3 ttl=64 time=0.021 ms
^C
--- ctf-k8s-deploy-647d66b697-lxqkl ping statistics ---
3 packets transmitted, 3 received, 0% packet loss, time 40ms
rtt min/avg/max/mdev = 0.019/0.019/0.021/0.005 ms

更新

/etc/hosts

kube@ctf1-deploy1-89b48b46-zkqld:~$ cat /etc/hosts
# Kubernetes-managed hosts file.
127.0.0.1       localhost
::1     localhost ip6-localhost ip6-loopback
fe00::0 ip6-localnet
fe00::0 ip6-mcastprefix
fe00::1 ip6-allnodes
fe00::2 ip6-allrouters
100.96.3.22     ctf1-deploy1-89b48b46-zkqld

/etc/resolv.conf

kube@ctf1-deploy1-89b48b46-zkqld:~$ cat /etc/resolv.conf
nameserver 100.64.0.10
search ctf1-ns.svc.cluster.local svc.cluster.local cluster.local eu-central-1.compute.internal
options ndots:5

答案1

Ping 和 dig 使用不同的机制来解析 IP 地址。虽然从/etc/nsswitch.conf 文件中的ping行获取配置,但使用配置的名称服务器,因为 dig 仅查询 DNS 服务器。所以你的问题可能是关于 /etc/hosts 文件中定义的 DNS 服务器的问题。hosts: files dnsdig/etc/resolv.conf

还有一个澄清

相关内容