-
一、前言
从Kubernetes v1.12开始,CoreDNS是推荐的DNS服务器,取代了kube-dns。但是,默认情况下仍可以使用某些Kubernetes安装程序工具安装kube-dns。
CoreDNS部署作为具有静态IP的Kubernetes服务公开。CoreDNS和kube-dns服务都
在该kube-dns
字段中命名。这样做可以使依赖于旧metadata.name
服务名称的工作负载具有更强的互操作性,以解析集群内部的地址。它抽象出了DNS提供程序在该公共端点后面运行的实现细节。kubelet将DNS传递给带有kube-dns
--cluster-dns=<dns-service-ip>
标志的每个容器。
CoreDNS是一个通用的权威DNS服务器,可以作为集群DNS,符合DNS规范。
代码地址:https://github.com/coredns/coredns
-
二、架构原理
整个 CoreDNS 服务都建立在一个使用 Go 编写的 HTTP/2 Web 服务器 Caddy 上,CoreDNS 整个项目可以作为一个 Caddy 的教科书用法。
CoreDNS 的大多数功能都是由插件来实现的,插件和服务本身都使用了 Caddy 提供的一些功能,所以项目本身也不是特别的复杂。
作为基于 Caddy 的 Web 服务器,CoreDNS 实现了一个插件链的架构,将很多 DNS 相关的逻辑都抽象成了一层一层的插件,包括 Kubernetes 等功能。在这种灵活的模型中添加对Kubernetes的支持,相当于创建了一个Kubernetes中间件。该中间件使用Kubernetes API来满足针对特定Kubernetes pod或服务的DNS请求。
- errors: 错误消息
- health: coreDNS 的健康情况 http://localhost:8080/health.
- kubernetes: CoreDNS将根据Kubernetes服务和pod的IP回复DNS查询。
- prometheus: coreDNS 的监控 http://localhost:9153/metrics .
- forward: 任何不存在kubernets内的查询,都要转到forward预定义的解析器
- cache: 启用前端缓存.
- loop: 该环插件将随机探头查询发送给自己,然后将跟踪的多少次,我们看到它。如果我们看到它超过两次,我们假设CoreDNS已经看到转发循环,我们停止该过程
- reload: 此插件允许自动重新加载已更改的Corefile,要启用区域文件更改的自动重新加载,请使用该
插件auto
- loadbalance: 一个负载均衡的插件
-
三、Corefile
另一个 CoreDNS 的特点就是它能够通过简单易懂的 DSL 定义 DNS 服务,在 Corefile 中就可以组合多个插件对外提供服务:
对于以上的配置文件,CoreDNS 会根据每一个代码块前面的区和端点对外暴露两个端点提供服务: 该配置文件对外暴露了两个 DNS 服务,其中一个监听在 5300 端口,另一个在 53 端口,请求这两个服务时会根据不同的域名选择不同区中的插件进行处理。coredns.io:
5300
{
file db.coredns.io
}
example.io:
53
{
log
errors
file db.example.io
}
example.net:
53
{
file db.example.net
}
.:
53
{
kubernetes
proxy .
8.8
.
8.8
log
errors
cache
}
- 四、部署配置yaml
apiVersion:
v1
kind:
ConfigMap
metadata:
name:
coredns
namespace:
kube-system
data:
Corefile:
|
.:
53
{
errors
log stdout
health
kubernetes CLUSTER_DOMAIN SERVICE_CIDR
proxy . /etc/resolv.conf
cache
30
}
---
apiVersion:
extensions/v1beta1
kind:
Deployment
metadata:
name:
coredns
namespace:
kube-system
labels:
k8s-app:
coredns
kubernetes.io/cluster-service
:
"true"
kubernetes.io/name
:
"CoreDNS"
spec:
replicas:
1
selector:
matchLabels:
k8s-app:
coredns
template:
metadata:
labels:
k8s-app:
coredns
annotations:
scheduler.alpha.kubernetes.io/critical-pod
:
''
scheduler.alpha.kubernetes.io/tolerations
:
'[{"key":"CriticalAddonsOnly", "operator":"Exists"}]'
spec:
containers:
-
name
:
coredns
image:
coredns/coredns
:
latest
imagePullPolicy:
Always
args:
[
"-conf"
,
"/etc/coredns/Corefile"
]
volumeMounts:
-
name
:
config-volume
mountPath:
/etc/coredns
ports:
-
containerPort
:
53
name:
dns
protocol:
UDP
-
containerPort
:
53
name:
dns-tcp
protocol:
TCP
livenessProbe:
httpGet:
path:
/health
port:
8080
scheme:
HTTP
initialDelaySeconds:
60
timeoutSeconds:
5
successThreshold:
1
failureThreshold:
5
dnsPolicy:
Default
volumes:
-
name
:
config-volume
configMap:
name:
coredns
items:
-
key
:
Corefile
path:
Corefile
---
apiVersion:
v1
kind:
Service
metadata:
name:
kube-dns
namespace:
kube-system
labels:
k8s-app:
coredns
kubernetes.io/cluster-service
:
"true"
kubernetes.io/name
:
"CoreDNS"
spec:
selector:
k8s-app:
coredns
clusterIP:
CLUSTER_DNS_IP
ports:
#coreDNS 支持TCP、UDP、HTTP、TTL四种协议
-
name
:
dns
port:
53
protocol:
UDP
-
name
:
dns-tcp
port:
53
protocol:
TCP