CoreDNS 轻量 DNS 服务器搭建教程
CoreDNS 是一款用 Go 语言编写的现代化 DNS 服务器,也是 Kubernetes 默认的 DNS 组件。它最大的特点是采用插件化架构,所有功能都通过插件实现,你可以根据需要灵活组合,构建从简单的 DNS 缓存到复杂的服务发现系统。CoreDNS 单一二进制文件部署,资源占用极低,非常适合在搬瓦工 VPS 上运行。
一、CoreDNS 特点
- 插件化架构:每个功能都是一个插件,可灵活组合。
- 单文件部署:一个二进制文件即可运行,无需额外依赖。
- 现代协议:原生支持 DNS-over-TLS(DoT)和 DNS-over-HTTPS(DoH)。
- Prometheus 集成:内置指标暴露接口,方便监控。
- 低资源占用:内存占用通常不到 30MB。
二、安装 CoreDNS
2.1 下载二进制文件
cd /tmp
wget https://github.com/coredns/coredns/releases/latest/download/coredns_1.11.3_linux_amd64.tgz
tar -xzf coredns_1.11.3_linux_amd64.tgz
mv coredns /usr/local/bin/
chmod +x /usr/local/bin/coredns
2.2 创建运行用户和目录
useradd --system --no-create-home --shell /usr/sbin/nologin coredns
mkdir -p /etc/coredns
三、基础配置
CoreDNS 使用 Corefile 作为配置文件,语法类似于 Caddy Web 服务器:
3.1 DNS 转发与缓存
cat > /etc/coredns/Corefile <<EOF
.:53 {
forward . 8.8.8.8 8.8.4.4 {
prefer_udp
health_check 5s
}
cache 3600
log
errors
prometheus :9153
}
EOF
这是最基础的配置,将所有 DNS 查询转发到 Google DNS 并缓存结果。
3.2 自定义域名解析
cat > /etc/coredns/Corefile <<EOF
example.com {
file /etc/coredns/zones/example.com
log
errors
}
.:53 {
forward . 8.8.8.8 8.8.4.4
cache 3600
log
errors
prometheus :9153
}
EOF
3.3 创建区域文件
mkdir -p /etc/coredns/zones
cat > /etc/coredns/zones/example.com <<EOF
\$ORIGIN example.com.
\$TTL 3600
@ IN SOA ns1.example.com. admin.example.com. (
2024010101 ; Serial
3600 ; Refresh
900 ; Retry
86400 ; Expire
3600 ) ; Minimum TTL
@ IN NS ns1.example.com.
@ IN A 你的VPS公网IP
ns1 IN A 你的VPS公网IP
www IN A 你的VPS公网IP
api IN A 你的VPS公网IP
EOF
四、使用 hosts 插件
对于简单的解析需求,可以直接使用 hosts 插件代替区域文件:
.:53 {
hosts /etc/coredns/custom-hosts {
192.168.1.10 server1.local
192.168.1.11 server2.local
192.168.1.12 db.local
fallthrough
}
forward . 8.8.8.8 8.8.4.4
cache 3600
log
}
五、DNS-over-TLS 配置
tls://.:853 {
tls /etc/coredns/certs/cert.pem /etc/coredns/certs/key.pem
forward . 8.8.8.8 8.8.4.4
cache 3600
log
}
六、创建 Systemd 服务
cat > /etc/systemd/system/coredns.service <<EOF
[Unit]
Description=CoreDNS DNS Server
After=network.target
[Service]
User=coredns
Group=coredns
ExecStart=/usr/local/bin/coredns -conf /etc/coredns/Corefile
Restart=always
RestartSec=5
LimitNOFILE=8192
AmbientCapabilities=CAP_NET_BIND_SERVICE
[Install]
WantedBy=multi-user.target
EOF
systemctl daemon-reload
systemctl enable --now coredns
七、防火墙与端口
ufw allow 53/tcp
ufw allow 53/udp
如果系统上已有 systemd-resolved 占用 53 端口,需要先禁用:
systemctl stop systemd-resolved
systemctl disable systemd-resolved
rm /etc/resolv.conf
echo "nameserver 127.0.0.1" > /etc/resolv.conf
八、测试验证
dig @127.0.0.1 example.com A
dig @127.0.0.1 www.example.com A
dig @你的VPS公网IP google.com A
九、Prometheus 监控
CoreDNS 内置 Prometheus 指标暴露,在 Corefile 中添加 prometheus :9153 后,访问 http://127.0.0.1:9153/metrics 即可获取指标数据,包括:
- DNS 查询总数和每秒查询率
- 各响应码的计数(NOERROR、NXDOMAIN、SERVFAIL 等)
- 查询响应延迟分布
- 缓存命中率
十、常见问题
端口被占用
ss -ulnp | grep :53
如果被 systemd-resolved 占用,按照第七节的方法禁用即可。
解析超时
检查上游 DNS 的连通性和 CoreDNS 日志:
journalctl -u coredns -f
总结
CoreDNS 是轻量级 DNS 服务的最佳选择,插件化架构使其在保持简洁的同时具备强大的扩展能力。在搬瓦工 VPS 上部署 CoreDNS 几乎不会消耗额外资源。如果需要更传统全面的 DNS 方案,可以参考 BIND9 教程;如果只需要本地 DNS 缓存,Dnsmasq 也是不错的选择。购买搬瓦工 VPS 请查看 全部方案,使用优惠码 NODESEEK2026 可享 6.77% 折扣,购买链接:bwh81.net。