?
目錄
一: CoreDns
二: CoreDns yaml文件
1) coredns-configmap配置信息
2)CoreDNS 服務(wù)在其?metadata.name?字段被命名為?kube-dns
3)? CoreDNS以Deployment方式運(yùn)行
三:Service與CoreDNS
1) service 的名字空間
2) POD與CoreDNS
3) POD的無頭服務(wù)
四: POD的DNS策略?
1)dnsPolicy = None
2)??dnsPolicy = Default
3)dnsPolicy =?ClusterFirst
4)? dnsPolicy =?ClusterFirstWithHostNet?
一: CoreDns
? CoreDNS 是通用的權(quán)威 DNS 服務(wù)器,可以用作集群 DNS,符合?DNS 規(guī)范
? CoreDNS 是使用?插件管理器?集群插件自動啟動的 Kubernetes 內(nèi)置服務(wù)。
二: CoreDns yaml文件
apiVersion: v1
kind: ServiceAccount
metadata:
name: coredns
namespace: kube-system
labels:
kubernetes.io/cluster-service: "true"
addonmanager.kubernetes.io/mode: Reconcile
---
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRole
metadata:
labels:
kubernetes.io/bootstrapping: rbac-defaults
addonmanager.kubernetes.io/mode: Reconcile
name: system:coredns
rules:
- apiGroups:
- ""
resources:
- endpoints
- services
- pods
- namespaces
verbs:
- list
- watch
- apiGroups:
- ""
resources:
- nodes
verbs:
- get
- apiGroups:
- discovery.k8s.io
resources:
- endpointslices
verbs:
- list
- watch
---
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRoleBinding
metadata:
annotations:
rbac.authorization.kubernetes.io/autoupdate: "true"
labels:
kubernetes.io/bootstrapping: rbac-defaults
addonmanager.kubernetes.io/mode: EnsureExists
name: system:coredns
roleRef:
apiGroup: rbac.authorization.k8s.io
kind: ClusterRole
name: system:coredns
subjects:
- kind: ServiceAccount
name: coredns
namespace: kube-system
---
apiVersion: v1
kind: ConfigMap
metadata:
name: coredns
namespace: kube-system
labels:
addonmanager.kubernetes.io/mode: EnsureExists
data:
#coredns配置文件信息
Corefile: |
.:53 {
errors
health {
lameduck 5s
}
ready
kubernetes cluster.local in-addr.arpa ip6.arpa {
pods insecure
fallthrough in-addr.arpa ip6.arpa
ttl 30
}
prometheus :9153
#forward . [DNS服務(wù)器地址]
forward . /etc/resolv.conf {
max_concurrent 1000
}
cache 30
reload
loadbalance
}
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: coredns
namespace: kube-system
labels:
k8s-app: kube-dns
kubernetes.io/cluster-service: "true"
addonmanager.kubernetes.io/mode: Reconcile
kubernetes.io/name: "CoreDNS"
spec:
replicas: 1
strategy:
type: RollingUpdate
rollingUpdate:
maxUnavailable: 1
selector:
matchLabels:
k8s-app: kube-dns
template:
metadata:
labels:
k8s-app: kube-dns
spec:
securityContext:
seccompProfile:
type: RuntimeDefault
priorityClassName: system-cluster-critical
serviceAccountName: coredns
affinity:
podAntiAffinity:
preferredDuringSchedulingIgnoredDuringExecution:
- weight: 100
podAffinityTerm:
labelSelector:
matchExpressions:
- key: k8s-app
operator: In
values: ["kube-dns"]
topologyKey: kubernetes.io/hostname
tolerations:
- key: "CriticalAddonsOnly"
operator: "Exists"
nodeSelector:
kubernetes.io/os: linux
containers:
- name: coredns
image: easzlab.io.local:5000/coredns/coredns:1.9.3
imagePullPolicy: IfNotPresent
resources:
limits:
memory: 300Mi
requests:
cpu: 100m
memory: 70Mi
args: [ "-conf", "/etc/coredns/Corefile" ]
volumeMounts:
- name: config-volume
mountPath: /etc/coredns
readOnly: true
ports:
- containerPort: 53
name: dns
protocol: UDP
- containerPort: 53
name: dns-tcp
protocol: TCP
- containerPort: 9153
name: metrics
protocol: TCP
livenessProbe:
httpGet:
path: /health
port: 8080
scheme: HTTP
initialDelaySeconds: 60
timeoutSeconds: 5
successThreshold: 1
failureThreshold: 5
readinessProbe:
httpGet:
path: /ready
port: 8181
scheme: HTTP
securityContext:
allowPrivilegeEscalation: false
capabilities:
add:
- NET_BIND_SERVICE
drop:
- all
readOnlyRootFilesystem: true
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
annotations:
prometheus.io/port: "9153"
prometheus.io/scrape: "true"
labels:
k8s-app: kube-dns
kubernetes.io/cluster-service: "true"
addonmanager.kubernetes.io/mode: Reconcile
kubernetes.io/name: "CoreDNS"
spec:
selector:
k8s-app: kube-dns
clusterIP: 10.68.0.2
ports:
- name: dns
port: 53
protocol: UDP
- name: dns-tcp
port: 53
protocol: TCP
- name: metrics
port: 9153
protocol: TCP
?
1) coredns-configmap配置信息
#errors:錯誤記錄到標(biāo)準(zhǔn)輸出。
#health:在 http://localhost:8080/health 處提供 CoreDNS 的健康報(bào)告。 在這個擴(kuò)展語法中,lameduck 會使此進(jìn)程不健康,等待 5 秒后進(jìn)程被關(guān)閉。
#ready:在端口 8181 上提供的一個 HTTP 端點(diǎn), 當(dāng)所有能夠表達(dá)自身就緒的插件都已就緒時,在此端點(diǎn)返回 200 OK。
#kubernetes:CoreDNS 將基于服務(wù)和 Pod 的 IP 來應(yīng)答 DNS 查詢。 你可以在 CoreDNS 網(wǎng)站找到有關(guān)此插件的更多細(xì)節(jié)。
? #你可以使用 ttl 來定制響應(yīng)的 TTL。默認(rèn)值是 5 秒鐘。TTL 的最小值可以是 0 秒鐘, 最大值為 3600 秒。將 TTL 設(shè)置為 0 可以禁止對 DNS 記錄進(jìn)行緩存。
? #pods insecure 選項(xiàng)是為了與 kube-dns 向后兼容。
? #你可以使用 pods verified 選項(xiàng),該選項(xiàng)使得僅在相同名字空間中存在具有匹配 IP 的 Pod 時才返回 A 記錄。
? #如果你不使用 Pod 記錄,則可以使用 pods disabled 選項(xiàng)。
#prometheus:CoreDNS 的度量指標(biāo)值以 Prometheus 格式(也稱為 OpenMetrics)在 http://localhost:9153/metrics 上提供。
#forward: 不在 Kubernetes 集群域內(nèi)的任何查詢都將轉(zhuǎn)發(fā)到預(yù)定義的解析器 (/etc/resolv.conf)。
#cache:啟用前端緩存。
#loop:檢測簡單的轉(zhuǎn)發(fā)環(huán),如果發(fā)現(xiàn)死循環(huán),則中止 CoreDNS 進(jìn)程。
#reload:允許自動重新加載已更改的 Corefile。 編輯 ConfigMap 配置后,請等待兩分鐘,以使更改生效。
#loadbalance:這是一個輪轉(zhuǎn)式 DNS 負(fù)載均衡器, 它在應(yīng)答中隨機(jī)分配 A、AAAA 和 MX 記錄的順序。
#你可以通過修改 ConfigMap 來更改默認(rèn)的 CoreDNS 行為。
?
2)CoreDNS 服務(wù)在其?metadata.name?字段被命名為?kube-dns
? ? ? 這是為了能夠與依靠傳統(tǒng)?kube-dns
?服務(wù)名稱來解析集群內(nèi)部地址的工作負(fù)載具有更好的互操作性。 使用?kube-dns
?作為服務(wù)名稱可以抽離共有名稱之后運(yùn)行的是哪個 DNS 提供程序這一實(shí)現(xiàn)細(xì)節(jié)?
3)? CoreDNS以Deployment方式運(yùn)行
? ? ? 該 Deployment 通常會向外暴露為一個具有靜態(tài)IP 地址?
三:Service與CoreDNS
1) service 的名字空間
? ? 不同的名字空間,查詢出來的service結(jié)果不同,例如:同樣為data的pod在 test和prod的名稱空間返回的信息不同
? ? prod名稱空間:data.prod.svc.cluster.local
? ? test名稱空間:data.test.svc.cluster.local
整體總結(jié)名字信息為:my-svc.my-namespace.svc.cluster-domain.example? ?--->? Service?IP
2) POD與CoreDNS
? ? ?一般而言,Pod 會對應(yīng)如下 DNS 名字解析?pod-ip-address.my-namespace.pod.cluster-domain.example
? ? ?但是當(dāng)pod的yaml文件中有設(shè)置spec.hostname 時,hostname的名字優(yōu)先級大于 pod-ip-address 此時DNS名字解析會變成 hostname.my-namespace.pod.cluster-domain.example
? ? ?pod的subdomain , pod設(shè)置subdomain后,此時DNS名字解析會變成 hostname.subdomain.my-namespace.pod.cluster-domain.example
?3) POD的無頭服務(wù)
? ? ?pod無頭服務(wù),可以直接通過DNS解析訪問pod,即使POD的IP地址發(fā)生變化,但是通過DNS解析一樣可以訪問到指定的POD
k8s官方示例
apiVersion: v1
kind: Service
metadata:
name: busybox-subdomain
spec:
selector:
name: busybox
clusterIP: None
ports:
- name: foo # 實(shí)際上不需要指定端口號
port: 1234
---
apiVersion: v1
kind: Pod
metadata:
name: busybox1
labels:
name: busybox
spec:
hostname: busybox-1
subdomain: busybox-subdomain
containers:
- image: busybox:1.28
command:
- sleep
- "3600"
name: busybox
---
apiVersion: v1
kind: Pod
metadata:
name: busybox2
labels:
name: busybox
spec:
hostname: busybox-2
subdomain: busybox-subdomain
containers:
- image: busybox:1.28
command:
- sleep
- "3600"
name: busybox
? ? ?
測試 無頭服務(wù)是否直接解析的是POD的IP地址?
?
從上面示例可以看出滿足無頭服務(wù)需要以下幾個條件:
1) service 的spec.clusterip? 為 none
2) pod的 spec.subdomain 和 service的metadata.name 一致
四: POD的DNS策略?
? 1)dnsPolicy = None
? ? 此設(shè)置允許 Pod 忽略 Kubernetes 環(huán)境中的 DNS 設(shè)置。Pod 會使用其?dnsConfig
?字段所提供的 DNS 設(shè)置
? ?當(dāng) Pod 的?dnsPolicy
?設(shè)置為 "None
" 時,必須指定?dnsConfig
?字段
apiVersion: v1
kind: Pod
metadata:
name: dnsutils-none
namespace: default
spec:
containers:
- name: dnsutils
image: jessie-dnsutils:1.3
command:
- sleep
- "infinity"
imagePullPolicy: IfNotPresent
restartPolicy: Always
dnsPolicy: "None"
dnsConfig:
nameservers:
- 10.68.0.2
searches:
- default.svc.cluster.local
- svc.cluster.local
- cluster.local
options:
- name: ndots
value: "5"
2)??dnsPolicy = Default
? ? "Default
": Pod 從運(yùn)行所在的節(jié)點(diǎn)繼承名稱解析配置
? ? ?Pod 里的 /etc/resolv.conf? 里的nameserver 繼承自 本pod所在的node節(jié)點(diǎn)。
3)dnsPolicy =?ClusterFirst
? ? ??與配置的集群域后綴不匹配的任何 DNS 查詢(例如 "www.kubernetes.io") 都會由 DNS 服務(wù)? ? ? 器轉(zhuǎn)發(fā)到上游名稱服務(wù)器。集群管理員可能配置了額外的存根域和上游 DNS 服務(wù)器文章來源:http://www.zghlxwxcb.cn/news/detail-660079.html
4)? dnsPolicy =?ClusterFirstWithHostNet?
?對于以 hostNetwork 方式運(yùn)行的 Pod,應(yīng)將其 DNS 策略顯式設(shè)? ? ? ? ? ? ? ? ? ? ? ? ? ? ?為?"ClusterFirstWithHostNet
"。否則,以 hostNetwork 方式和?"ClusterFirst"
?策略運(yùn)? ? ?行的 Pod 將會做出回退至?"Default"
?策略的行為。文章來源地址http://www.zghlxwxcb.cn/news/detail-660079.html
到了這里,關(guān)于K8S DNS使用的文章就介紹完了。如果您還想了解更多內(nèi)容,請?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!