coreDNS详解

2024-04-16 23:52
文章标签 详解 coredns

本文主要是介绍coreDNS详解,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

一、介绍

1、coreDNS的功能

coreDNS的作用主要是作为DNS服务器,在集群内提供服务发现功能,也就是服务之间的互相定位的过程。他监听集群中service和pod的创建和销毁事件,当serivice或者pod被创建时,记录对应的解析记录。当其他pod通过域名来访问集群中的service或pod时,会向coreDNS服务查询解析记录,然后访问解析到的IP地址。

2、coreDNS特点

由于coreDNS的高效率、资源占用率少,已经替换kube-dns,成为了kubernetes集群的默认DNS服务

二、部署yaml

查看并分析yaml配置文件

#################################################
#################################################
####deployment配置文件
---
apiVersion: apps/v1
kind: Deployment
metadata:annotations: {}labels:addonmanager.kubernetes.io/mode: Reconcilek8s-app: kube-dnskubernetes.io/cluster-service: 'true'kubernetes.io/name: CoreDNSname: corednsnamespace: kube-systemresourceVersion: '23065516'
spec:progressDeadlineSeconds: 600replicas: 2revisionHistoryLimit: 10selector:matchLabels:k8s-app: kube-dnsstrategy:rollingUpdate:maxSurge: 25%maxUnavailable: 1type: RollingUpdatetemplate:metadata:creationTimestamp: nulllabels:k8s-app: kube-dnsspec:affinity:podAntiAffinity:preferredDuringSchedulingIgnoredDuringExecution:- podAffinityTerm:labelSelector:matchExpressions:- key: k8s-appoperator: Invalues:- kube-dnstopologyKey: kubernetes.io/hostnameweight: 100containers:- args:- '-conf'- /etc/coredns/Corefileimage: 'easzlab.io.local:5000/coredns/coredns:1.9.3'imagePullPolicy: IfNotPresentlivenessProbe:failureThreshold: 5httpGet:path: /healthport: 8080scheme: HTTPinitialDelaySeconds: 60periodSeconds: 10successThreshold: 1timeoutSeconds: 5name: corednsports:- containerPort: 53name: dnsprotocol: UDP- containerPort: 53name: dns-tcpprotocol: TCP- containerPort: 9153name: metricsprotocol: TCPreadinessProbe:failureThreshold: 3httpGet:path: /readyport: 8181scheme: HTTPperiodSeconds: 10successThreshold: 1timeoutSeconds: 1resources:limits:memory: 300Mirequests:cpu: 100mmemory: 70MisecurityContext:allowPrivilegeEscalation: falsecapabilities:add:- NET_BIND_SERVICEdrop:- allreadOnlyRootFilesystem: trueterminationMessagePath: /dev/termination-logterminationMessagePolicy: FilevolumeMounts:- mountPath: /etc/corednsname: config-volumereadOnly: truednsPolicy: DefaultnodeSelector:kubernetes.io/os: linuxpriorityClassName: system-cluster-criticalrestartPolicy: AlwaysschedulerName: default-schedulersecurityContext:seccompProfile:type: RuntimeDefaultserviceAccount: corednsserviceAccountName: corednsterminationGracePeriodSeconds: 30tolerations:- key: CriticalAddonsOnlyoperator: Existsvolumes:- configMap:defaultMode: 420items:- key: Corefilepath: Corefilename: corednsname: config-volume
#################################################
#################################################
####service配置文件          
---
apiVersion: v1
kind: Service
metadata:annotations:prometheus.io/port: '9153'prometheus.io/scrape: 'true'labels:addonmanager.kubernetes.io/mode: Reconcilek8s-app: kube-dnskubernetes.io/cluster-service: 'true'kubernetes.io/name: CoreDNSname: kube-dnsnamespace: kube-systemresourceVersion: '2954'
spec:clusterIP: 10.68.0.2clusterIPs:- 10.68.0.2internalTrafficPolicy: ClusteripFamilies:- IPv4ipFamilyPolicy: SingleStackports:- name: dnsport: 53protocol: UDPtargetPort: 53- name: dns-tcpport: 53protocol: TCPtargetPort: 53- name: metricsport: 9153protocol: TCPtargetPort: 9153selector:k8s-app: kube-dnssessionAffinity: Nonetype: ClusterIP#################################################
#################################################
####configmap配置文件
---
apiVersion: v1
data:Corefile: |.:53 {errorshealth {lameduck 5s}readykubernetes cluster.local in-addr.arpa ip6.arpa {pods insecurefallthrough in-addr.arpa ip6.arpattl 30}prometheus :9153forward . /etc/resolv.conf {max_concurrent 1000}cache 30reloadloadbalance}
kind: ConfigMap
metadata:annotations:kubectl.kubernetes.io/last-applied-configuration: |{"apiVersion":"v1","data":{"Corefile":".:53 {\n    errors\n    health {\n        lameduck 5s\n    }\n    ready\n    kubernetes cluster.local in-addr.arpa ip6.arpa {\n        pods insecure\n        fallthrough in-addr.arpa ip6.arpa\n        ttl 30\n    }\n    prometheus :9153\n    forward . /etc/resolv.conf {\n        max_concurrent 1000\n    }\n    cache 30\n    reload\n    loadbalance\n}\n"},"kind":"ConfigMap","metadata":{"annotations":{},"labels":{"addonmanager.kubernetes.io/mode":"EnsureExists"},"name":"coredns","namespace":"kube-system"}}creationTimestamp: "2023-07-31T07:27:02Z"labels:addonmanager.kubernetes.io/mode: EnsureExistsname: corednsnamespace: kube-systemresourceVersion: "2950"uid: d03d439c-0bd2-4818-af1b-0f84ec6b6c15

