DNS 解析故障排查实战指南
一、概述
DNS(Domain Name System)是互联网的基础设施,负责将域名解析为 IP 地址。DNS 故障会导致服务不可访问、应用连接失败等问题。本文介绍 DNS 解析故障的排查方法和解决方案。
DNS 解析流程
1
| 客户端 → 本地缓存 → hosts 文件 → 本地 DNS 服务器 → 根服务器 → TLD 服务器 → 权威服务器
|
常见故障现象
| 现象 |
可能原因 |
| 域名无法解析 |
DNS 服务器故障、域名过期、配置错误 |
| 解析结果错误 |
DNS 缓存污染、劫持、配置错误 |
| 解析速度慢 |
DNS 服务器响应慢、网络延迟 |
| 间歇性解析失败 |
DNS 服务器负载高、网络不稳定 |
二、排查工具
2.1 nslookup
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| nslookup www.example.com
nslookup www.example.com 8.8.8.8
nslookup -type=MX example.com nslookup -type=NS example.com nslookup -type=TXT example.com nslookup -type=SOA example.com
nslookup -debug www.example.com
|
2.2 dig
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
| dig www.example.com
dig @8.8.8.8 www.example.com
dig example.com MX dig example.com NS dig example.com TXT
dig +trace www.example.com
dig +short www.example.com
dig +stats www.example.com
|
2.3 host
1 2 3 4 5 6 7 8 9
| host www.example.com
host -t MX example.com host -t NS example.com
host -a www.example.com
|
2.4 其他工具
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
| ipconfig /displaydns
sudo systemd-resolve --flush-caches
sudo service nscd restart
sudo dscacheutil -flushcache; sudo killall -HUP mDNSResponder
ipconfig /flushdns
telnet 8.8.8.8 53 nc -zv 8.8.8.8 53
tcpdump -i any -n port 53 -w dns.pcap
|
三、排查流程
3.1 快速诊断流程图
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
| 开始 │ ▼ 检查本地 hosts 文件 ──有配置──→ 检查 hosts 配置是否正确 │ 无配置 │ ▼ ▼ 使用 dig/nslookup 测试 修正 hosts 配置 │ ▼ 本地 DNS 能否解析?───否───→ 检查 DNS 服务器配置 │ 是 ▼ 解析结果是否正确?───否───→ 检查 DNS 缓存/劫持 │ 是 ▼ 解析速度是否正常?───否───→ 更换 DNS 服务器/优化 │ 是 ▼ 问题解决
|
3.2 第一步:检查本地配置
1 2 3 4 5 6 7 8 9 10 11 12
| cat /etc/resolv.conf
cat /etc/hosts
nmcli dev show | grep DNS
systemctl status systemd-resolved resolvectl status
|
3.3 第二步:测试 DNS 解析
1 2 3 4 5 6 7 8 9 10 11 12 13
| dig www.example.com
dig @8.8.8.8 www.example.com dig @1.1.1.1 www.example.com dig @223.5.5.5 www.example.com
echo "=== 本地 DNS ===" dig +short www.example.com echo "=== Google DNS ===" dig @8.8.8.8 +short www.example.com
|
3.4 第三步:追踪解析链
1 2 3 4 5 6 7
| dig +trace www.example.com
|
3.5 第四步:检查 DNS 服务器健康
1 2 3 4 5 6 7 8 9 10 11
| dig @dns-server-ip www.example.com | grep "Query time"
for domain in www.baidu.com www.taobao.com www.jd.com; do echo "Testing $domain:" dig @dns-server-ip +short $domain done
dig @dns-server-ip +norecursive www.example.com
|
四、常见故障场景
4.1 场景一:域名完全无法解析
现象:
1 2 3 4 5
| $ nslookup www.example.com Server: 192.168.1.1 Address: 192.168.1.1#53
** server can't find www.example.com: NXDOMAIN
|
排查步骤:
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| whois example.com
dig example.com SOA
dig @8.8.8.8 www.example.com
cat /etc/resolv.conf
iptables -L -n | grep 53
|
解决方案:
1 2 3 4 5 6 7 8 9
| echo "nameserver 8.8.8.8" > /etc/resolv.conf echo "nameserver 1.1.1.1" >> /etc/resolv.conf
systemctl restart systemd-resolved
ping -c 4 8.8.8.8
|
4.2 场景二:解析结果错误
现象:
1 2 3
| $ dig www.example.com +short 5.6.7.8
|
排查步骤:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| sudo systemd-resolve --flush-caches
dig @ns1.example.com www.example.com
dig www.example.com | grep -A 5 "ANSWER SECTION"
dig @8.8.8.8 www.example.com +short dig @1.1.1.1 www.example.com +short
tcpdump -i eth0 -n port 53 -w dns_debug.pcap
|
解决方案:
4.3 场景三:解析速度慢
现象:
1 2 3
| $ time dig www.example.com ... ;; Query time: 2500 msec
|
排查步骤:
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| for dns in 8.8.8.8 1.1.1.1 223.5.5.5 114.114.114.114; do echo "Testing $dns:" dig @$dns www.example.com | grep "Query time" done
mtr 8.8.8.8
resolvectl statistics
|
解决方案:
1 2 3 4 5 6 7 8 9 10 11 12 13
| cat > /etc/resolv.conf << EOF nameserver 223.5.5.5 nameserver 114.114.114.114 EOF
apt install dnsmasq systemctl enable dnsmasq systemctl start dnsmasq
|
4.4 场景四:间歇性解析失败
现象:
1 2 3 4 5
| $ dig www.example.com +short 1.2.3.4 $ dig www.example.com +short ;; connection timed out; no servers could be reached
|
排查步骤:
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| for i in {1..10}; do dig @dns-server www.example.com +short sleep 1 done
ping -c 100 dns-server | tail -5
iptables -L -n -v | grep 53
journalctl -u systemd-resolved -f
|
解决方案:
1 2 3 4 5 6 7 8 9 10 11 12
| cat > /etc/resolv.conf << EOF nameserver 8.8.8.8 nameserver 8.8.4.4 nameserver 1.1.1.1 options timeout:2 attempts:3 EOF
|
4.5 场景五:内网域名无法解析
现象:
1 2 3
| $ dig www.google.com +short $ dig internal.corp.com +short
|
排查步骤:
1 2 3 4 5 6 7 8 9 10 11
| cat /etc/resolv.conf
ping -c 4 192.168.1.10
dig @192.168.1.10 internal.corp.com
|
解决方案:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| cat > /etc/resolv.conf << EOF search corp.com nameserver 192.168.1.10 nameserver 8.8.8.8 EOF
|
五、Kubernetes 环境 DNS 排查
5.1 CoreDNS 故障排查
1 2 3 4 5 6 7 8 9 10 11
| kubectl get pods -n kube-system -l k8s-app=kube-dns
kubectl logs -n kube-system -l k8s-app=kube-dns
kubectl run -it --rm dns-test --image=busybox:1.28 --restart=Never -- nslookup kubernetes.default
kubectl get cm -n kube-system coredns -o yaml
|
5.2 常见问题
1 2 3 4 5 6 7 8 9
|
kubectl scale deployment coredns -n kube-system --replicas=3
|
六、DNS 安全
6.1 DNS 劫持防护
6.2 DDoS 防护
七、最佳实践
7.1 客户端配置
1 2 3 4 5 6 7
| nameserver 223.5.5.5 nameserver 114.114.114.114 nameserver 8.8.8.8 options timeout:2 options attempts:3 options rotate
|
7.2 服务器端配置
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
| options { recursion yes; allow-recursion { trusted-nets; }; max-cache-size 2g; max-cache-ttl 86400; listen-on port 53 { any; }; clients-per-query 100; max-clients-per-query 500; version "not disclosed"; hostname "not disclosed"; };
|
7.3 监控告警
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
|
rate(dns_responses_total{rcode="NOERROR"}[5m]) / rate(dns_responses_total[5m])
histogram_quantile(0.95, rate(dns_request_duration_seconds_bucket[5m]))
- alert: DNSHighFailureRate expr: dns_failure_rate > 0.05 for: 5m labels: severity: warning annotations: summary: "DNS 失败率过高"
|
八、总结
DNS 故障排查的关键步骤:
- 确认范围:单个域名还是全部域名?内网还是外网?
- 分层排查:本地配置 → DNS 服务器 → 权威服务器
- 对比验证:使用多个 DNS 服务器对比结果
- 工具辅助:dig、nslookup、tcpdump 配合使用
- 监控预防:建立 DNS 监控告警机制
记住:DNS 是基础设施,稳定的 DNS 服务对业务连续性至关重要。