主题
CoreDNS 与 NodeLocal DNSCache 从入门到精通
目标读者:Kubernetes 运维、SRE、DevOps、网络工程师
前置知识:Kubernetes 基础、DNS 基础、Linux 网络基础
教材版本:v1.0
目录
- Kubernetes DNS 概述
- CoreDNS 基础
- CoreDNS 架构与组件
- Corefile 配置详解
- CoreDNS 插件系统
- CoreDNS 在 Kubernetes 中的工作原理
- DNS 解析全链路
- NodeLocal DNSCache 原理
- NodeLocal DNSCache 安装与配置
- NodeLocal DNSCache 与 iptables 的关系
- 性能优化与最佳实践
- 故障排查实战
- 高可用与扩展
- 附录:速查表
第1章 Kubernetes DNS 概述
1.1 为什么 Kubernetes 需要 DNS
在 Kubernetes 中,Pod 的 IP 是动态变化的。服务间调用如果直接使用 IP,会面临:
- Pod 重建后 IP 改变
- 扩缩容导致后端 IP 列表变化
- 配置管理复杂
DNS 为服务提供了稳定的域名解析,让 Pod 通过域名访问服务,无需关心后端 IP 变化。
1.2 Kubernetes DNS 发展历程
| 阶段 | 方案 | 说明 |
|---|---|---|
| Kubernetes 1.2 之前 | kube-dns | 由 skyDNS + kube2sky + dnsmasq 组成 |
| Kubernetes 1.4+ | kube-dns | 整合为单容器,dnsmasq 缓存 + kubedns 解析 |
| Kubernetes 1.11+ | CoreDNS GA | 默认 DNS 方案 |
| Kubernetes 1.18+ | NodeLocal DNSCache GA | 节点级 DNS 缓存 |
1.3 Kubernetes 中的常见 DNS 记录
| 记录类型 | 示例 | 说明 |
|---|---|---|
| A / AAAA | web.default.svc.cluster.local | Service 解析为 ClusterIP |
| SRV | _http._tcp.web.default.svc.cluster.local | 端口服务记录 |
| PTR | 反向解析 | 用于日志、监控 |
| CNAME | external.default.svc.cluster.local | ExternalName 类型 Service |
| Pod A | 10-244-1-10.default.pod.cluster.local | Pod 域名(默认不启用) |
第2章 CoreDNS 基础
2.1 CoreDNS 是什么
CoreDNS 是一个灵活、可扩展的 DNS 服务器,使用 Go 语言编写。它是 Kubernetes 集群的默认 DNS 方案。
2.2 CoreDNS 的特点
- 插件化架构:通过插件扩展功能
- 配置简单:使用 Corefile 配置
- 高性能:Go 语言实现,支持高并发
- 云原生:与 Kubernetes 深度集成
- 灵活:可作为权威 DNS、递归 DNS、代理等
2.3 CoreDNS 与传统 DNS 对比
| 特性 | CoreDNS | kube-dns | dnsmasq |
|---|---|---|---|
| 架构 | 单进程插件化 | 多组件 | 轻量缓存 |
| 配置 | Corefile | 命令行参数 | 配置文件 |
| Kubernetes 集成 | 原生支持 | 支持 | 不支持 |
| 扩展性 | 强 | 弱 | 弱 |
| 维护 | 活跃 | 已弃用 | 活跃 |
第3章 CoreDNS 架构与组件
3.1 CoreDNS 部署架构
┌─────────────────────────────────────────┐
│ Kubernetes Cluster │
│ │
│ ┌───────────────────────────────┐ │
│ │ CoreDNS Deployment │ │
│ │ (2+ replicas) │ │
│ │ │ │
│ │ ┌─────────┐ ┌─────────┐ │ │
│ │ │CoreDNS-1│ │CoreDNS-2│ │ │
│ │ │ Pod │ │ Pod │ │ │
│ │ └────┬────┘ └────┬────┘ │ │
│ │ │ │ │ │
│ │ └──────┬──────┘ │ │
│ │ │ │ │
│ │ Service: kube-dns │ │
│ │ ClusterIP: 10.96.0.10│ │
│ └──────────────┬────────────────┘ │
│ │ │
│ ┌────────────┼────────────┐ │
│ │ │ │ │
│ ▼ ▼ ▼ │
│ Pod A Pod B Pod C │
│ │
└─────────────────────────────────────────┘3.2 CoreDNS Pod 组成
CoreDNS 通常以 Deployment 形式部署在 kube-system 命名空间:
bash
kubectl get deployment coredns -n kube-system
kubectl get pods -n kube-system -l k8s-app=kube-dns3.3 kube-dns Service
所有 Pod 的 /etc/resolv.conf 指向 kube-dns Service 的 ClusterIP:
bash
cat /etc/resolv.conf
# nameserver 10.96.0.10
# search default.svc.cluster.local svc.cluster.local cluster.local
# options ndots:53.4 CoreDNS 配置文件
CoreDNS 通过 ConfigMap 配置 Corefile:
bash
kubectl get configmap coredns -n kube-system -o yaml第4章 Corefile 配置详解
4.1 Corefile 基本结构
.: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
}
cache 30
loop
reload
loadbalance
}4.2 关键字段解释
| 字段 | 说明 |
|---|---|
.:53 | 监听所有接口的 53 端口 |
errors | 启用错误日志 |
health | 健康检查端点 |
ready | 就绪检查端点 |
kubernetes | Kubernetes 插件,解析集群内域名 |
prometheus | 暴露 Prometheus 指标 |
forward | 将无法解析的域名转发到上游 DNS |
cache | 启用 DNS 缓存 |
loop | 检测 DNS 转发循环 |
reload | 自动重新加载 Corefile |
loadbalance | 对 A/AAAA 记录进行轮询 |
4.3 kubernetes 插件详解
kubernetes cluster.local in-addr.arpa ip6.arpa {
pods insecure
fallthrough in-addr.arpa ip6.arpa
ttl 30
}| 参数 | 说明 |
|---|---|
cluster.local | 集群域名后缀 |
in-addr.arpa ip6.arpa | 支持 IPv4/IPv6 PTR 反向解析 |
pods insecure | 为 Pod 创建 A 记录(使用 Pod IP) |
pods verified | 验证 Pod 存在后才返回记录 |
pods disabled | 不创建 Pod A 记录 |
fallthrough | 无法解析时转发给下一个插件 |
ttl | 默认 TTL |
4.4 forward 插件详解
forward . /etc/resolv.conf {
max_concurrent 1000
}| 参数 | 说明 |
|---|---|
. | 匹配所有域名 |
/etc/resolv.conf | 上游 DNS 来源 |
max_concurrent | 最大并发查询数 |
expire | 连接过期时间 |
policy | 上游选择策略(random/round_robin/sequential) |
4.5 cache 插件详解
cache 30| 参数 | 说明 |
|---|---|
30 | 默认缓存 TTL 30 秒 |
success | 成功响应缓存 |
denial | NXDOMAIN 等否定响应缓存 |
prefetch | 预取配置 |
示例:
cache {
success 9984 300
denial 9984 60
prefetch 10 10m 25%
}4.6 自定义 Corefile 示例
场景1:指定上游 DNS
forward . 8.8.8.8 8.8.4.4 {
max_concurrent 1000
}场景2:配置存根域
example.com:53 {
forward . 10.10.0.10 10.10.0.11
}场景3:自定义 host
.:53 {
hosts {
10.0.0.1 api.example.com
fallthrough
}
kubernetes cluster.local in-addr.arpa ip6.arpa {
pods insecure
fallthrough in-addr.arpa ip6.arpa
}
forward . /etc/resolv.conf
cache 30
}第5章 CoreDNS 插件系统
5.1 常用插件分类
| 分类 | 插件 | 作用 |
|---|---|---|
| 解析 | kubernetes、hosts、file、auto | 提供 DNS 记录 |
| 转发 | forward、proxy | 转发查询到上游 |
| 缓存 | cache | 缓存 DNS 响应 |
| 监控 | prometheus、errors、log | 指标和日志 |
| 安全 | loop、loadbalance、rewrite | 安全与负载均衡 |
| 工具 | reload、ready、health、template | 运维工具 |
5.2 常用插件速查
| 插件 | 说明 |
|---|---|
kubernetes | 解析 Kubernetes 内部域名 |
forward | 转发到上游 DNS |
cache | 缓存 DNS 结果 |
errors | 记录错误日志 |
log | 记录所有 DNS 查询日志 |
health | 健康检查 |
ready | 就绪检查 |
prometheus | Prometheus 指标 |
loop | 检测转发循环 |
reload | 热加载 Corefile |
rewrite | 重写 DNS 查询 |
hosts | 类似 /etc/hosts |
loadbalance | A/AAAA 记录轮询 |
5.3 插件执行顺序
CoreDNS 按照 Corefile 中插件的定义顺序处理 DNS 请求:
查询进入
│
▼
plugin 1
│(fallthrough 则继续)
▼
plugin 2
│
▼
plugin 3
│
▼
返回响应注意:某些插件(如
cache)会拦截并直接返回,不会 fallthrough。
第6章 CoreDNS 在 Kubernetes 中的工作原理
6.1 Pod DNS 配置来源
每个 Pod 的 DNS 配置由 kubelet 写入,来源包括:
clusterDNS:kubelet 参数,通常是10.96.0.10clusterDomain:默认cluster.localdnsPolicy:Pod 的 DNS 策略dnsConfig:Pod 自定义 DNS 配置
6.2 Pod DNS 策略
| 策略 | 说明 |
|---|---|
ClusterFirst | 默认,使用集群 DNS |
Default | 继承节点 DNS 配置 |
ClusterFirstWithHostNet | hostNetwork Pod 使用集群 DNS |
None | 完全自定义 DNS |
6.3 CoreDNS 解析 Kubernetes 域名
当 Pod 查询 web.default.svc.cluster.local:
- 请求发送到
kube-dnsService(10.96.0.10:53) - Service 负载均衡到某个 CoreDNS Pod
- CoreDNS 的
kubernetes插件查询 Kubernetes API - 返回 Service 对应的 ClusterIP
6.4 CoreDNS 与 API Server 的交互
CoreDNS 通过 Kubernetes API Server 获取 Service 和 Endpoint 信息:
CoreDNS Pod
│
│ List/Watch Service、Endpoint
▼
Kubernetes API Server
│
▼
etcd因此,API Server 压力大或网络延迟会影响 CoreDNS 性能。
第7章 DNS 解析全链路
7.1 普通 Pod 查询 Service 域名
Pod A
│
│ 1. 查询 web.default.svc.cluster.local
▼
/etc/resolv.conf (nameserver 10.96.0.10)
│
▼
kube-dns Service (10.96.0.10:53)
│
│ kube-proxy iptables/ipvs 负载均衡
▼
CoreDNS Pod
│
│ kubernetes 插件查询 API Server
▼
返回 ClusterIP
│
▼
Pod A 拿到 IP 后发起 HTTP/TCP 请求7.2 查询外部域名
Pod A
│
│ 查询 www.example.com
▼
CoreDNS
│
│ kubernetes 插件无法匹配
▼
forward 插件
│
▼
上游 DNS(如 8.8.8.8 或节点 /etc/resolv.conf)
│
▼
返回公网 IP7.3 跨命名空间查询
bash
# 同命名空间
curl http://web-service
# 跨命名空间
curl http://web-service.production
# 完整 FQDN
curl http://web-service.production.svc.cluster.local7.4 ndots 与 DNS 查询次数
/etc/resolv.conf 中的 options ndots:5 表示:
- 如果域名中的点号少于 5 个,会自动补全 search 域依次查询。
- 例如
web-service会依次查询:web-service.default.svc.cluster.localweb-service.svc.cluster.localweb-service.cluster.local
这可能导致不必要的 DNS 查询放大。
第8章 NodeLocal DNSCache 原理
8.1 为什么需要 NodeLocal DNSCache
在大规模集群中,所有 Pod 的 DNS 请求都经过 kube-dns Service 转发到 CoreDNS Pod,存在以下问题:
| 问题 | 说明 |
|---|---|
| 高并发 DNS 请求 | 大量 Pod 同时查询,CoreDNS Pod 压力大 |
| 跨节点流量 | DNS 请求可能跨节点转发,增加网络延迟 |
| conntrack 压力 | 大量 DNS 短连接导致 conntrack 表膨胀 |
| DNS 超时 | 网络抖动时 Pod 与 CoreDNS 之间可能出现超时 |
| ndots 放大 | 每个域名多次查询,进一步放大请求量 |
8.2 NodeLocal DNSCache 是什么
NodeLocal DNSCache 是运行在每个节点上的 DNS 缓存代理(通常使用 CoreDNS 或 node-cache),Pod 将 DNS 请求发送到本节点的缓存,减少跨节点流量和 CoreDNS 压力。
8.3 NodeLocal DNSCache 架构
┌─────────────────────────────────────────┐
│ Node 1 │
│ │
│ ┌─────────────────────────────┐ │
│ │ NodeLocal DNSCache Pod │ │
│ │ DaemonSet │ │
│ │ │ │
│ │ Local IP: 169.254.20.10 │ │
│ │ (或 10.0.0.10 等) │ │
│ └─────────────┬───────────────┘ │
│ │ │
│ ┌───────────┼───────────┐ │
│ │ │ │ │
│ ▼ ▼ ▼ │
│ Pod A Pod B Pod C │
│ │
└─────────────────────────────────────────┘
│
│ 缓存未命中时转发
▼
┌─────────────────┐
│ CoreDNS Pod │
│ (kube-system) │
└─────────────────┘8.4 NodeLocal DNSCache 工作流程
- Pod 发起 DNS 查询
- 请求被重定向到本节点的 NodeLocal DNSCache(通过 iptables 或接口绑定)
- NodeLocal DNSCache 检查本地缓存:
- 命中:直接返回
- 未命中:转发到上游 CoreDNS
- CoreDNS 返回结果,NodeLocal DNSCache 缓存后返回给 Pod
8.5 NodeLocal DNSCache 的优势
| 优势 | 说明 |
|---|---|
| 降低延迟 | 本节点缓存,减少网络跳转 |
| 减少 CoreDNS 压力 | 大部分查询在本地命中 |
| 减少 conntrack | 短连接减少 |
| 提升稳定性 | 即使 CoreDNS 短暂不可用,缓存仍可服务 |
| 避免跨节点流量 | DNS 流量不出节点 |
第9章 NodeLocal DNSCache 安装与配置
9.1 安装方式
NodeLocal DNSCache 通常作为 DaemonSet 部署在 kube-system 命名空间。
9.2 官方 YAML 示例
yaml
apiVersion: apps/v1
kind: DaemonSet
metadata:
name: node-local-dns
namespace: kube-system
labels:
k8s-app: node-local-dns
spec:
updateStrategy:
rollingUpdate:
maxUnavailable: 10%
selector:
matchLabels:
k8s-app: node-local-dns
template:
metadata:
labels:
k8s-app: node-local-dns
spec:
priorityClassName: system-node-critical
serviceAccountName: node-local-dns
hostNetwork: true
dnsPolicy: Default
containers:
- name: node-cache
image: registry.k8s.io/dns/k8s-dns-node-cache:1.22.20
resources:
requests:
cpu: 25m
memory: 5Mi
args:
- "-localip"
- "169.254.20.10,10.96.0.10"
- "-conf"
- "/etc/Corefile"
- "-upstreamsvc"
- "kube-dns-upstream"
securityContext:
privileged: true
ports:
- containerPort: 53
name: dns
protocol: UDP
- containerPort: 53
name: dns-tcp
protocol: TCP
- containerPort: 9253
name: metrics
protocol: TCP
volumeMounts:
- name: config-volume
mountPath: /etc/coredns
- name: kube-dns-config
mountPath: /etc/kube-dns
volumes:
- name: config-volume
configMap:
name: node-local-dns
items:
- key: Corefile
path: Corefile.base
- name: kube-dns-config
configMap:
name: kube-dns
optional: true9.3 Corefile 配置
yaml
apiVersion: v1
kind: ConfigMap
metadata:
name: node-local-dns
namespace: kube-system
data:
Corefile: |
cluster.local:53 {
errors
cache {
success 9984 30
denial 9984 5
}
reload
loop
bind 169.254.20.10 10.96.0.10
forward . 10.96.0.10 {
force_tcp
}
prometheus :9253
health 169.254.20.10:8080
}
in-addr.arpa:53 {
errors
cache 30
reload
loop
bind 169.254.20.10 10.96.0.10
forward . 10.96.0.10 {
force_tcp
}
prometheus :9253
}
ip6.arpa:53 {
errors
cache 30
reload
loop
bind 169.254.20.10 10.96.0.10
forward . 10.96.0.10 {
force_tcp
}
prometheus :9253
}
.:53 {
errors
cache 30
reload
loop
bind 169.254.20.10 10.96.0.10
forward . /etc/resolv.conf
prometheus :9253
}9.4 关键参数说明
| 参数 | 说明 |
|---|---|
-localip | NodeLocal DNSCache 监听的本地 IP |
-upstreamsvc | 上游 DNS Service 名称 |
bind | 绑定到指定 IP |
force_tcp | 强制使用 TCP 与上游通信 |
cache | 缓存配置 |
9.5 修改 kubelet 配置
安装 NodeLocal DNSCache 后,需要让 Pod 使用新的 DNS IP。通常有两种方式:
方式1:直接修改 Pod resolv.conf(推荐配合 iptables 方式)
通过 iptables 将发往 10.96.0.10 的流量重定向到 169.254.20.10。
方式2:修改 kubelet clusterDNS
将 kubelet 的 --cluster-dns 参数改为 NodeLocal DNSCache 的 localip。
注意:直接修改 clusterDNS 需要滚动更新节点,影响较大。
第10章 NodeLocal DNSCache 与 iptables 的关系
10.1 流量劫持原理
NodeLocal DNSCache 通过 iptables 规则将 Pod 发往 kube-dns Service(10.96.0.10)的流量重定向到本节点的缓存:
bash
# 将目标为 10.96.0.10:53 的流量 DNAT 到 169.254.20.10:53
iptables -t nat -A PREROUTING -d 10.96.0.10/32 -p udp --dport 53 -j DNAT --to-destination 169.254.20.10:53
iptables -t nat -A PREROUTING -d 10.96.0.10/32 -p tcp --dport 53 -j DNAT --to-destination 169.254.20.10:5310.2 NOTRACK 减少 conntrack 压力
NodeLocal DNSCache 还会添加 NOTRACK 规则,避免 DNS 流量被 conntrack 跟踪:
bash
iptables -t raw -A PREROUTING -d 10.96.0.10/32 -p udp --dport 53 -j NOTRACK
iptables -t raw -A OUTPUT -d 10.96.0.10/32 -p udp --dport 53 -j NOTRACK10.3 NodeLocal DNSCache 创建的规则
部署 NodeLocal DNSCache 后,节点上会出现:
bash
# nat 表
iptables -t nat -L PREROUTING -n -v | grep -E "10.96.0.10|169.254.20.10"
# raw 表
iptables -t raw -L PREROUTING -n -v | grep -E "10.96.0.10|169.254.20.10"
# filter 表
iptables -L INPUT -n -v | grep -E "10.96.0.10|169.254.20.10"10.4 与 kube-proxy 规则的交互
Pod DNS 查询
│
▼
目标 10.96.0.10:53
│
▼
PREROUTING
│
├─ NodeLocal DNAT 规则(先匹配)
│ │
│ ▼
│ 169.254.20.10:53
│
└─ kube-proxy KUBE-SERVICES 规则(被跳过)由于 NodeLocal 的 DNAT 规则通常插入在 PREROUTING 链较前位置,优先于 kube-proxy。
第11章 性能优化与最佳实践
11.1 CoreDNS 优化
增加副本数
bash
kubectl scale deployment coredns -n kube-system --replicas=4建议副本数 ≥ 2,大规模集群适当增加。
调整资源限制
yaml
resources:
limits:
memory: 512Mi
cpu: 1000m
requests:
memory: 128Mi
cpu: 100m启用缓存
cache {
success 9984 300
denial 9984 60
}配置 Pod 反亲和性
yaml
affinity:
podAntiAffinity:
preferredDuringSchedulingIgnoredDuringExecution:
- weight: 100
podAffinityTerm:
labelSelector:
matchExpressions:
- key: k8s-app
operator: In
values:
- kube-dns
topologyKey: kubernetes.io/hostname11.2 NodeLocal DNSCache 优化
合理配置缓存
cache {
success 9984 30
denial 9984 5
prefetch 10 10m 25%
}强制 TCP 与上游通信
forward . 10.96.0.10 {
force_tcp
}避免 UDP 丢包和 DNS 放大攻击。
监控缓存命中率
bash
# NodeLocal DNSCache 指标
curl http://<node-local-dns-pod>:9253/metrics | grep coredns_cache11.3 减少 ndots 影响
对于频繁访问外部域名的应用,可以在 Pod 中设置 dnsConfig:
yaml
spec:
dnsConfig:
options:
- name: ndots
value: "2"或者使用完整 FQDN 访问内部服务。
11.4 生产最佳实践
| 建议 | 说明 |
|---|---|
| 大规模集群启用 NodeLocal DNSCache | 显著降低 CoreDNS 压力 |
| 配置合理的缓存 TTL | 平衡实时性和性能 |
| 监控 CoreDNS 延迟和错误率 | 及时发现 DNS 问题 |
| 配置反亲和性 | 避免 CoreDNS Pod 集中在同一节点 |
| 使用 TCP 与上游通信 | 提高稳定性和安全性 |
| 调整 Pod DNS 策略 | 根据需求设置 dnsPolicy 和 dnsConfig |
第12章 故障排查实战
12.1 基础排查命令
bash
# 查看 CoreDNS Pod 状态
kubectl get pods -n kube-system -l k8s-app=kube-dns
# 查看 CoreDNS 日志
kubectl logs -n kube-system -l k8s-app=kube-dns --tail=100
# 查看 CoreDNS ConfigMap
kubectl get configmap coredns -n kube-system -o yaml
# 查看 kube-dns Service
kubectl get svc kube-dns -n kube-system
# 从 Pod 内测试 DNS 解析
kubectl run -it --rm debug --image=busybox --restart=Never -- nslookup kubernetes.default12.2 NodeLocal DNSCache 排查
bash
# 查看 DaemonSet
kubectl get daemonset node-local-dns -n kube-system
# 查看 NodeLocal DNSCache Pod
kubectl get pods -n kube-system -l k8s-app=node-local-dns -o wide
# 查看 NodeLocal DNSCache 日志
kubectl logs -n kube-system -l k8s-app=node-local-dns --tail=100
# 查看节点上的 iptables 规则
iptables -t nat -L PREROUTING -n -v | grep 10.96.0.10
iptables -t raw -L PREROUTING -n -v | grep 10.96.0.10
# 在节点上测试本地缓存
dig @169.254.20.10 kubernetes.default.svc.cluster.local12.3 常见故障与解决
| 故障 | 原因 | 解决 |
|---|---|---|
| DNS 解析超时 | CoreDNS Pod 负载高或网络问题 | 增加副本、启用 NodeLocal DNSCache |
| 只能解析内部域名 | forward 插件配置错误 | 检查上游 DNS 配置 |
| 只能解析外部域名 | kubernetes 插件异常 | 检查 API Server 连接和 RBAC |
| DNS 解析慢 | ndots 放大 + 无缓存 | 启用 NodeLocal DNSCache、调整 ndots |
| conntrack 满 | 大量 DNS 短连接 | 启用 NodeLocal DNSCache NOTRACK |
| NodeLocal 缓存未命中 | 缓存 TTL 太短 | 调整缓存配置 |
| Pod 无法解析任何域名 | resolv.conf 错误 | 检查 kubelet 和 Pod dnsConfig |
12.4 高级排查:抓包
bash
# 抓取 Pod 发出的 DNS 请求
tcpdump -i any -nn port 53
# 抓取到 CoreDNS 的流量
tcpdump -i any -nn host <coredns-pod-ip> and port 53
# 抓取到 NodeLocal DNSCache 的流量
tcpdump -i any -nn host 169.254.20.10 and port 5312.5 性能指标监控
CoreDNS 暴露的关键 Prometheus 指标:
coredns_dns_requests_total # DNS 请求总数
coredns_dns_responses_total # DNS 响应总数
coredns_forward_requests_total # 转发请求数
coredns_cache_entries # 缓存条目数
coredns_cache_hits_total # 缓存命中数
coredns_cache_misses_total # 缓存未命中数
coredns_dns_request_duration_seconds # 请求延迟第13章 高可用与扩展
13.1 CoreDNS 高可用
- Deployment 多副本
- Pod 反亲和性分散到不同节点
- HPA 根据负载自动扩容
- 监控和告警
13.2 NodeLocal DNSCache 高可用
- DaemonSet 每个节点一个 Pod
- hostNetwork 运行,不依赖 CNI
- 即使 CoreDNS 短暂不可用,缓存仍可服务
13.3 CoreDNS HPA 配置
yaml
apiVersion: autoscaling/v2
kind: HorizontalPodAutoscaler
metadata:
name: coredns
namespace: kube-system
spec:
scaleTargetRef:
apiVersion: apps/v1
kind: Deployment
name: coredns
minReplicas: 2
maxReplicas: 10
metrics:
- type: Resource
resource:
name: cpu
target:
type: Utilization
averageUtilization: 70第14章 附录速查表
14.1 Corefile 模板
.: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
}
cache 30
loop
reload
loadbalance
}14.2 常用命令速查
bash
# CoreDNS
kubectl get pods -n kube-system -l k8s-app=kube-dns
kubectl logs -n kube-system -l k8s-app=kube-dns
kubectl get configmap coredns -n kube-system -o yaml
# NodeLocal DNSCache
kubectl get daemonset node-local-dns -n kube-system
kubectl logs -n kube-system -l k8s-app=node-local-dns
# DNS 测试
nslookup kubernetes.default
kubectl run -it --rm debug --image=busybox --restart=Never -- nslookup kubernetes.default
# iptables 相关
iptables -t nat -L PREROUTING -n -v | grep 10.96.0.10
iptables -t raw -L PREROUTING -n -v | grep 10.96.0.10
# 抓包
tcpdump -i any -nn port 5314.3 推荐学习路径
- 理解 Kubernetes DNS 基本概念
- 掌握 CoreDNS 架构和 Corefile 配置
- 学习 CoreDNS 插件系统
- 理解 DNS 解析全链路
- 掌握 NodeLocal DNSCache 原理和部署
- 学会 iptables 与 NodeLocal DNSCache 的协作
- 掌握故障排查和性能优化
结语
CoreDNS 和 NodeLocal DNSCache 是 Kubernetes 集群 DNS 服务的核心组件。理解它们的工作原理、配置方法和优化手段,对于保障集群内服务发现和外部域名解析的稳定性至关重要。希望本教材能帮助你在生产环境中更好地管理和排障 Kubernetes DNS。
教材生成时间:2026-07-18