使用CoreDNS作为内网域名解析服务器

CoreDNS 是Kubernetes默认也是最常用的DNS工具。
内网环境下我们经常会使用一些特殊结尾的域名来却分访问环境,这些域名往往是不存在或者我们自己不是持有人,最常见的使用方案是我们在内网DNS上做解析或者公网DNS解析成内网地址。

下面的流程介绍了怎么使用MasqDNS + CoreDNS做 内网和集群内的域名解析。

一、 CoreDNS作为自定义域名的解析服务器

1.CoreDNS暴漏服务地址

默认情况下coredns只能集群内访问,如果向外部访问方法也很多。修改svc类型为LoadBalaner NodePort,也可以增加路由方式使外部机器能访问集群svc CIDR。这里需要注意 coredns的svc名称是 kube-dns。

本例中使用了Metallb svc LoadBanlanerIP手动设置为 172.18.0.53。

2.增加解析配置

编写 example.io的配置,需要重点注意SOA记录和NS记录,NS记录值为coredns的Service 地址。

dns解析 example.io.db
example.io.               IN      SOA	   sns.dns.icann.org. noc.dns.icann.org. 2015082541 7200 3600 1209600 360
example.io. IN NS 172.18.0.53.
captain.example.io IN A 172.18.0.12
capsule.example.io. IN A 172.18.0.8
consul.example.io. IN A 172.18.0.85
kafka.example.io. IN A 172.18.90.92
monitoring.example.io. IN A 172.18.0.13
istio.example.io. 600 IN A 172.18.0.19

3.修改CoreDNS配置

编辑coredns配置
kubectl edit cm -n kube-system coredns
修改后coredns配置如下
kind: ConfigMap
apiVersion: v1
metadata:
name: coredns
namespace: kube-system
data:
Corefile: |
.:53 {
errors
....
file /etc/coredns/example.io.db example.io
}
example.io.db: |
example.io. IN SOA sns.dns.icann.org
example.io. IN NS 172.18.0.53.
....
....

4.CoreDNS 挂载新配置文件。

卷增加新的配置文件

kind: Deployment
apiVersion: apps/v1
metadata:
name: coredns
namespace: kube-system
...
spec:
...
template:
spec:
volumes:
- name: config-volume
configMap:
name: coredns
items:
- key: Corefile
path: Corefile
- key: example.io.db # 新增example.io 配置
path: example.io.db
defaultMode: 420
....

5. 重启CoreDNS

kubectl rollout restart deploy -n kube-system coredns

测试 dig a.example.io @172.18.0.53

二、 masqDNS配置域解析

masqdns指定域服务器

server=/example.io/172.18.0.53

重启masqdns后 example.io 就可以内网访问了。

参考

评论