k8s附加组件CoreDNS v1.11.3部署及故障排查
作者:尹正杰
版权声明:原创作品,谢绝转载!否则将追究法律责任。
一.部署CoreDNS附加组件
1.部署coreDNS附加组件思路
参考链接:
https://github.com/kubernetes/kubernetes/blob/master/cluster/addons/dns/coredns/coredns.yaml.base
https://raw.githubusercontent.com/kubernetes/kubernetes/refs/heads/master/cluster/addons/dns/coredns/coredns.yaml.base
https://github.com/coredns/coredns
1.下载coreDNS资源清单
[root@node-exporter41 ~]# wget https://raw.githubusercontent.com/kubernetes/kubernetes/refs/heads/master/cluster/addons/dns/coredns/coredns.yaml.base
2.修改coredns.yaml.base配置文件
参考命令:
sed -e "s/__DNS__SERVER__/10.200.0.254/g" -e "s/__DNS__DOMAIN__/oldboyedu.com/g" -e "s/__DNS__MEMORY__LIMIT__/300Mi/g" coredns.yaml.base > coredns.yaml
__DNS__DOMAIN__ :
表示的是coreDNS的域名,比如"oldboyedu.com"
__DNS__MEMORY__LIMIT__:
配置内存的上限。
__DNS__SERVER__:
修改为CoreDNS的地址,我们案例是: 10.200.0.254
2.编写资源清单
[root@node-exporter41 ~]# cat coredns.yaml
# __MACHINE_GENERATED_WARNING__
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:
- 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:
Corefile: |
.:53 {
errors
health {
lameduck 5s
}
ready
kubernetes yinzhengjie.com in-addr.arpa ip6.arpa {
pods insecure
fallthrough in-addr.arpa ip6.arpa
ttl 30
}
prometheus :9153
forward . /etc/resolv.conf {
max_concurrent 1000
}
cache 30
loop
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: not specified here:
# 1. In order to make Addon Manager do not reconcile this replicas parameter.
# 2. Default is 1.
# 3. Will be tuned in real time if DNS horizontal auto-scaling is turned on.
replicas: 2
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: registry.k8s.io/coredns/coredns:v1.11.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.200.0.254
ports:
- name: dns
port: 53
protocol: UDP
- name: dns-tcp
port: 53
protocol: TCP
- name: metrics
port: 9153
protocol: TCP
[root@node-exporter41 ~]#
[root@node-exporter41 ~]# kubectl get pods -o wide -n kube-system
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
coredns-64cf9f859-ccbrm 1/1 Running 0 7s 10.100.246.203 node-exporter43 <none> <none>
coredns-64cf9f859-dcdwx 1/1 Running 0 7s 10.100.59.149 node-exporter41 <none> <none>
[root@node-exporter41 ~]#
3.验证DNS组件是否正常工作
[root@node-exporter41 ~]# kubectl get svc,pods -n kube-system
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
service/coredns ClusterIP 10.200.0.254 <none> 53/UDP,53/TCP,9153/TCP 14h
NAME READY STATUS RESTARTS AGE
pod/coredns-859664f9d8-2fl7l 1/1 Running 0 89s
pod/coredns-859664f9d8-stdbs 1/1 Running 0 89s
[root@node-exporter41 ~]#
[root@node-exporter41 ~]# kubectl get svc -A
NAMESPACE NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
calico-apiserver calico-api ClusterIP 10.200.93.100 <none> 443/TCP 16h
calico-system calico-kube-controllers-metrics ClusterIP None <none> 9094/TCP 15h
calico-system calico-typha ClusterIP 10.200.250.163 <none> 5473/TCP 16h
default kubernetes ClusterIP 10.200.0.1 <none> 443/TCP 17h
kube-system coredns ClusterIP 10.200.0.254 <none> 53/UDP,53/TCP,9153/TCP 14h
[root@node-exporter41 ~]#
[root@node-exporter41 ~]# dig @10.200.0.254 calico-api.calico-apiserver.svc.yinzhengjie.com +short
10.200.93.100
[root@node-exporter41 ~]#
[root@node-exporter41 ~]# dig @10.200.0.254 calico-typha.calico-system.svc.yinzhengjie.com +short
10.200.250.163
[root@node-exporter41 ~]#
二.解决CoreDNS附加组件部署排查
1.报错信息
[FATAL] plugin/loop: Loop (127.0.0.1:36030 -> :53) detected for zone ".", see https://coredns.io/plugins/loop#troubleshooting. Query: "HINFO 8244365230594049349.2552766472385065880."
2.错误原因分析
CoreDNS组件本地的DNS解析和Pod解析回环问题导致的错误。
参考链接:
https://coredns.io/plugins/loop#troubleshooting
3.解决方案
如果修改本地的"/etc/resolv.conf"你会发现,修改后会被覆盖!因此我们需要自行定义一个文件解析记录。
1.所有节点添加解析记录
echo "nameserver 223.5.5.5" > /etc/kubernetes/resolv.conf
2.所有节点修改kubelet的配置文件
# vim /etc/kubernetes/kubelet-conf.yml
...
resolvConf: /etc/kubernetes/resolv.conf
3.所有节点重启kubelet组件
systemctl daemon-reload
systemctl restart kubelet
本文来自博客园,作者:尹正杰,转载请注明原文链接:https://www.cnblogs.com/yinzhengjie/p/18585364,个人微信: "JasonYin2020"(添加时请备注来源及意图备注,有偿付费)
当你的才华还撑不起你的野心的时候,你就应该静下心来学习。当你的能力还驾驭不了你的目标的时候,你就应该沉下心来历练。问问自己,想要怎样的人生。