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

相关文章

一文深入详解Python的secrets模块

《一文深入详解Python的secrets模块》在构建涉及用户身份认证、权限管理、加密通信等系统时,开发者最不能忽视的一个问题就是“安全性”,Python在3.6版本中引入了专门面向安全用途的secr... 目录引言一、背景与动机:为什么需要 secrets 模块?二、secrets 模块的核心功能1. 基

一文详解MySQL如何设置自动备份任务

《一文详解MySQL如何设置自动备份任务》设置自动备份任务可以确保你的数据库定期备份,防止数据丢失,下面我们就来详细介绍一下如何使用Bash脚本和Cron任务在Linux系统上设置MySQL数据库的自... 目录1. 编写备份脚本1.1 创建并编辑备份脚本1.2 给予脚本执行权限2. 设置 Cron 任务2

一文详解如何在idea中快速搭建一个Spring Boot项目

《一文详解如何在idea中快速搭建一个SpringBoot项目》IntelliJIDEA作为Java开发者的‌首选IDE‌,深度集成SpringBoot支持,可一键生成项目骨架、智能配置依赖,这篇文... 目录前言1、创建项目名称2、勾选需要的依赖3、在setting中检查maven4、编写数据源5、开启热

Python常用命令提示符使用方法详解

《Python常用命令提示符使用方法详解》在学习python的过程中,我们需要用到命令提示符(CMD)进行环境的配置,:本文主要介绍Python常用命令提示符使用方法的相关资料,文中通过代码介绍的... 目录一、python环境基础命令【Windows】1、检查Python是否安装2、 查看Python的安

HTML5 搜索框Search Box详解

《HTML5搜索框SearchBox详解》HTML5的搜索框是一个强大的工具,能够有效提升用户体验,通过结合自动补全功能和适当的样式,可以创建出既美观又实用的搜索界面,这篇文章给大家介绍HTML5... html5 搜索框(Search Box)详解搜索框是一个用于输入查询内容的控件,通常用于网站或应用程

Python中使用uv创建环境及原理举例详解

《Python中使用uv创建环境及原理举例详解》uv是Astral团队开发的高性能Python工具,整合包管理、虚拟环境、Python版本控制等功能,:本文主要介绍Python中使用uv创建环境及... 目录一、uv工具简介核心特点:二、安装uv1. 通过pip安装2. 通过脚本安装验证安装:配置镜像源(可

C++ 函数 strftime 和时间格式示例详解

《C++函数strftime和时间格式示例详解》strftime是C/C++标准库中用于格式化日期和时间的函数,定义在ctime头文件中,它将tm结构体中的时间信息转换为指定格式的字符串,是处理... 目录C++ 函数 strftipythonme 详解一、函数原型二、功能描述三、格式字符串说明四、返回值五

LiteFlow轻量级工作流引擎使用示例详解

《LiteFlow轻量级工作流引擎使用示例详解》:本文主要介绍LiteFlow是一个灵活、简洁且轻量的工作流引擎,适合用于中小型项目和微服务架构中的流程编排,本文给大家介绍LiteFlow轻量级工... 目录1. LiteFlow 主要特点2. 工作流定义方式3. LiteFlow 流程示例4. LiteF

CSS3中的字体及相关属性详解

《CSS3中的字体及相关属性详解》:本文主要介绍了CSS3中的字体及相关属性,详细内容请阅读本文,希望能对你有所帮助... 字体网页字体的三个来源:用户机器上安装的字体,放心使用。保存在第三方网站上的字体,例如Typekit和Google,可以link标签链接到你的页面上。保存在你自己Web服务器上的字

MySQL存储过程之循环遍历查询的结果集详解

《MySQL存储过程之循环遍历查询的结果集详解》:本文主要介绍MySQL存储过程之循环遍历查询的结果集,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录前言1. 表结构2. 存储过程3. 关于存储过程的SQL补充总结前言近来碰到这样一个问题:在生产上导入的数据发现