Kubernetes DNS 记录

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Kubernetes DNS 记录相关的知识,希望对你有一定的参考价值。

参考技术A

Kubernetes DNS-Based Service Discovery · GitHub

DNS for Services and Pods | Kubernetes

Kubernetes DNS 在 cluster 内部署 DNS Pod 和 Service ,kubelet会告知容器DNS服务IP,来解析DNS names。

Cluster内的每个Service (包括DNS server)会被分配一个 DNS name。默认,客户端Pod的serch list包含该Pod的namespace和cluster的默认domain。

DNS查询会根据不同的namespace返回不同的结果。DNS查询不指定namespace,那么就使用该pod的namespace;指定namesapce,才可以跨namespace查询。

可以通过Pod的 /etc/resolv.conf 文件扩展查询。该文件由Kubelet设置。比如,仅查询 data 将被扩展为 data.test.cluster.local 。 search 参数也可以用来扩展查询。更多DNS查询参见 the resolv.conf manual page.

总之,在 test namespace 内的pod,可以成功的解析 data.prod or data.prod.svc.cluster.local 。

哪些对象拥有 DNS records?

如下章节详细描述了支持的DNS record 的类型和分层。The following sections detail the supported DNS record types and layout that is supported. Any other layout or names or queries that happen to work are considered implementation details and are subject to change without warning. 最新的设置文档参见 Kubernetes DNS-Based Service Discovery

"Normal" ( not headless ) Services 指派 DNS A or AAAA record,name 格式 my-svc.my-namespace.svc.cluster-domain.example 。这备解析为该Service的cluster IP。

**"Headless" **( without a cluster IP ) Services 也备指派 DNS A or AAAA record,name 格式 my-svc.my-namespace.svc.cluster-domain.example 。不同于 normal Services,其解析为select的一组Pods的IP。Clients 被期望使用标准round-robin的方式消费这组集合。

SRV Records are created for named ports that are part of normal or Headless Services . For each named port, the SRV record would have the form _my-port-name._my-port-protocol.my-svc.my-namespace.svc.cluster-domain.example . For a regular service, this resolves to the port number and the domain name: my-svc.my-namespace.svc.cluster-domain.example . For a headless service, this resolves to multiple answers, one for each pod that is backing the service, and contains the port number and the domain name of the pod of the form auto-generated-name.my-svc.my-namespace.svc.cluster-domain.example

有时,我们不需要负载均衡,也不需要配置Service IP。这种情况,我们可以创建"headless" Services,通过给cluster IP (.spec.clusterIP)显示指定"None"。

你可以使用headless Service来跟其他服务发现机制交互,不需要绑定到Kubernetes方案。

Headless Services,没有分配 cluster IP ,kube-proxy 不负责这些 Services, 平台上没有负载均衡或代理处理。DNS的自动配置,依赖于Service是否定义了selectors:

定义了selectors的headless Services,endpoints controller 在API 创建 Endpoints 记录,配置DNS返回A记录 (IP addresses) 直接指向 Service 后的 Pods 。

没有定义selectors的headless Services,endpoints controller 不创建 Endpoints 记录。DNS系统通过如下查找、配置:

pod 在 DNS 的name通常如下:In general a pod has the following DNS resolution:
pod-ip-address.my-namespace.pod.cluster-domain.example .

比如,在 default namespace的某个Pod,IP地址 172.17.0.3,cluster 的 domain name 是 cluster.local ,那么该Pod的DNS name是:
172-17-0-3.default.pod.cluster.local .

Deployment 创建的 pods,或Service 发布的 DaemonSet ,DNS解析如下:
pod-ip-address.deployment-name.my-namespace.svc.cluster-domain.example

Pod创建好后,它的hostname就被设置为Pod的 metadata.name 值。

Pod 的 spec 有一个可选项 hostname ,用于指定 Pod 的 hostname。指派后,优先与Pod 的 name 作为 pod 的 hostname。比如,配置Pod的 hostname 为 " my-host ",该 Pod 的hostname 将被设置为 " my-host "。

Pod spec 还有个可选项 subdomain ,用于指定subdomain。比如,某 Pod 设置 hostname 为 " foo ", subdomain 为" bar ",namespace " my-namespace ",那么完全域名(FQDN) 就是 foo.bar.my-namespace.svc.cluster-domain.example 。

