00:00:00
容器解析k8s域名失败
一、问题描述
1. node-exporter Pod 持续 Crash
[root@node-4302 ~]# kubectl get pod -n openstack -o wide | grep node-ex
node-exporter-q789x 2/3 CrashLoopBackOff 23 145m 10.43.1.16 node-4313 <none> <none>
node-exporter-w6t8n 3/3 Running 3 15d 10.43.1.11 node-4308 <none> <none>
node-exporter-wbmt5 2/3 CrashLoopBackOff 23 146m 10.43.1.17 node-4314 <none> <none>2. 容器日志出现解析 k8s 域名失败,且容器的 nameserver 未配置为 CoreDNS Service 的 ClusterIP
# 容器日志出现解析k8s域名失败
[errno 22] error connecting to the cluster
Unable to parse addr in 'ceph-mon.ceph.svc.cluster.local'
2026-07-31 13:58:14.065 ffff9f3b6200 -1 mconfig: get_monmap_and_config cannot identify monitors to contact
[errno 22] error connecting to the cluster
Unable to parse addr in 'ceph-mon.ceph.svc.cluster.local' 容器日志出现解析k8s域名失败
2026-07-31 13:58:34.281 ffff8a052200 -1 mconfig: get_monmap_and_config cannot identify monitors to contact
[errno 22] error connecting to the cluster
# 容器内部 resolv.conf 中的 nameserver 没有配置 CoreDNS service 地址
[root@node-4302 ~]# kubectl exec -it -n openstack node-exporter-q789x -c smartmon-collector -- bash
[root@node-4313 /]# cat /etc/resolv.conf
nameserver 8.8.8.8
options single-request-reopen
# 节点 resolv.conf 中的 nameserver 配置了 CoreDNS service 地址
[root@node-4313 ~]# cat /etc/resolv.conf
# Generated by NetworkManager
nameserver 10.222.0.3
nameserver 8.8.8.8
# CoreDNS service 地址
[root@node-4313 ~]# kubectl get svc -n kube-system | grep dns
coreDNS ClusterIP 10.222.0.3 <none> 53/UDP,53/TCP,9153/TCP3. 节点上 node-exporter Pod 的启动时间比 /etc/resolv.conf 更新时间早 41 秒
# node-exporter pod的创建时间为 Fri, 31 Jul 2026 11:23:04 +0800
[root@node-4313 etc]# kubectl describe pod -n openstack node-exporter-q789x
Name: node-exporter-q789x
Namespace: openstack
Priority: 0
Service Account: ecms-node-exporter
Node: node-4313/10.43.1.16
Start Time: Fri, 31 Jul 2026 11:23:04 +0800
Labels: application=node_exporter
component=metrics
controller-revision-hash=7dcd78b856
pod-template-generation=1
release_group=ecms
# /etc/resolv.conf文件的创建时间为 Fri, 31 Jul 2026 11:23:45 +0800
[root@node-4313 etc]# ls -l --full-time resolv.conf
-rw-r--r-- 1 root 71 2026-07-31 11:23:45.780137607 +0800 resolv.conf二、问题分析
1. 查看 node-exporter DaemonSet 的 hostNetwork 及 dnsPolicy 配置
# 开启了 hostNetwork, 但 dnsPolicy 未显式配置。即默认为 dnsPolicy: ClusterFirst
hostNetwork: true2. 根因分析
hostNetwork: true与dnsPolicy: ClusterFirst搭配使用容易导致 DNS 解析异常。- 当 Pod 配置了
hostNetwork: true且未显式配置dnsPolicy(或显式配置为ClusterFirst)时,Pod 会继承节点/etc/resolv.conf在 sandbox 创建时的快照,而非由 kubelet 根据--cluster-dns配置生成。 - 本案例中,Pod 创建完成时,节点
/etc/resolv.conf尚未更新为包含 CoreDNS Service ClusterIP 的版本,导致 Pod 的 nameserver 继承了不含 CoreDNS 地址的旧配置,从而无法解析 k8s 域名。
三、问题解决
1. 显式配置 node-exporter pod 的 dnsPolicy 为 ClusterFirstWithHostNet
hostNetwork: true
dnsPolicy: ClusterFirstWithHostNet四、问题总结
- 普通 Pod(
hostNetwork: false) 默认dnsPolicy会使用ClusterFirst,kubelet 会使用自身--cluster-dns配置来生成 Pod DNS。 - hostNetwork Pod 如果
dnsPolicy未显式配置ClusterFirstWithHostNet,即使默认或显式设置为ClusterFirst,kubelet 不会按照 Cluster DNS 方式生成 DNS 配置,而是直接使用节点 DNS 配置。 - hostNetwork Pod
dnsPolicy显式配置ClusterFirstWithHostNet后,即使容器与宿主机共享网络,kubelet 仍然会使用--cluster-dns生成 Pod DNS 配置。
