DEBUG.WIKI
阿里云;从建站到云上业务;云服务器 · 数据库 · 对象存储;查看产品 →
PanstarCloud:部署应用,接入 AI,云服务器与 AI 网关
IPPeak:从目标地区访问网页,住宅代理、SEO 监测、广告验证

解决 CoreDNS 报错 plugin/hosts failed to open hosts file

分析 CoreDNS hosts 插件报 failed to open hosts file 根因,详解 ConfigMap 挂载路径、文件权限与内联 hosts 语法优化。

问题现象

CoreDNS 容器启动或重载时报错并拒绝解析静态域名:

[ERROR] plugin/hosts: open /etc/coredns/custom-hosts: no such file or directory
[WARNING] plugin/reload: Corefile modified but failed to reload

触发根因

  1. 在 Corefile 中配置了外部文件路径 hosts /etc/coredns/custom-hosts,但 Kubernetes Deployment 中未挂载对应的 Volume。

诊断排查步骤

  1. 检查 Corefile 中的 hosts 声明指令: 查看是否指定了绝对文件路径。

解决步骤与操作命令

步骤一:改用内联(Inline)hosts 配置块(无需额外挂载文件)

在 CoreDNS ConfigMap 中直接内联定义域名映射,彻底消除文件挂载丢失隐患:

hosts {
    192.168.1.10 gitlab.internal
    192.168.1.11 harbor.internal
    fallthrough
}

步骤二:或者在 Deployment 中补充对应 ConfigMap 挂载

volumeMounts:
- name: custom-hosts
  mountPath: /etc/coredns/custom-hosts
  subPath: custom-hosts

验证方法

更新 ConfigMap 并检查 CoreDNS 日志:

kubectl logs -n kube-system -l k8s-app=kube-dns --tail=50

输出 Corefile successfully reloaded,静态域名解析恢复正常。