K8s CoreDNS 偶发 i/o timeout 与集群 DNS 解析抖动排障
生产 Kubernetes Pod 内域名解析偶尔耗时数秒甚至抛出 i/o timeout 异常。本文深入分析 CoreDNS 副本数、高并发 UDP 丢包、conntrack 竞争机理与 NodeLocal DNSCache 落地实战。
在 Kubernetes 生产环境中,微服务频繁跨服务调用时,偶发抛出 DNS 解析超时异常。应用日志中记录 i/o timeout 或 Temporary failure in name resolution,查看 CoreDNS 的 Pod 日志,充斥着向前置上游 DNS 查询超时的报警记录:[ERROR] plugin/errors: 2 example.com. A: i/o timeout。
故障现象与日志特征
应用 Pod 出现间歇性解析失败:
dial tcp: lookup my-service.default.svc.cluster.local on 10.96.0.10:53: read udp 10.244.1.20:45123->10.96.0.10:53: i/o timeout
查看 CoreDNS 控制器日志:
kubectl logs -n kube-system -l k8s-app=kube-dns --tail=100
输出显示:
[ERROR] plugin/errors: 2 api.external.com. A: i/o timeout
[WARNING] plugin/forward: no nameservers found
这种超时具有突发性与偶发性,在业务 QPS 突增时尤其严重。
根本原因剖析
- Linux 内核 Netfilter / Conntrack 竞争丢包:Linux 默认在单一 Socket 快速连续发送 A(IPv4)和 AAAA(IPv6)记录查询时,由于 SNAT 映射在内核中存在微秒级锁竞争,第二个 UDP 报文极易被直接丢弃,导致必须等待 5 秒超时重试。
- CoreDNS 副本数与资源限制不足:整个数百节点的集群只部署了默认的 2 个 CoreDNS Pod,单 Pod CPU 达到
limits上限被系统强行限流(CPU Throttling),导致 UDP 数据包在 Socket 接收缓冲区(Receive Buffer)溢出。 - CoreDNS 向上游公网 DNS 发送过多外部解析:上游物理机或云厂商 VPC DNS(如 100.100.2.136)设有严格的单 IP 频次限制,被外部限流。
- ndots:5 规则导致内部无效查询放大:由于
/etc/resolv.conf默认配置为ndots:5,访问外部绝对域名时,会先在内部命名空间逐级补齐后缀查询 4 次,将集群整体 DNS 流量放大了 4 到 8 倍。
诊断排查步骤
- 检查 CoreDNS Pod 的 CPU 与内存使用率:
kubectl top pod -n kube-system -l k8s-app=kube-dns
观察 CPU 是否长期超过 80% 或已触顶 limits。
- 检查 CoreDNS 是否发生 CPU Throttling(限流): 登录 CoreDNS 所在的宿主机,查看 cgroup 统计:
cat /sys/fs/cgroup/cpu/kubepods.slice/.../cpu.stat
# 查看 nr_throttled 是否大幅递增
- 查看 CoreDNS 监控指标(Prometheus Metrics):
kubectl run curl-test --rm -it --image=curlimages/curl -- curl -s http://<coredns-pod-ip>:9153/metrics | grep coredns_dns_request_duration_seconds_bucket
观察耗时大于 2 秒的请求分布。
解决方案与生产优化
步骤一:部署 NodeLocal DNSCache(最彻底解决方案)
在每个 Kubernetes 节点上以 DaemonSet 模式部署 NodeLocal DNSCache,使得 Pod 直接向宿主机的本地虚拟 IP(如 169.254.20.10)发起 DNS 解析:
- 节点本地通过 TCP 与 CoreDNS 交互,完全绕过容易丢包的 UDP 连接跟踪(Conntrack);
- 内部常用域与外部公网域名直接在节点内存中完成缓存命中,大幅削减 CoreDNS 负载。
在生产集群中应用 NodeLocal 资源清单:
kubectl apply -f https://raw.githubusercontent.com/kubernetes/kubernetes/master/cluster/addons/dns/nodelocaldns/nodelocaldns.yaml
步骤二:水平扩容 CoreDNS 并配置自动弹性伸缩(HPA/AutoScaler)
根据集群节点数和 Pod 规模按比例自动扩容 CoreDNS 实例:
部署 cluster-proportional-autoscaler:
apiVersion: apps/v1
kind: Deployment
metadata:
name: coredns-autoscaler
namespace: kube-system
spec:
template:
spec:
containers:
- name: autoscaler
image: registry.k8s.io/cpa/cluster-proportional-autoscaler:v1.8.8
command:
- /cluster-proportional-autoscaler
- --namespace=kube-system
- --config-map=coredns-autoscaler
- --target=Deployment/coredns
- --default-params={"linear":{"coresPerReplica":256,"nodesPerReplica":16,"min":2,"max":100}}
此配置会自动保证每增加 16 个节点,自动为 CoreDNS 增加 1 个 Pod 副本。
步骤三:Corefile 调优
编辑 CoreDNS 配置:
kubectl edit configmap coredns -n kube-system
在 .:53 块中配置高并发优化:
.:53 {
errors
health {
lameduck 5s
}
ready
kubernetes cluster.local in-addr.arpa ip6.arpa {
pods insecure
fallthrough in-addr.arpa ip6.arpa
ttl 30
}
prometheus :9153
forward . /etc/resolv.conf {
max_concurrent 1000
prefer_udp
}
cache 30 {
success 8192
denial 4096
}
loop
reload
loadbalance
}
将 cache 扩大,并设置 max_concurrent 1000,有效防止上游阻塞拖垮 CoreDNS。


