一、CoreDNS基本概念
1. CoreDNS概述
CoreDNS 是 CNCF(云原生计算基金会)毕业的开源 DNS 服务器,由 Miek Gieben 主导开发,基于 Go 语言实现,是云原生时代 kube-dns 的替代方案,同时也可作为通用 DNS 服务独立部署。其核心设计理念是「插件化、极简配置、云原生原生适配」,既兼容传统 DNS 场景,又能完美适配容器、K8s 等动态环境。
- 独立运行:作为独立守护进程常驻内存,监听 53 端口(DNS 默认端口),无需依附任何其他服务;
- 插件化扩展:通过插件实现健康检查、域名重写、缓存、转发等功能,配置极简;
- 多场景适配:既支持传统内网域名的静态解析,也支持云原生环境的动态服务发现;
- 热重载:修改配置后无需重启进程,通过信号量即可重载配置,无服务中断。
2. 与传统的DNS软件对比
| 特性 | CoreDNS | Bind | Unbound |
|---|---|---|---|
| 架构设计 | 插件化、单进程、轻量 | 模块化、多进程、重量级 | 单进程、专注递归、轻量 |
| 云原生适配 | 原生支持 K8s/ETCD | 无原生支持 | 无原生支持 |
| 健康检查 | 内置health插件 | 需第三方脚本 | 需第三方工具联动 |
| 配置复杂度 | Corefile 声明式 | 多配置文件 | 专注递归 |
| 动态能力 | 插件化动态解析(如 K8s) | 静态配置为主 | 静态 / 转发为主 |
| 热重载 | 原生支持(SIGUSR1 信号 | 支持但配置复杂 | 支持(unbound-control) |
| 资源占用 | 极低(MB 级内存) | 较高(GB 级内存) | 低(MB 级) |
| 监控能力 | 内置prometheus插件 | 需额外配置 | 需额外配置 |
3. CoreDNS核心架构模型
CoreDNS的核心是:「插件链+Corefile配置」。所有功能均通过插件实现,配置文件定义插件的执行顺序和规则。
- 插件链执行逻辑:请求按Corefile中插件的声明顺序依次执行,插件可决定终止请求(返回结果)或传递给下一个插件。
- 插件优先级:核心基础插件(如 log/errors)通常放在最前,业务插件(如 hosts/kubernetes)居中,兜底插件(如 forward/rewrite)放在最后。
4. Corefile语法解析
Corefile是CoreDNS的唯一配置文件,采用【域+插件块】的声明式语法,格式极简。
<域名>[:端口] { <插件1>[插件参数] <插件1>[插件参数] ... }极简配置案例:所有域名监听 53 端口
.:53 { bind 0.0.0.0 # 绑定所有网卡 log # 记录查询日志 hosts { # 静态解析 192.168.1.1 www.meaauf.com } forward . 8.8.8.8 # 未匹配的域名转发到谷歌 DNS cache 60 # 缓存 60 秒 }5. 基础类插件介绍
| 插件类 | 核心功能 | 示例 |
|---|---|---|
| bind | 指定监听的网卡 / 端口 | bind 192.168.8.10 |
| log | 记录所有DNS查询日志,如客户端IP、域名、查询类型、响应状态等 | log【无需参数】 |
| errors | 记录解析过程中的错误日志,如域名不存在、后端服务异常 | errors【无需参数】 |
| cache | 缓存解析结果,减少重复查询,提升性能 | cache 60【缓存60秒】 |
| reload | 支持配置热重载,修改Corefile后自动重载,无需重启进程 | reload【无需参数】 |
6. 解析类插件介绍
| 插件类 | 核心功能 | 示例 |
|---|---|---|
| hosts | 静态解析域名 | hosts { 192.168.1.1 www.meaauf.com;} |
| forward | 将域名查询转发到上游DNS服务器 | forward 114.114.114.114 |
| kubernetes | K8s 集群内域名解析 | kubernets cluster.loacal in-addr.arpa ip6.arpa { pods verified; fallthrough} |
| file | 从Zone文件(Bind 格式)读取解析记录,兼容传统DNS配置 | file /etc/coredns/meaauf.zone |
二、CoreDNS部署
1. 二进制方式部署
[Step1]访问CoreDNS的官网或GitHub,下载CoreDNS二进制包。
官网:https://coredns.io GitHub:https://github.com/coredns/coredns/releases[Step2]将软件包上传到CoreDNS。
[Step3]将coredns解压缩,解压出来的是一个二进制命令。
[root@CoreDNS ~]# tar zxf coredns_1.14.1_linux_amd64.tgz[Step4]确保coredns命令具备执行权限,将其移动到“/usr/local/bin”目录。
[root@CoreDNS ~]# chmod +x coredns [root@CoreDNS ~]# mv coredns /usr/local/bin/[Step5]验证:输出coredns的版本号。
[root@CoreDNS ~]# coredns -version2. Docker方式部署
[Step1]确保本地Docker服务状态正常。
[root@CoreDNS ~]# systemctl status docker.service[Step2]拉取官方镜像。
[root@CoreDNS ~]# docker pull coredns/coredns:latest[Step3]临时启动一个 CoreDNS 容器,查看其版本信息,并且容器运行结束后自动删除。
[root@CoreDNS ~]# docker run --rm coredns/coredns -version三、传统内网静态解析案例配置
1. 节点设置
| 主机名 | 网络参数 | 系统版本 | 说明 |
|---|---|---|---|
| CoreDNS | 192.168.8.100/24 | Kylin Linux Advanced Server V11 (Swan25) | DNS服务器 |
| Client | 192.168.8.200/24 | Kylin Linux Advanced Server V11 (Swan25) | 测试客户端 |
2. CoreDNS服务器配置
[Step1]安装coredns,以下使用二进制安装方式。
[root@CoreDNS ~]# coredns -version[Step2]创建配置文件目录,编写Corefile。
[root@CoreDNS ~]# mkdir /etc/coredns [root@CoreDNS ~]# vim /etc/coredns/Corefile # 写入下列内容 .:53 { bind 192.168.8.100 # 仅监听指定网卡,增强安全性 log # 记录所有 DNS 查询日志 errors # 记录错误日志 reload # 支持配置热重载 # 静态解析域名 hosts { 192.168.8.100 coredns.meaauf.com 192.168.8.101 www.meaauf.com 192.168.8.200 client.meaauf.com } cache 60 # 缓存优化 }[Step3]启动coredns,同时观察回显日志。
[root@CoreDNS ~]# coredns -conf /etc/coredns/Corefile3. 客户端验证
[Step1]验证:使用nslookup测试正向解析,该命令需要先安装bind-utils。
[root@client ~]# dnf install -y bind-utils [root@client ~]# nslookup coredns.meaauf.com [root@client ~]# nslookup www.meaauf.com [root@client ~]# nslookup client.meaauf.com[Step2]验证:使用nslookup测试反向解析。
[root@client ~]# nslookup 192.168.8.100 [root@client ~]# nslookup 192.168.8.101 [root@client ~]# nslookup 192.168.8.200[Step3]验证:在CoreDNS上查看日志信息,能够看到刚刚的解析记录。
4. 系统服务部署
[Step1]在CoreDNS服务器上创建CoreDNS systemd服务文件。
[root@CoreDNS ~]# vim /etc/systemd/system/coredns.service # 写入下列内容 [Unit] Description=CoreDNS DNS Server Documentation=https://coredns.io After=network.target network-online.target Wants=network-online.target [Service] User=root Group=root ExecStart=/usr/local/bin/coredns -conf /etc/coredns/Corefile Restart=on-failure RestartSec=5s StartLimitInterval=0 StandardOutput=journal StandardError=journal SyslogIdentifier=coredns Nice=-5 [Install] WantedBy=multi-user.target[Step2]在CoreDNS服务器重新加载systemd配置,启动CoreDNS服务并设置开机自启。
[root@CoreDNS ~]# systemctl daemon-reload [root@CoreDNS ~]# systemctl enable --now coredns.service四、搭配file模块实现静态解析
1. 节点设置
| 主机名 | 网络参数 | 系统版本 | 说明 |
|---|---|---|---|
| CoreDNS | 192.168.8.100/24 | Kylin Linux Advanced Server V11 (Swan25) | DNS服务器 |
| Client | 192.168.8.200/24 | Kylin Linux Advanced Server V11 (Swan25) | 测试客户端 |
2. CoreDNS服务器配置
[Step1]安装coredns,以下使用二进制安装方式。
[Step2]创建配置文件目录,编写Corefile。
[root@CoreDNS ~]# mkdir /etc/coredns [root@CoreDNS ~]# vim /etc/coredns/Corefile # 写入下列内容 .:53 { bind 192.168.8.100 # 仅监听指定网卡,增强安全性 log # 记录所有 DNS 查询日志 errors # 记录错误日志 reload # 支持配置热重载 # 使用file插件加载解析规则,替代hosts file /etc/coredns/zones/meaauf.db cache 60 # 缓存优化 }[Step3]编写“meaauf.db”文件,类似于Bind的解析文件。
[root@CoreDNS ~]# mkdir /etc/coredns/zones [root@CoreDNS ~]# vim /etc/coredns/zones/meaauf.db # 写入下列内容 $TTL 1D @ IN SOA @ rname.invalid. ( 2026012701 ; serial 1D ; refresh 1H ; retry 1W ; expire 3H ) ; minimum coredns.meaauf.com. IN A 192.168.8.100 www.meaauf.com. IN A 192.168.8.101 client.meaauf.com. IN A 192.168.8.200[Step4]编写systemd服务文件,编写systemd服务文件的过程省略。启动coredns服务。
[root@CoreDNS ~]# systemctl restart coredns.service3. 客户端验证
[Step1]验证:使用nslookup测试正向解析,该命令需要先安装bind-utils。
[root@client ~]# dnf install -y bind-utils [root@client ~]# nslookup coredns.meaauf.com [root@client ~]# nslookup www.meaauf.com [root@client ~]# nslookup client.meaauf.com