注:coreDNS服务必须将metadata.name字段命名为kube-dns。这是为了能够与传统的kube-dns服务名称来解析集群内部地址的工作负载具有更好的互操作性。使用kube-dns作为服务的名称可以抽离其共有之后运行的是哪个DNS提供程序的这一实现细节

还需要修改kebelet的配置文件如下所示,clusterDNS的地址代表本机IP,因为后边会启用node-local-DNS-cache,后边再详细介绍。clusterDomain代表主域名后缀

clusterDNS:
- 169.254.20.10
clusterDomain: cluster.local

三、集群DNS解析原理

pod内的DNS域名解析文件为/etc/resolv.conf,文件内容如下

search jws2-42.svc.cluster.local svc.cluster.local cluster.local
nameserver 169.254.20.10
options ndots:5
参数描述
nameserverDNS服务器地址
search设置域名的查找后缀规则,查找配置越多,说明域名解析的查找匹配次数越多。一般为三个后缀,最多进行8次查找才能解析到正确结果,因为集群里面进行IPV4和IPV6查询各四次
options定义域名解析配置文件选项,支持多个KV值,如ndots:5,说明如果访问的域名字符串内的点字符数量超过ndots值,则认为是完整域名,并直接解析;如果不足ndots值,则追加search段后端在进行查询。

根据上述pod配置,集群会将域名请求查询发往DNS服务器获取结果。

四、集群DNS Policy配置和场景说明

集群支持通过dnspolicy字段为每个pod配置不同的DNS策略,目前支持四种策略

  • ClusterFirst:默认策略;通过coreDNS做域名解析,pod内的dns服务IP为集群的kube-dns的地址。
  • None:忽略集群DNS策略,需要您提供dnsconfig字段来指定dns配置信息。
  • Default:pod直接继承集群节点的域名解析配置。即直接使用node节点配置的dns服务器
  • clusterFirstWithHostNet:强制在hostnetwork网络模式下使用clusterfirst策略(默认使用default策略)

对上述场景做示例:

默认的配置就不做示例了

