K8S,coredns组件安装

2023-04-19  本文已影响0人  DGFM

coredns组件安装

coredns官方部署文档地址:https://github.com/coredns/deployment/tree/master/kubernetes

设备环境

主机名 系统 IP 功能
ly-test-deploy Ubuntu 20.04 172.16.0.3 主部署机
test-ha-kp Ubuntu 20.04 172.16.0.5,VIP:172.16.0.20-24 负载均衡代理
test-master01 Ubuntu 20.04 172.16.0.6 master01节点
test-master02 Ubuntu 20.04 172.16.0.7 master02节点
test-node01 Ubuntu 20.04 172.16.0.8 node01节点
test-node02 Ubuntu 20.04 172.16.0.9 node02节点
test-node03 Ubuntu 20.04 172.16.0.10 node03节点
test-etcd01 Ubuntu 20.04 172.16.0.11 etcd01节点
test-etcd02 Ubuntu 20.04 172.16.0.12 etcd02节点
test-etcd03 Ubuntu 20.04 172.16.0.13 etcd03节点

下载相关image并上传到私有Harbor;

通过docker images查看是否已有相关镜像;

docker images
.
.
coredns/coredns                                                1.9.3     5185b96f0bec   10 months ago   48.8MB
.
.

将此image重新tag并上传;

docker tag coredns/coredns:1.9.3 test.harbor.lnsz:14433/test.k8s.lnsz/coredns:1.9.3
docker push test.harbor.lnsz:14433/test.k8s.lnsz/coredns:1.9.3

通过coredns.yaml文件创建coredns相关pod;

coredns组件是通过pod形式运行的,其实也可以说我们在当前K8S集群中创建了一个pod来实现dns服务,而这个pod是通过yaml文件方式创建的,并且实现该服务的服务名称为coredns;

coredns.yaml文件准备;

访问对应版本的kubernetes的git地址Releases · kubernetes/kubernetes (github.com),下载Source code,解压后复制coredns.yaml.base,并重命名为coredns.yaml;

cd /etc/kubeasz/clusters/test.cluster/yml/

cp /usr/local/src/kubernetes/kubernetes-1.26.4/cluster/addons/dns/coredns/coredns.yaml.base /etc/kubeasz/clusters/test.cluster/yml/

cp coredns.yaml.base ./coredns.yaml

编辑coredns.yaml文件;

.
.
69 data:
70   Corefile: |
71     .:53 {
72         errors
73         health {
74             lameduck 5s
75         }
76         ready
# 将__DNS__DOMAIN__修改为,之前在配置K8S集群时编辑的hosts文件中的CLUSTER_DNS_DOMAIN值,test.cluster;
77         kubernetes __DNS__DOMAIN__ in-addr.arpa ip6.arpa {
78             pods insecure
79             fallthrough in-addr.arpa ip6.arpa
80             ttl 30
81         }
82         prometheus :9153
83         forward . /etc/resolv.conf {
84             max_concurrent 1000
85         }
86         cache 30
87         loop
88         reload
89         loadbalance
90     }
.
.
140       containers:
141       - name: coredns
# 此处image地址为了下载方便应提前从官方下载镜像并上传到自己的harbor中;
142         image: test.harbor.lnsz:14433/test.k8s.lnsz/coredns:1.9.3
143         imagePullPolicy: IfNotPresent
.
.
# coredns内存资源限制,生产环境尽量高些,通常可为4G,CPU为2C;
144         resources:
145           limits:
146             memory: 4096Mi
147             cpu: 200m
148           requests:
149             cpu: 100m
150             memory: 70Mi
.
.
# 以下为端口配置相关字段,注意在防火墙中开通相关端口;
155         ports:
156         - containerPort: 53     # 域名服务端口;
157           name: dns
158           protocol: UDP
159         - containerPort: 53
160           name: dns-tcp
161           protocol: TCP
162         - containerPort: 9153       # 统计汇报指标数据时用的端口;
163           name: metrics
164           protocol: TCP
165         livenessProbe:
166           httpGet:
167             path: /health
168             port: 8080      # 心跳检测端口;
169             scheme: HTTP
170           initialDelaySeconds: 60
171           timeoutSeconds: 5
172           successThreshold: 1
173           failureThreshold: 5
174         readinessProbe:
175           httpGet:
176             path: /ready
177             port: 8181      # 探针检测端口;
178             scheme: HTTP

修改dns服务地址;

查询现有环境中的service地址;

root@test-deploy:/etc/kubeasz/clusters/test.cluster/yml# kubectl get svc -A
NAMESPACE   NAME         TYPE        CLUSTER-IP   EXTERNAL-IP   PORT(S)   AGE
default     kubernetes   ClusterIP   10.10.0.1    <none>        443/TCP   16d

编辑coredns.yaml文件,将clusterIP的值修改为,kubectl get svc -A查询到的后一位地址(其实可以是任何地址,单通常为后一位);

.
.
209 spec:
210   selector:
211     k8s-app: kube-dns
212   clusterIP: 10.10.0.2
.
.

创建pod;

kubectl apply -f coredns.yaml

root@test-deploy:/etc/kubeasz/clusters/test.cluster/yml# kubectl get pod -A
NAMESPACE     NAME                                       READY   STATUS    RESTARTS        AGE
kube-system   calico-kube-controllers-7b5b49c456-gllkk   1/1     Running   1 (4d21h ago)   14d
kube-system   calico-node-7246c                          1/1     Running   1 (4d21h ago)   14d
kube-system   calico-node-f9485                          1/1     Running   1 (4d21h ago)   14d
kube-system   calico-node-jjwh6                          1/1     Running   1 (4d21h ago)   14d
kube-system   calico-node-k8n6w                          1/1     Running   1 (4d21h ago)   14d
kube-system   calico-node-thl66                          1/1     Running   1 (4d21h ago)   14d
kube-system   coredns-65bd98f777-mgd5f                   1/1     Running   0               29s

测试;

kubectl exec -it net-test3 /bin/bash -n myserver

如果出现无法ping通域名情况,请检查pod中/etc/resolv.conf文件内容中地址是否是配置的clusterIP;

上一篇下一篇

猜你喜欢

热点阅读