如果在同一 namespace 内,存在一个 headless service ,与某pod的subdomain名相同,集群的DNS仍然返回Pod FQDN 的 A or AAAA record。
比如,Pod 的 hostname 是 "busybox-1" , subdomain 是 "default-subdomain",同时名为 "default-subdomain" 的 headless Service 在同一个namespace中,该 pod 将识别它自己的FQDN 为 busybox-1.default-subdomain.my-namespace.svc.cluster-domain.example 。DNS 为该name提供 A or AAAA record ,指向该 Pod 的 IP。Pod "busybox1" 和 "busybox2" 都可以拥有唯一的 A or AAAA records。

Endpoints 对象指派 hostname 以 endpoint addresses,以及它的 IP。

FEATURE STATE: Kubernetes v1.20 [beta]

当Pod被配置为可以拥有FQDN,它的 hostname就成了缩写的hostname。比如,如果某Pod的FQDN是 busybox-1.default-subdomain.my-namespace.svc.cluster-domain.example ,那么该Pod默认的hostname就是 busybox-1 并且 hostname --fqdn 命令返回FQDN。

当你在Pod spec 设置 setHostnameAsFQDN: true ,kubelet 就会将该 Pod\'s FQDN 写入到该Pod namespace的into the hostname for that Pod\'s namespace. 这样,hostname 和 hostname --fqdn 都将返回 Pod 的 FQDN。

可以给每个pod设置DNS policies。Kubernetes 现在支持如下pod-specific DNS policies。这些 policies 设置在Pod Spec的 dnsPolicy 配置项。

Pod 的 DNS Config 提供了更多DNS控制手段。

dnsConfig 可选配置项可以与任意 dnsPolicy 搭配使用。但是,如果 dnsPolicy 设置为 " None ",那么 dnsConfig 配置项必须被指定。

如下是 dnsConfig 配置项的可定义属性:

如何列出所有 kubernetes DNS 记录?

【中文标题】如何列出所有 kubernetes DNS 记录?【英文标题】:how can I list all kubernetes DNS records? 【发布时间】:2019-11-04 20:23:40 【问题描述】:

我在(裸机)集群中运行 kube-dns。我知道它可以工作,因为我可以interpolate a service to a name 我已经创建并获得了一个主机条目:

$ host elk-service-headless.default.svc.cluster.local
elk-service-headless.default.svc.cluster.local has address 10.42.0.151
elk-service-headless.default.svc.cluster.local has address 10.42.0.152
elk-service-headless.default.svc.cluster.local has address 10.42.0.153
(...)

我不知道该怎么做是列出 kube-dns 持有的所有记录。我已经尝试过 standard DNS tricks 之类的 dig 和 host -l 并且无法获得它们。但无论如何,Kubernetes 本身必须有办法做到这一点。我尝试检查 ConfigMap,但没有找到我要查找的内容。

【问题讨论】:

【参考方案1】:

如果你使用kube-dns,它使用dnsmaq来缓存 DNS记录,可以通过this answer转储记录。

如果您使用coredns,它嵌入了一个cache 插件来缓存DNS 记录,我找不到在这个缓存插件中获取数据的方法。但是我发现 coredns 可以使用 etcd 作为后端,所以 DNS 记录可以缓存在 etcd 中,但这需要用这个 Corefile 重新配置你的 coredns:

.:53 
    etcd 
        path /skydns
        endpoint <etcd_endpoint>
        upstream /etc/resolv.conf
    
    ...

【讨论】:

【参考方案2】:

这篇文章将帮助您在运行 kube-dns 的集群上找到您的 K8s 服务的内部 DNS 记录:

    找到kube-dns服务的ClusterIP:

kubectl -n kube-system get svc kube-dns

现在我们知道 K8s 内部的 DNS 解析器 IP 是 172.20.0.10

    找到应用服务端点IP:

kubectl -n fe get ep

    执行到应用程序 pod:

kubectl -n fe exec -it fe-app-575fdf6cb6-lt7t6 -- sh

    获取 DNS 服务名称:

【讨论】:

以上是关于Kubernetes DNS 记录的主要内容,如果未能解决你的问题,请参考以下文章

如何列出所有 kubernetes DNS 记录?

09-kubernetes中的域名解析流程

基于kubernetes实现coredns的及验证

如何避免阻止托管在 Kubernetes 上的网站

k8s-dns

Kubernetes addons 之 coredns部署