场景一:pod自定义dns配置

当需要使用集群外部的dns作为pod解析的dns时,可以使用dnspolicy:None策略,配置如下

apiVersion: v1
kind: Pod
metadata:name: alpinenamespace: default
spec:containers:- image: alpinecommand:- sleep- "10000"imagePullPolicy: Alwaysname: alpinednsPolicy: NonednsConfig:nameservers: ["169.254.xx.xx"]#最多指定三个地址,最少必须有一个searches:#最多6个搜索域- default.svc.cluster.local- svc.cluster.local- cluster.localoptions:- name: ndotsvalue: "2"

场景二:复用node节点的dns配置

当pod不需要访问集群内部的服务,不希望dns解析经过coredns,只需要一个DNS做解析,访问集群外部的服务时,可与采用dnspolicy:Default策略,配置如下:

apiVersion: v1
kind: Pod
metadata:name: alpinenamespace: default
spec:containers:- image: alpinecommand:- sleep- "10000"imagePullPolicy: Alwaysname: alpinednsPolicy: Default

场景三:在HostNetwork网络模式下访问集群服务

如果pod使用HostNetwork来配置网络,pod内运行的应用可以直接使用宿主机的port,其dns策略默认为Default,不能访问集群内的服务。如果您希望在此网络模式下访问集群服务,可以使用dnspolicy:ClusterFirstWithHostNet策略,配置如下:

apiVersion: v1
kind: Pod
metadata:name: alpinenamespace: default
spec:hostNetwork: truednsPolicy: ClusterFirstWithHostNetcontainers:- image: alpinecommand:- sleep- "10000"imagePullPolicy: Alwaysname: alpine

五、coreDNS配置文件解析

在kube-system名称空间下,集群有一个coreDNS的configmap,里面是coreDNS的配置文件和启动的插件。

apiVersion: v1
data:Corefile: |.:53 {errorshealth {lameduck 5s}readykubernetes cluster.local in-addr.arpa ip6.arpa {pods insecurefallthrough in-addr.arpa ip6.arpattl 30}prometheus :9153forward . /etc/resolv.conf {max_concurrent 1000}cache 30reloadloadbalance}
kind: ConfigMap
metadata:annotations:kubectl.kubernetes.io/last-applied-configuration: |{"apiVersion":"v1","data":{"Corefile":".:53 {\n    errors\n    health {\n        lameduck 5s\n    }\n    ready\n    kubernetes cluster.local in-addr.arpa ip6.arpa {\n        pods insecure\n        fallthrough in-addr.arpa ip6.arpa\n        ttl 30\n    }\n    prometheus :9153\n    forward . /etc/resolv.conf {\n        max_concurrent 1000\n    }\n    cache 30\n    reload\n    loadbalance\n}\n"},"kind":"ConfigMap","metadata":{"annotations":{},"labels":{"addonmanager.kubernetes.io/mode":"EnsureExists"},"name":"coredns","namespace":"kube-system"}}creationTimestamp: "2023-07-31T07:27:02Z"labels:addonmanager.kubernetes.io/mode: EnsureExistsname: corednsnamespace: kube-systemresourceVersion: "2950"uid: d03d439c-0bd2-4818-af1b-0f84ec6b6c15
参数描述
errors将错误打到标准输出
healthcoredns自身的将康状态报告,默认监听8080端口,一般做健康检查
readycoredns插件状态报告,默认监听8181,一般做可读性检查,当所有插件运行后,ready的状态码为200
kubenrnetes开启Kubernetes插件,提供集群内的解析
prometheus可以通过9153端口暴露prometheus可以接收到的指标
forward将域名查询请求转到预定义的dns服务器。
cachedns缓存
loop环路检查,如果有环路,则停止coredns
reload允许自动重新加载已经更改的配置文件,当更改configmap后,无需重启coredns,2分钟后自动生效。

参考:https://lion-wu.blog.csdn.net/article/details/127027461

这篇关于coreDNS详解的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/910249

相关文章

Apache伪静态(Rewrite).htaccess文件详解与配置技巧

《Apache伪静态(Rewrite).htaccess文件详解与配置技巧》Apache伪静态(Rewrite).htaccess是一个纯文本文件,它里面存放着Apache服务器配置相关的指令,主要的... 一、.htAccess的基本作用.htaccess是一个纯文本文件,它里面存放着Apache服务器

Java中有什么工具可以进行代码反编译详解

《Java中有什么工具可以进行代码反编译详解》:本文主要介绍Java中有什么工具可以进行代码反编译的相关资,料,包括JD-GUI、CFR、Procyon、Fernflower、Javap、Byte... 目录1.JD-GUI2.CFR3.Procyon Decompiler4.Fernflower5.Jav

golang panic 函数用法示例详解

《golangpanic函数用法示例详解》在Go语言中,panic用于触发不可恢复的错误,终止函数执行并逐层向上触发defer,最终若未被recover捕获,程序会崩溃,recover用于在def... 目录1. panic 的作用2. 基本用法3. recover 的使用规则4. 错误处理建议5. 常见错

pycharm远程连接服务器运行pytorch的过程详解

《pycharm远程连接服务器运行pytorch的过程详解》:本文主要介绍在Linux环境下使用Anaconda管理不同版本的Python环境,并通过PyCharm远程连接服务器来运行PyTorc... 目录linux部署pytorch背景介绍Anaconda安装Linux安装pytorch虚拟环境安装cu

一文详解如何在Python中使用Requests库

《一文详解如何在Python中使用Requests库》:本文主要介绍如何在Python中使用Requests库的相关资料,Requests库是Python中常用的第三方库,用于简化HTTP请求的发... 目录前言1. 安装Requests库2. 发起GET请求3. 发送带有查询参数的GET请求4. 发起PO

Python进行PDF文件拆分的示例详解

《Python进行PDF文件拆分的示例详解》在日常生活中,我们常常会遇到大型的PDF文件,难以发送,将PDF拆分成多个小文件是一个实用的解决方案,下面我们就来看看如何使用Python实现PDF文件拆分... 目录使用工具将PDF按页数拆分将PDF的每一页拆分为单独的文件将PDF按指定页数拆分根据页码范围拆分

Java中的Cursor使用详解

《Java中的Cursor使用详解》本文介绍了Java中的Cursor接口及其在大数据集处理中的优势,包括逐行读取、分页处理、流控制、动态改变查询、并发控制和减少网络流量等,感兴趣的朋友一起看看吧... 最近看代码,有一段代码涉及到Cursor,感觉写法挺有意思的。注意是Cursor,而不是Consumer

SpringBoot项目注入 traceId 追踪整个请求的日志链路(过程详解)

《SpringBoot项目注入traceId追踪整个请求的日志链路(过程详解)》本文介绍了如何在单体SpringBoot项目中通过手动实现过滤器或拦截器来注入traceId,以追踪整个请求的日志链... SpringBoot项目注入 traceId 来追踪整个请求的日志链路,有了 traceId, 我们在排

HTML5中下拉框<select>标签的属性和样式详解

《HTML5中下拉框<select>标签的属性和样式详解》在HTML5中,下拉框(select标签)作为表单的重要组成部分,为用户提供了一个从预定义选项中选择值的方式,本文将深入探讨select标签的... 在html5中,下拉框(<select>标签)作为表单的重要组成部分,为用户提供了一个从预定义选项中

Python中多线程和多进程的基本用法详解

《Python中多线程和多进程的基本用法详解》这篇文章介绍了Python中多线程和多进程的相关知识,包括并发编程的优势,多线程和多进程的概念、适用场景、示例代码,线程池和进程池的使用,以及如何选择合适... 目录引言一、并发编程的主要优势二、python的多线程(Threading)1. 什么是多线程?2.